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
use anyhow::Result;
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::process::Command;

/// Find the active Claude Code JSONL for this project.
/// Returns the path to the most recently modified JSONL.
fn find_active_session(repo_path: &Path) -> Option<PathBuf> {
    let home = std::env::var("HOME").ok()?;
    let projects_dir = PathBuf::from(&home).join(".claude").join("projects");

    let cwd = repo_path.canonicalize().ok()?;
    let cwd_key = cwd.to_string_lossy().replace('/', "-");

    let project_dir = std::fs::read_dir(&projects_dir)
        .ok()?
        .filter_map(|e| e.ok())
        .find(|e| {
            let name = e.file_name();
            let name = name.to_string_lossy();
            name == cwd_key || name.trim_start_matches('-') == cwd_key.trim_start_matches('-')
        })?
        .path();

    let mut sessions: Vec<(std::time::SystemTime, PathBuf)> = std::fs::read_dir(&project_dir)
        .ok()?
        .filter_map(|e| e.ok())
        .filter(|e| e.path().extension().and_then(|x| x.to_str()) == Some("jsonl"))
        .filter_map(|e| {
            let mtime = e.metadata().ok()?.modified().ok()?;
            Some((mtime, e.path()))
        })
        .collect();

    sessions.sort_by(|a, b| b.0.cmp(&a.0));
    sessions.into_iter().next().map(|(_, p)| p)
}

/// Parse ISO 8601 timestamp to unix seconds.
fn parse_iso_ts(ts: &str) -> u64 {
    if ts.len() < 19 {
        return 0;
    }

    let date = &ts[..10];
    let time = &ts[11..19];
    let tz = &ts[19..];

    let parts: Vec<u64> = date.split('-').filter_map(|p| p.parse().ok()).collect();
    let tparts: Vec<u64> = time.split(':').filter_map(|p| p.parse().ok()).collect();
    if parts.len() != 3 || tparts.len() != 3 {
        return 0;
    }

    let (y, m, d) = (parts[0] as i64, parts[1] as i64, parts[2] as i64);
    let (h, min, s) = (tparts[0] as i64, tparts[1] as i64, tparts[2] as i64);

    let days = days_from_epoch(y, m, d);
    let mut unix = days * 86400 + h * 3600 + min * 60 + s;

    // strip milliseconds e.g. ".318" before parsing timezone
    let tz = if tz.starts_with('.') {
        let rest = tz.trim_start_matches(|c: char| c == '.' || c.is_ascii_digit());
        rest.trim()
    } else {
        tz.trim()
    };

    // parse timezone offset e.g. "+0200", "-0530", "+02:00", "Z", ""
    if tz == "Z" || tz.is_empty() {
        // UTC
    } else if tz.len() >= 5 {
        let sign: i64 = if tz.starts_with('-') { -1 } else { 1 };
        let tz_clean = tz[1..].replace(':', "");
        let tz_h: i64 = tz_clean[..2].parse().unwrap_or(0);
        let tz_m: i64 = tz_clean[2..4].parse().unwrap_or(0);
        unix -= sign * (tz_h * 3600 + tz_m * 60);
    }

    unix.max(0) as u64
}

fn days_from_epoch(y: i64, m: i64, d: i64) -> i64 {
    let (y, m) = if m <= 2 { (y - 1, m + 12) } else { (y, m) };
    let a = y / 100;
    let b = 2 - a + a / 4;
    ((365.25 * (y + 4716) as f64) as i64) + ((30.6001 * (m + 1) as f64) as i64) + d + b
        - 1524
        - 2440588
}

fn normalize_path(path: &str) -> String {
    let p = Path::new(path);
    if let Ok(cwd) = std::env::current_dir() {
        if let Ok(rel) = p.strip_prefix(&cwd) {
            return rel.to_string_lossy().to_string();
        }
    }
    p.file_name()
        .and_then(|n| n.to_str())
        .unwrap_or(path)
        .to_string()
}

/// Get added lines per file from git diff.
fn diff_added_lines(repo_path: &Path, sha: &str) -> HashMap<String, Vec<String>> {
    let out = Command::new("git")
        .current_dir(repo_path)
        .args(["diff", &format!("{}^..{}", sha, sha), "-U0"])
        .output();

    let text = match out {
        Ok(o) if o.status.success() => String::from_utf8_lossy(&o.stdout).to_string(),
        _ => return HashMap::new(),
    };

    let mut result: HashMap<String, Vec<String>> = HashMap::new();
    let mut current_file = String::new();

    for line in text.lines() {
        if let Some(stripped) = line.strip_prefix("+++ b/") {
            current_file = stripped.to_string();
        } else if line.starts_with('+') && !line.starts_with("+++") {
            let content = line[1..].trim().to_string();
            if !content.is_empty() {
                result
                    .entry(current_file.clone())
                    .or_default()
                    .push(content);
            }
        }
    }

    result
}

/// Result of attributing a commit against a session.
pub struct Attribution {
    pub ai_attributed: bool,
    pub attribution_pct: Option<f32>,
    /// The raw JSONL lines from the session that fall within this commit's window.
    pub session_slice: Vec<String>,
    /// Duration in seconds of the session window (first to last message timestamp).
    pub session_duration_secs: Option<u64>,
}

/// Attribute a commit to a Claude session.
///
/// 1. Find the active session JSONL in ~/.claude/projects/<project>/
/// 2. Slice it to the window (prev_commit_ts, commit_ts]
/// 3. Match agent Write/Edit lines against git diff lines by file+content
/// 4. Return attribution + the raw JSONL slice for storage
pub fn attribute_commit(
    repo_path: &Path,
    sha: &str,
    commit_ts: u64,
    prev_commit_ts: u64,
) -> Attribution {
    let Some(session_path) = find_active_session(repo_path) else {
        return Attribution {
            ai_attributed: false,
            attribution_pct: None,
            session_slice: vec![],
            session_duration_secs: None,
        };
    };

    let content = match std::fs::read_to_string(&session_path) {
        Ok(c) => c,
        Err(_) => {
            return Attribution {
                ai_attributed: false,
                attribution_pct: None,
                session_slice: vec![],
                session_duration_secs: None,
            }
        }
    };

    // Slice JSONL to the window (prev_commit_ts, commit_ts + 60s grace]
    let mut session_slice: Vec<String> = Vec::new();
    let mut agent_edits: Vec<(String, Vec<String>)> = Vec::new(); // (file, lines)
    let mut first_ts: Option<u64> = None;
    let mut last_ts: Option<u64> = None;

    for raw_line in content.lines() {
        let raw_line = raw_line.trim();
        if raw_line.is_empty() {
            continue;
        }

        let record: serde_json::Value = match serde_json::from_str(raw_line) {
            Ok(v) => v,
            Err(_) => continue,
        };

        let ts = parse_iso_ts(
            record
                .get("timestamp")
                .and_then(|v| v.as_str())
                .unwrap_or(""),
        );

        if ts == 0 || ts <= prev_commit_ts || ts > commit_ts + 60 {
            continue;
        }

        if first_ts.is_none() {
            first_ts = Some(ts);
        }
        last_ts = Some(ts);

        session_slice.push(raw_line.to_string());

        if record.get("type").and_then(|v| v.as_str()) != Some("assistant") {
            continue;
        }

        let blocks = match record.pointer("/message/content") {
            Some(serde_json::Value::Array(b)) => b,
            _ => continue,
        };

        for block in blocks {
            if block.get("type").and_then(|v| v.as_str()) != Some("tool_use") {
                continue;
            }

            let tool = block.get("name").and_then(|v| v.as_str()).unwrap_or("");
            let input = match block.get("input") {
                Some(v) => v,
                None => continue,
            };

            let file = match input.get("file_path").and_then(|v| v.as_str()) {
                Some(f) => normalize_path(f),
                None => continue,
            };

            let written = match tool {
                "Write" => input.get("content").and_then(|v| v.as_str()).unwrap_or(""),
                "Edit" => input
                    .get("new_string")
                    .and_then(|v| v.as_str())
                    .unwrap_or(""),
                _ => continue,
            };

            let lines: Vec<String> = written
                .lines()
                .map(|l| l.trim().to_string())
                .filter(|l| !l.is_empty())
                .collect();

            if !lines.is_empty() {
                agent_edits.push((file, lines));
            }
        }
    }

    let session_duration_secs = match (first_ts, last_ts) {
        (Some(f), Some(l)) if l > f => Some(l - f),
        _ => None,
    };

    if session_slice.is_empty() {
        return Attribution {
            ai_attributed: false,
            attribution_pct: None,
            session_slice: vec![],
            session_duration_secs: None,
        };
    }

    let diff = diff_added_lines(repo_path, sha);
    let total_added: usize = diff.values().map(|v| v.len()).sum();

    if total_added == 0 || agent_edits.is_empty() {
        return Attribution {
            ai_attributed: false,
            attribution_pct: None,
            session_slice,
            session_duration_secs,
        };
    }

    let diff_sets: HashMap<String, std::collections::HashSet<String>> = diff
        .into_iter()
        .map(|(f, lines)| (f, lines.into_iter().collect()))
        .collect();

    let mut matched = 0usize;
    for (file, lines) in &agent_edits {
        if let Some(diff_lines) = diff_sets.get(file) {
            for line in lines {
                if diff_lines.contains(line) {
                    matched += 1;
                }
            }
        }
    }

    if matched == 0 {
        return Attribution {
            ai_attributed: false,
            attribution_pct: None,
            session_slice,
            session_duration_secs,
        };
    }

    let pct = (matched as f32 / total_added as f32).clamp(0.0, 1.0);
    Attribution {
        ai_attributed: pct >= 0.3,
        attribution_pct: Some(pct),
        session_slice,
        session_duration_secs,
    }
}

/// Print the session slice stored for a commit.
pub fn run_show_session(repo_path: &Path, sha: &str) -> Result<()> {
    use crate::cognitive_debt::read_session_slice_from_branch;

    let lines = read_session_slice_from_branch(repo_path, sha)?;
    if lines.is_empty() {
        println!("No session slice stored for {}.", &sha[..8.min(sha.len())]);
        return Ok(());
    }

    println!("\n--- session slice for {} ---\n", &sha[..8.min(sha.len())]);
    for line in &lines {
        let record: serde_json::Value = match serde_json::from_str(line) {
            Ok(v) => v,
            Err(_) => {
                println!("{}", line);
                continue;
            }
        };

        let role = record.get("type").and_then(|v| v.as_str()).unwrap_or("?");
        let ts = record
            .get("timestamp")
            .and_then(|v| v.as_str())
            .unwrap_or("")
            .get(..19)
            .unwrap_or("");

        match role {
            "user" => {
                let text = record
                    .pointer("/message/content")
                    .and_then(|v| match v {
                        serde_json::Value::String(s) => Some(s.clone()),
                        serde_json::Value::Array(arr) => {
                            let parts: Vec<&str> = arr
                                .iter()
                                .filter(|b| b.get("type").and_then(|t| t.as_str()) == Some("text"))
                                .filter_map(|b| b.get("text").and_then(|t| t.as_str()))
                                .collect();
                            if parts.is_empty() {
                                None
                            } else {
                                Some(parts.join(" "))
                            }
                        }
                        _ => None,
                    })
                    .unwrap_or_default();
                if !text.is_empty() {
                    println!("[{}] human: {}", ts, &text[..120.min(text.len())]);
                }
            }
            "assistant" => {
                let blocks = match record.pointer("/message/content") {
                    Some(serde_json::Value::Array(b)) => b,
                    _ => continue,
                };
                for block in blocks {
                    match block.get("type").and_then(|v| v.as_str()) {
                        Some("text") => {
                            let text = block
                                .get("text")
                                .and_then(|v| v.as_str())
                                .unwrap_or("")
                                .trim();
                            if !text.is_empty() {
                                println!("[{}] agent: {}", ts, &text[..120.min(text.len())]);
                            }
                        }
                        Some("tool_use") => {
                            let tool = block.get("name").and_then(|v| v.as_str()).unwrap_or("?");
                            let file = block
                                .pointer("/input/file_path")
                                .and_then(|v| v.as_str())
                                .unwrap_or("");
                            println!("[{}] tool:  {} {}", ts, tool, file);
                        }
                        _ => {}
                    }
                }
            }
            _ => {}
        }
    }
    println!();
    Ok(())
}