agentdiff 0.1.25

Audit and trace autonomous AI code contributions in git repositories
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
use anyhow::Result;
use colored::Colorize;
use std::process::Command;

use crate::cli::StatusArgs;
use crate::keys;
use crate::store::{self, Store};
use crate::util::{dim, ok, print_command_header, warn};

pub fn run(store: &Store, args: &StatusArgs) -> Result<()> {
    if args.remote {
        return run_remote(store, args);
    }

    print_command_header("status");

    print_init_status(store);
    print_keys_status();
    print_traces_status(store)?;
    print_hook_status(store);
    print_agent_hook_status();
    print_unpushed_status(store);

    Ok(())
}

/// Renders a status prefix padded to a fixed width so columns line up.
/// Widths: ok=2, warn=4, error=5 → pad everything to 5 chars.
fn prefix(label: colored::ColoredString) -> String {
    let visible_len = strip_ansi(&label.to_string()).chars().count();
    let pad = 5usize.saturating_sub(visible_len);
    format!("{}{}", label, " ".repeat(pad))
}

fn strip_ansi(s: &str) -> String {
    // cheap ANSI escape stripper for length calculation
    let mut out = String::with_capacity(s.len());
    let mut in_esc = false;
    for c in s.chars() {
        if in_esc {
            if c == 'm' {
                in_esc = false;
            }
        } else if c == '\u{1b}' {
            in_esc = true;
        } else {
            out.push(c);
        }
    }
    out
}

fn print_init_status(store: &Store) {
    if store.is_initialized() {
        println!(
            "  {} repo             initialized (.git/agentdiff/ exists)",
            prefix(ok())
        );
    } else {
        println!(
            "  {} repo             not initialized — run {} to start tracking",
            prefix(warn()),
            "agentdiff init".cyan()
        );
    }
}

fn print_keys_status() {
    let priv_path = match keys::private_key_path() {
        Ok(p) => p,
        Err(_) => {
            println!("  {} signing keys  cannot resolve home dir", prefix(warn()));
            return;
        }
    };
    let pub_path = match keys::public_key_path() {
        Ok(p) => p,
        Err(_) => return,
    };

    let priv_ok = priv_path.exists();
    let pub_ok = pub_path.exists();

    #[cfg(unix)]
    let perms_ok = {
        use std::os::unix::fs::MetadataExt;
        priv_path
            .metadata()
            .map(|m| m.mode() & 0o077 == 0)
            .unwrap_or(false)
    };
    #[cfg(not(unix))]
    let perms_ok = true;

    if priv_ok && pub_ok {
        let key_id = keys::load_verifying_key()
            .map(|vk| keys::compute_key_id(&vk))
            .unwrap_or_else(|_| "error reading".to_string());
        let perm_label = if perms_ok {
            "chmod 600 ok"
        } else {
            "chmod 600 MISSING"
        };
        let perm_color = if perms_ok {
            perm_label.green().to_string()
        } else {
            perm_label.red().to_string()
        };
        println!(
            "  {} signing keys  key_id={} ({})",
            prefix(ok()),
            key_id,
            perm_color
        );
    } else if !priv_ok && !pub_ok {
        println!(
            "  {} signing keys  not initialized — run '{}'",
            prefix(warn()),
            "agentdiff keys init".cyan()
        );
    } else {
        println!(
            "  {} signing keys  partial — private={} public={}",
            prefix(warn()),
            if priv_ok { "ok" } else { "missing" },
            if pub_ok { "ok" } else { "missing" }
        );
    }
}

fn print_traces_status(store: &Store) -> Result<()> {
    let traces = store.load_meta_traces()?;

    if traces.is_empty() {
        println!(
            "  {} traces         none in refs/agentdiff/meta",
            prefix(dim())
        );
        return Ok(());
    }

    let total = traces.len();
    let signed = traces.iter().filter(|t| t.sig.is_some()).count();
    let last = traces.last().unwrap();
    let last_ts = last.timestamp.format("%Y-%m-%d %H:%M:%SZ");
    let last_id = &last.id[..last.id.len().min(8)];

    println!(
        "  {} traces         {} entries ({}/{} signed), last: {} ({})",
        prefix(ok()),
        total,
        signed,
        total,
        last_id,
        last_ts
    );

    Ok(())
}

fn print_hook_status(store: &Store) {
    let hooks_dir = store.repo_root.join(".git").join("hooks");
    let pre = hooks_dir.join("pre-commit");
    let post = hooks_dir.join("post-commit");
    let pre_push = hooks_dir.join("pre-push");

    let pre_ok = pre.exists()
        && std::fs::read_to_string(&pre)
            .map(|s| s.contains("agentdiff"))
            .unwrap_or(false);
    let post_ok = post.exists()
        && std::fs::read_to_string(&post)
            .map(|s| s.contains("agentdiff"))
            .unwrap_or(false);
    let pre_push_ok = pre_push.exists()
        && std::fs::read_to_string(&pre_push)
            .map(|s| s.contains("agentdiff"))
            .unwrap_or(false);

    if pre_ok && post_ok && pre_push_ok {
        println!(
            "  {} git hooks      pre-commit + post-commit + pre-push installed",
            prefix(ok())
        );
    } else {
        println!(
            "  {} git hooks      pre-commit={} post-commit={} pre-push={} — run '{}'",
            prefix(warn()),
            if pre_ok { "ok" } else { "missing" },
            if post_ok { "ok" } else { "missing" },
            if pre_push_ok { "ok" } else { "missing" },
            "agentdiff init".cyan()
        );
    }
}

fn print_agent_hook_status() {
    let home = match dirs::home_dir() {
        Some(h) => h,
        None => return,
    };

    struct AgentCheck {
        name: &'static str,
        config_path_parts: &'static [&'static str],
        marker: &'static str,
    }

    let checks = [
        AgentCheck {
            name: "claude-code",
            config_path_parts: &[".claude", "settings.json"],
            marker: "capture-claude",
        },
        AgentCheck {
            name: "cursor",
            config_path_parts: &[".cursor", "hooks.json"],
            marker: "capture-cursor",
        },
        AgentCheck {
            name: "codex",
            config_path_parts: &[".codex", "config.toml"],
            marker: "capture-codex",
        },
        AgentCheck {
            name: "windsurf",
            config_path_parts: &[".codeium", "windsurf", "hooks.json"],
            marker: "capture-windsurf",
        },
    ];

    // OpenCode uses dirs::config_dir() (platform-aware: ~/Library/Application Support on macOS).
    let opencode_path = dirs::config_dir().map(|d| d.join("opencode").join("plugins").join("agentdiff.ts"));

    let mut any_checked = false;
    let mut any_missing = false;

    // Check the struct-based agents.
    for check in &checks {
        let path = check
            .config_path_parts
            .iter()
            .fold(home.clone(), |p, part| p.join(part));
        if !path.exists() {
            continue;
        }
        any_checked = true;
        let registered = std::fs::read_to_string(&path)
            .map(|s| s.contains(check.marker))
            .unwrap_or(false);
        if registered {
            println!("  {} agent hook     {} registered", prefix(ok()), check.name);
        } else {
            println!(
                "  {} agent hook     {} config found but agentdiff hook missing — re-run 'agentdiff configure'",
                prefix(warn()),
                check.name
            );
            any_missing = true;
        }
    }

    // Gemini CLI + Antigravity: two separate products sharing ~/.gemini/
    {
        let gemini_dir = home.join(".gemini");
        if gemini_dir.exists() {
            any_checked = true;
            let cli_ok = std::fs::read_to_string(gemini_dir.join("settings.json"))
                .map(|s| s.contains("capture-antigravity"))
                .unwrap_or(false);
            let rule_ok = std::fs::read_to_string(gemini_dir.join("GEMINI.md"))
                .map(|s| s.contains("agentdiff: managed block"))
                .unwrap_or(false);
            match (cli_ok, rule_ok) {
                (true, true) => {
                    println!("  {} agent hook     gemini-cli registered; antigravity rule set", prefix(ok()));
                }
                (true, false) => {
                    println!("  {} agent hook     gemini-cli registered", prefix(ok()));
                    println!(
                        "  {} agent hook     antigravity GEMINI.md rule missing — re-run 'agentdiff configure'",
                        prefix(warn())
                    );
                    any_missing = true;
                }
                (false, true) => {
                    println!(
                        "  {} agent hook     gemini-cli hooks missing — re-run 'agentdiff configure'",
                        prefix(warn())
                    );
                    println!("  {} agent hook     antigravity rule set", prefix(ok()));
                    any_missing = true;
                }
                (false, false) => {
                    println!(
                        "  {} agent hook     gemini/antigravity config found but hooks missing — re-run 'agentdiff configure'",
                        prefix(warn())
                    );
                    any_missing = true;
                }
            }
        }
    }

    // OpenCode: platform-aware path via dirs::config_dir()
    if let Some(ref ocp) = opencode_path {
        if ocp.exists() {
            any_checked = true;
            let registered = std::fs::read_to_string(ocp)
                .map(|s| s.contains("agentdiff"))
                .unwrap_or(false);
            if registered {
                println!("  {} agent hook     opencode registered", prefix(ok()));
            } else {
                println!(
                    "  {} agent hook     opencode plugin found but agentdiff missing — re-run 'agentdiff configure'",
                    prefix(warn())
                );
                any_missing = true;
            }
        }
    }

    // Copilot: check for agentdiff extension directory in VS Code extensions paths.
    // Check all dirs — the extension may be installed in only one (e.g., vscode-server on remote).
    let mut copilot_found = false;
    let mut copilot_checked = false;
    for vscode_dir in &[".vscode/extensions", ".vscode-server/extensions", ".vscode-insiders/extensions"] {
        let ext_dir = home.join(vscode_dir);
        if !ext_dir.exists() {
            continue;
        }
        copilot_checked = true;
        any_checked = true;
        if std::fs::read_dir(&ext_dir)
            .map(|d| d.filter_map(|e| e.ok()).any(|e| e.file_name().to_string_lossy().starts_with("agentdiff-copilot")))
            .unwrap_or(false)
        {
            copilot_found = true;
        }
    }
    if copilot_checked {
        if copilot_found {
            println!("  {} agent hook     copilot registered", prefix(ok()));
        } else {
            println!(
                "  {} agent hook     copilot extension not found — re-run 'agentdiff configure'",
                prefix(warn())
            );
            any_missing = true;
        }
    }

    if !any_checked {
        println!(
            "  {} agent hooks    no AI agent configs found — run 'agentdiff configure'",
            prefix(dim())
        );
    } else if any_missing {
        println!(
            "  {}",
            "Re-run 'agentdiff configure' to restore missing hooks.".yellow()
        );
    }
}

fn print_unpushed_status(store: &Store) {
    let branch = match store.current_branch() {
        Ok(b) => b,
        Err(_) => return,
    };

    let local_path = store.local_traces_path(&branch);
    if !local_path.exists() {
        return;
    }

    if let Ok(raw) = std::fs::read_to_string(&local_path) {
        let count = raw.lines().filter(|l| !l.trim().is_empty()).count();
        if count > 0 {
            println!(
                "  {} unpushed       {} trace(s) for branch '{}' — run '{}'",
                prefix(warn()),
                count,
                branch,
                "agentdiff push".cyan()
            );
        }
    }
}

// ── Remote status (--remote) ────────────────────────────────────────────────

fn run_remote(store: &Store, args: &StatusArgs) -> Result<()> {
    let ls_out = Command::new("git")
        .args(["ls-remote", "origin", "refs/agentdiff/*"])
        .current_dir(&store.repo_root)
        .output();

    let remote_refs: Vec<(String, String)> = match ls_out {
        Ok(out) if out.status.success() => String::from_utf8_lossy(&out.stdout)
            .lines()
            .filter_map(|line| {
                let mut parts = line.splitn(2, '\t');
                let sha = parts.next()?.trim().to_string();
                let refname = parts.next()?.trim().to_string();
                Some((sha, refname))
            })
            .collect(),
        Ok(_) => Vec::new(),
        Err(e) => anyhow::bail!("git ls-remote failed: {e}"),
    };

    let remote_label = Command::new("git")
        .args(["remote", "get-url", "origin"])
        .current_dir(&store.repo_root)
        .output()
        .ok()
        .and_then(|o| {
            o.status
                .success()
                .then(|| String::from_utf8_lossy(&o.stdout).trim().to_string())
        })
        .unwrap_or_else(|| "origin".to_string());

    print_command_header("status --remote");
    println!("  {}", remote_label.dimmed());
    println!();

    if remote_refs.is_empty() {
        println!("  {} no agentdiff refs found on remote", dim());
        println!();
        println!("  Push local traces with: {}", "agentdiff push".cyan());
        println!();
        return Ok(());
    }

    let hdr = format!("  {:<45} {:<10} {}", "REF", "TRACES", "LOCAL");
    println!("{}", hdr.dimmed());
    println!("  {}", "".repeat(72).dimmed());

    for (sha, refname) in &remote_refs {
        let short_sha = if sha.len() >= 8 { &sha[..8] } else { sha };

        let trace_count = if !args.no_fetch {
            fetch_trace_count(&store.repo_root, refname)
        } else {
            None
        };

        let count_str = match trace_count {
            Some(n) => format!("{n}"),
            None => short_sha.to_string(),
        };

        let local_str = local_ref_status(store, refname);

        println!(
            "  {:<45} {:<10} {}",
            refname.cyan(),
            count_str,
            local_str.dimmed()
        );
    }

    if let Ok(branch) = store.current_branch() {
        let local_path = store.local_traces_path(&branch);
        if local_path.exists() {
            let local_traces = store.load_local_traces(&branch).unwrap_or_default();
            let branch_ref = store::branch_ref_name(&branch);
            let on_remote = remote_refs.iter().any(|(_, r)| r == &branch_ref);
            if !on_remote && !local_traces.is_empty() {
                println!();
                println!(
                    "  {} {} local trace(s) for '{}' not yet pushed — run: {}",
                    warn(),
                    local_traces.len(),
                    branch,
                    "agentdiff push".cyan()
                );
            }
        }
    }

    println!();
    Ok(())
}

fn fetch_trace_count(repo_root: &std::path::Path, ref_name: &str) -> Option<usize> {
    if let Some(n) = count_local_ref(repo_root, ref_name) {
        return Some(n);
    }
    store::fetch_ref_content_via_api(repo_root, ref_name, "traces.jsonl")
        .ok()
        .flatten()
        .map(|content| content.lines().filter(|l| !l.trim().is_empty()).count())
}

fn count_local_ref(repo_root: &std::path::Path, ref_name: &str) -> Option<usize> {
    let spec = format!("{ref_name}:traces.jsonl");
    let out = Command::new("git")
        .args(["show", &spec])
        .current_dir(repo_root)
        .output()
        .ok()?;
    if !out.status.success() {
        return None;
    }
    let content = String::from_utf8_lossy(&out.stdout);
    Some(content.lines().filter(|l| !l.trim().is_empty()).count())
}

fn local_ref_status(store: &Store, ref_name: &str) -> String {
    let local_sha = Command::new("git")
        .args(["rev-parse", ref_name])
        .current_dir(&store.repo_root)
        .output()
        .ok()
        .and_then(|o| {
            o.status
                .success()
                .then(|| String::from_utf8_lossy(&o.stdout).trim().to_string())
        });

    match local_sha {
        Some(_) => "synced".to_string(),
        None => {
            if ref_name.starts_with("refs/agentdiff/traces/") {
                let branch_part = ref_name.trim_start_matches("refs/agentdiff/traces/");
                let local_path = store.local_traces_path(branch_part);
                if local_path.exists() {
                    return "local buffer only (run: agentdiff push)".to_string();
                }
            }
            "not fetched locally".to_string()
        }
    }
}