whogitit 1.0.0

Track AI-generated code at line-level granularity
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
use std::fs;

use git2::{Repository, Signature};
use tempfile::TempDir;

use whogitit::capture::pending::PendingBuffer;
use whogitit::capture::threeway::ThreeWayAnalyzer;
use whogitit::core::attribution::{AIAttribution, PromptInfo, SessionMetadata};
use whogitit::storage::notes::NotesStore;
use whogitit::storage::trailers::TrailerGenerator;

/// Test the full workflow: capture changes, commit, and analyze with three-way diff
#[test]
fn test_full_workflow() {
    // Create a test repo
    let dir = TempDir::new().unwrap();
    let repo = Repository::init(dir.path()).unwrap();

    // Configure git user
    let mut config = repo.config().unwrap();
    config.set_str("user.name", "Test User").unwrap();
    config.set_str("user.email", "test@example.com").unwrap();

    // Create initial file
    let file_path = dir.path().join("test.rs");
    fs::write(&file_path, "fn main() {}\n").unwrap();

    // Initial commit
    {
        let mut index = repo.index().unwrap();
        index.add_path(std::path::Path::new("test.rs")).unwrap();
        index.write().unwrap();

        let tree_id = index.write_tree().unwrap();
        let tree = repo.find_tree(tree_id).unwrap();
        let sig = Signature::now("Test User", "test@example.com").unwrap();
        repo.commit(Some("HEAD"), &sig, &sig, "Initial commit", &tree, &[])
            .unwrap();
    }

    // Simulate AI-assisted edit using v2 API
    let ai_content = r#"fn main() {
    println!("Hello, AI!");
}
"#;

    // Create pending buffer with v2 API
    let mut buffer = PendingBuffer::new("test-session-123", "claude-opus-4-5-20251101");
    buffer.record_edit(
        "test.rs",
        Some("fn main() {}\n"),
        ai_content,
        "Edit",
        "Add greeting to main function",
        None,
    );

    // Verify pending changes captured
    assert!(buffer.has_changes());
    assert_eq!(buffer.file_count(), 1);

    let history = buffer.get_file_history("test.rs").unwrap();
    assert_eq!(history.edits.len(), 1);
    assert_eq!(history.original.content, "fn main() {}\n");
    assert!(history.edits[0].after.content.contains("println"));

    // Simulate user manually adding a line before committing
    let final_content = r#"// Author: Test User
fn main() {
    println!("Hello, AI!");
}
"#;
    fs::write(&file_path, final_content).unwrap();

    // Perform three-way analysis
    let result = ThreeWayAnalyzer::analyze_with_diff(history, final_content);

    // Verify attribution
    assert_eq!(result.path, "test.rs");

    // Should detect the comment as human-added
    assert!(
        result.summary.human_lines >= 1,
        "Should have at least 1 human line"
    );

    // Should detect some AI lines
    assert!(
        result.summary.ai_lines >= 1,
        "Should have at least 1 AI line"
    );

    // Verify we can identify the human-added comment line
    let first_line = &result.lines[0];
    assert!(first_line.content.contains("Author"));
    assert!(
        first_line.source.is_human(),
        "First line should be human-added"
    );
}

/// Test that privacy redaction works
#[test]
fn test_privacy_redaction() {
    let redactor = whogitit::privacy::Redactor::default_patterns();

    let sensitive = "Use api_key = sk-secret123 for auth with user@email.com";
    let redacted = redactor.redact(sensitive);

    assert!(!redacted.contains("sk-secret123"));
    assert!(!redacted.contains("user@email.com"));
    assert!(redacted.contains("[REDACTED]"));
}

/// Test trailers generation
#[test]
fn test_trailers() {
    use whogitit::capture::snapshot::{AttributionSummary, FileAttributionResult};
    use whogitit::core::attribution::ModelInfo;

    let attribution = AIAttribution {
        version: 2,
        session: SessionMetadata {
            session_id: "abc123".to_string(),
            model: ModelInfo::claude("claude-opus-4-5-20251101"),
            started_at: "2026-01-30T10:00:00Z".to_string(),
            prompt_count: 5,
            used_plan_mode: false,
            subagent_count: 0,
        },
        prompts: vec![],
        files: vec![FileAttributionResult {
            path: "test.rs".to_string(),
            lines: vec![],
            summary: AttributionSummary {
                total_lines: 10,
                ai_lines: 5,
                ai_modified_lines: 2,
                human_lines: 3,
                original_lines: 0,
                unknown_lines: 0,
            },
        }],
    };

    let trailers = TrailerGenerator::generate(&attribution);

    assert!(trailers.iter().any(|(k, _)| k == "AI-Session"));
    assert!(trailers.iter().any(|(k, _)| k == "AI-Model"));
    assert!(trailers
        .iter()
        .any(|(k, v)| k == "Co-Authored-By" && v.contains("Claude")));
}

/// Test multiple AI edits on the same file
#[test]
fn test_multiple_ai_edits() {
    let mut buffer = PendingBuffer::new("test-session", "claude-opus-4-5-20251101");

    // First AI edit
    buffer.record_edit(
        "test.rs",
        Some("original\n"),
        "original\nfirst_ai_line\n",
        "Edit",
        "First prompt: add a line",
        None,
    );

    // Second AI edit
    buffer.record_edit(
        "test.rs",
        None, // Not needed - will use last AI state
        "original\nfirst_ai_line\nsecond_ai_line\n",
        "Edit",
        "Second prompt: add another line",
        None,
    );

    // Verify edit history
    let history = buffer.get_file_history("test.rs").unwrap();
    assert_eq!(history.edits.len(), 2);
    assert_eq!(history.edits[0].prompt_index, 0);
    assert_eq!(history.edits[1].prompt_index, 1);

    // The second edit should reference the first edit's output as its input
    assert_eq!(history.edits[1].before.content, "original\nfirst_ai_line\n");

    // Analyze final content matching AI exactly
    let result = ThreeWayAnalyzer::analyze(history, "original\nfirst_ai_line\nsecond_ai_line\n");

    // "original" exists in BOTH the original file AND AI output → Original (unchanged)
    // "first_ai_line" and "second_ai_line" only exist in AI output → AI
    assert_eq!(result.summary.ai_lines, 2);
    assert_eq!(result.summary.original_lines, 1);

    // Lines are attributed to the last edit that included them
    // Since all 3 lines appear in edit 1's output, they all have prompt_index 1
    let second_ai = result
        .lines
        .iter()
        .find(|l| l.content == "second_ai_line")
        .unwrap();
    assert_eq!(second_ai.prompt_index, Some(1));
}

/// Test detecting human modifications to AI code
#[test]
fn test_human_modifies_ai_code() {
    let mut buffer = PendingBuffer::new("test-session", "claude-opus-4-5-20251101");

    buffer.record_edit(
        "test.rs",
        Some(""),
        "fn hello() {\n    println!(\"hello\");\n}\n",
        "Write",
        "Create hello function",
        None,
    );

    let history = buffer.get_file_history("test.rs").unwrap();

    // Human modifies the println line
    let final_content = "fn hello() {\n    println!(\"hello, world!\");\n}\n";

    let result = ThreeWayAnalyzer::analyze_with_diff(history, final_content);

    // The "fn hello() {" line should still be AI
    let fn_line = result
        .lines
        .iter()
        .find(|l| l.content.contains("fn hello"))
        .unwrap();
    assert!(fn_line.source.is_ai(), "fn line should be AI");

    // The modified println line should be detected as AIModified or Human
    let _println_line = result
        .lines
        .iter()
        .find(|l| l.content.contains("println"))
        .unwrap();
    // It might be detected as AIModified if similar enough, or Human if different enough
    // Either is acceptable - the key is we don't mark it as pure AI
}

/// Test line attribution for new file creation
#[test]
fn test_new_file_attribution() {
    let mut buffer = PendingBuffer::new("test-session", "claude-opus-4-5-20251101");

    buffer.record_edit(
        "new_file.rs",
        None, // New file
        "// New file\nfn new_func() {}\n",
        "Write",
        "Create new file",
        None,
    );

    let history = buffer.get_file_history("new_file.rs").unwrap();
    assert!(history.was_new_file);
    assert!(history.original.content.is_empty());

    // All content was AI-generated
    let result = ThreeWayAnalyzer::analyze(history, "// New file\nfn new_func() {}\n");

    assert_eq!(result.summary.ai_lines, 2);
    assert_eq!(result.summary.human_lines, 0);
    assert_eq!(result.summary.original_lines, 0);
}

/// Test copying attribution between commits
#[test]
fn test_copy_attribution() {
    use whogitit::capture::snapshot::{
        AttributionSummary, FileAttributionResult, LineAttribution, LineSource,
    };
    use whogitit::core::attribution::ModelInfo;

    let dir = TempDir::new().unwrap();
    let repo = Repository::init(dir.path()).unwrap();

    // Configure git user
    let mut config = repo.config().unwrap();
    config.set_str("user.name", "Test User").unwrap();
    config.set_str("user.email", "test@example.com").unwrap();

    // Create first commit
    let sig = Signature::now("Test", "test@test.com").unwrap();
    let tree_id = repo.index().unwrap().write_tree().unwrap();
    let tree = repo.find_tree(tree_id).unwrap();
    let first_commit = repo
        .commit(Some("HEAD"), &sig, &sig, "First commit", &tree, &[])
        .unwrap();

    // Create second commit
    fs::write(dir.path().join("file.txt"), "content").unwrap();
    let mut index = repo.index().unwrap();
    index.add_path(std::path::Path::new("file.txt")).unwrap();
    index.write().unwrap();
    let tree_id = index.write_tree().unwrap();
    let tree = repo.find_tree(tree_id).unwrap();
    let second_commit = repo
        .commit(
            Some("HEAD"),
            &sig,
            &sig,
            "Second commit",
            &tree,
            &[&repo.find_commit(first_commit).unwrap()],
        )
        .unwrap();

    let store = NotesStore::new(&repo).unwrap();

    // Add attribution to first commit
    let attribution = AIAttribution {
        version: 2,
        session: SessionMetadata {
            session_id: "copy-test-session".to_string(),
            model: ModelInfo::claude("claude-opus-4-5-20251101"),
            started_at: "2026-01-30T10:00:00Z".to_string(),
            prompt_count: 1,
            used_plan_mode: false,
            subagent_count: 0,
        },
        prompts: vec![PromptInfo {
            index: 0,
            text: "Test copy functionality".to_string(),
            timestamp: "2026-01-30T10:00:00Z".to_string(),
            affected_files: vec!["test.rs".to_string()],
        }],
        files: vec![FileAttributionResult {
            path: "test.rs".to_string(),
            lines: vec![LineAttribution {
                line_number: 1,
                content: "fn test() {}".to_string(),
                source: LineSource::AI {
                    edit_id: "e1".to_string(),
                },
                edit_id: Some("e1".to_string()),
                prompt_index: Some(0),
                confidence: 1.0,
            }],
            summary: AttributionSummary {
                total_lines: 1,
                ai_lines: 1,
                ai_modified_lines: 0,
                human_lines: 0,
                original_lines: 0,
                unknown_lines: 0,
            },
        }],
    };

    store.store_attribution(first_commit, &attribution).unwrap();

    // Verify first has attribution, second doesn't
    assert!(store.has_attribution(first_commit));
    assert!(!store.has_attribution(second_commit));

    // Copy attribution
    store.copy_attribution(first_commit, second_commit).unwrap();

    // Verify both now have attribution
    assert!(store.has_attribution(first_commit));
    assert!(store.has_attribution(second_commit));

    // Verify content matches
    let original = store.fetch_attribution(first_commit).unwrap().unwrap();
    let copied = store.fetch_attribution(second_commit).unwrap().unwrap();
    assert_eq!(original.session.session_id, copied.session.session_id);
    assert_eq!(original.prompts.len(), copied.prompts.len());
    assert_eq!(original.files.len(), copied.files.len());
}

/// Test storing and fetching attribution from git notes
#[test]
fn test_notes_roundtrip() {
    use whogitit::capture::snapshot::{
        AttributionSummary, FileAttributionResult, LineAttribution, LineSource,
    };
    use whogitit::core::attribution::ModelInfo;

    let dir = TempDir::new().unwrap();
    let repo = Repository::init(dir.path()).unwrap();

    // Create initial commit
    {
        let sig = Signature::now("Test", "test@test.com").unwrap();
        let tree_id = repo.index().unwrap().write_tree().unwrap();
        let tree = repo.find_tree(tree_id).unwrap();
        repo.commit(Some("HEAD"), &sig, &sig, "Initial", &tree, &[])
            .unwrap();
    }

    let head = repo.head().unwrap().peel_to_commit().unwrap();
    let store = NotesStore::new(&repo).unwrap();

    let attribution = AIAttribution {
        version: 2,
        session: SessionMetadata {
            session_id: "test-session".to_string(),
            model: ModelInfo::claude("claude-opus-4-5-20251101"),
            started_at: "2026-01-30T10:00:00Z".to_string(),
            prompt_count: 1,
            used_plan_mode: false,
            subagent_count: 0,
        },
        prompts: vec![PromptInfo {
            index: 0,
            text: "Create test function".to_string(),
            timestamp: "2026-01-30T10:00:00Z".to_string(),
            affected_files: vec!["test.rs".to_string()],
        }],
        files: vec![FileAttributionResult {
            path: "test.rs".to_string(),
            lines: vec![LineAttribution {
                line_number: 1,
                content: "fn test() {}".to_string(),
                source: LineSource::AI {
                    edit_id: "e1".to_string(),
                },
                edit_id: Some("e1".to_string()),
                prompt_index: Some(0),
                confidence: 1.0,
            }],
            summary: AttributionSummary {
                total_lines: 1,
                ai_lines: 1,
                ai_modified_lines: 0,
                human_lines: 0,
                original_lines: 0,
                unknown_lines: 0,
            },
        }],
    };

    // Store
    store.store_attribution(head.id(), &attribution).unwrap();

    // Fetch and verify
    let fetched = store.fetch_attribution(head.id()).unwrap().unwrap();
    assert_eq!(fetched.version, 2);
    assert_eq!(fetched.session.session_id, "test-session");
    assert_eq!(fetched.files.len(), 1);
    assert_eq!(fetched.prompts.len(), 1);
    assert_eq!(fetched.prompts[0].text, "Create test function");
}