agentdiff 0.1.30

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
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
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
use anyhow::{Context, Result};
use chrono::{DateTime, Utc};
use colored::Colorize;
use std::collections::HashMap;
use std::process::Command;

use crate::cli::StatusArgs;
use crate::data::AgentTrace;
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.oneline {
        return run_oneline(store);
    }
    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(())
}

/// One-line attribution summary for HEAD, printed by the post-commit hook.
/// Stays silent when the commit has no AI-authored trace (honest human-only
/// commits print nothing). Best-effort — never errors out the commit.
fn run_oneline(store: &Store) -> Result<()> {
    let head = {
        let out = Command::new("git")
            .args(["rev-parse", "HEAD"])
            .current_dir(&store.repo_root)
            .output();
        match out {
            Ok(o) if o.status.success() => String::from_utf8_lossy(&o.stdout).trim().to_string(),
            _ => return Ok(()),
        }
    };
    if head.is_empty() {
        return Ok(());
    }

    let traces = store.find_traces_by_sha(&head).unwrap_or_default();
    // A trace counts as AI-authored only if it has at least one conversation
    // contributed by an AI. Human commits write a trace whose tool.name is the
    // git author and whose contributor.type is "human" — those stay quiet.
    let is_ai = |t: &&AgentTrace| {
        t.files
            .iter()
            .flat_map(|f| f.conversations.iter())
            .any(|c| c.contributor.contributor_type == "ai")
    };
    let ai: Vec<&AgentTrace> = traces.iter().filter(is_ai).collect();
    if ai.is_empty() {
        return Ok(());
    }

    // Only count files that carry AI authorship in this commit.
    let files: std::collections::BTreeSet<&str> = ai
        .iter()
        .flat_map(|t| {
            t.files
                .iter()
                .filter(|f| {
                    f.conversations
                        .iter()
                        .any(|c| c.contributor.contributor_type == "ai")
                })
                .map(|f| f.path.as_str())
        })
        .collect();
    let agents: Vec<&str> = {
        let set: std::collections::BTreeSet<&str> = ai.iter().map(|t| t.agent_name()).collect();
        set.into_iter().collect()
    };
    let signed = ai.iter().all(|t| t.sig.is_some());

    let sig_label = if signed {
        "signed".green()
    } else {
        "unsigned".yellow()
    };
    println!(
        "  {} {} {} file{} · {} · {}",
        "agentdiff".bold().cyan(),
        "".green(),
        files.len(),
        if files.len() == 1 { "" } else { "s" },
        agents.join(", "),
        sig_label
    );
    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: "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 content = std::fs::read_to_string(&path).unwrap_or_default();
        let registered = content.contains(check.marker);
        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;
        }
    }

    // Codex: the capture hook lives in ~/.codex/hooks.json; config.toml only
    // carries the features.codex_hooks=true flag. (The legacy `notify` key is
    // intentionally removed to avoid double-capture, so it is NOT a marker.)
    // Only surface codex when it is actually installed on this machine.
    {
        let codex_dir = home.join(".codex");
        if codex_dir.exists() {
            any_checked = true;
            let hook_ok = std::fs::read_to_string(codex_dir.join("hooks.json"))
                .map(|s| s.contains("capture-codex"))
                .unwrap_or(false);
            let flag_ok = std::fs::read_to_string(codex_dir.join("config.toml"))
                .ok()
                .and_then(|s| s.parse::<toml::Value>().ok())
                .and_then(|v| v.get("features")?.get("codex_hooks")?.as_bool())
                .unwrap_or(false);
            if hook_ok && flag_ok {
                println!("  {} agent hook     codex registered", prefix(ok()));
            } else {
                println!(
                    "  {} agent hook     codex installed but agentdiff hook missing — re-run 'agentdiff configure'",
                    prefix(warn())
                );
                any_missing = true;
            }
        }
    }

    // Gemini CLI + Antigravity: fully opt-in. Only confirm what agentdiff has
    // actually registered — never nag about the *other* half being missing.
    // Gemini is configured only when explicitly requested (--all / --agents
    // gemini), so an untouched or half-configured ~/.gemini stays quiet rather
    // than emitting "hooks missing" noise on every status.
    {
        let gemini_dir = home.join(".gemini");
        if gemini_dir.exists() {
            let settings_raw =
                std::fs::read_to_string(gemini_dir.join("settings.json")).unwrap_or_default();
            // Require both the hook entry AND tools.enableHooks=true — without the
            // flag Gemini silently ignores the hooks.
            let cli_ok = settings_raw.contains("capture-antigravity")
                && serde_json::from_str::<serde_json::Value>(&settings_raw)
                    .ok()
                    .and_then(|v| v.get("tools")?.get("enableHooks")?.as_bool())
                    .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);
            if cli_ok || rule_ok {
                any_checked = true;
                let label = match (cli_ok, rule_ok) {
                    (true, true) => "gemini-cli registered; antigravity rule set",
                    (true, false) => "gemini-cli registered",
                    (false, true) => "antigravity rule set",
                    (false, false) => unreachable!(),
                };
                println!("  {} agent hook     {label}", prefix(ok()));
            }
        }
    }

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

    let since_cutoff: Option<DateTime<Utc>> = match &args.since {
        None => None,
        Some(s) => Some(activity_cutoff_from_since(Utc::now(), s)?),
    };

    print_remote_developer_health(store, &remote_refs, since_cutoff);

    println!();
    Ok(())
}

/// Oldest UTC instant inside the `--since` window (`7`, `7d`, `48h`):
/// entries with activity at or after this instant pass the filter.
fn activity_cutoff_from_since(now: DateTime<Utc>, s: &str) -> Result<DateTime<Utc>> {
    let s = s.trim().to_ascii_lowercase();
    anyhow::ensure!(!s.is_empty(), "--since must not be empty");
    let duration = if let Some(rest) = s.strip_suffix('d') {
        let n: i64 = rest
            .trim()
            .parse()
            .context("invalid day count in --since")?;
        chrono::Duration::days(n)
    } else if let Some(rest) = s.strip_suffix('h') {
        let n: i64 = rest
            .trim()
            .parse()
            .context("invalid hour count in --since")?;
        chrono::Duration::hours(n)
    } else {
        let n: i64 = s
            .parse()
            .context("invalid --since (expected N, Nd, or Nh)")?;
        chrono::Duration::days(n)
    };
    anyhow::ensure!(
        duration > chrono::Duration::zero(),
        "--since must be positive"
    );
    Ok(now - duration)
}

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 load_traces_from_ref(repo_root: &std::path::Path, ref_name: &str) -> Vec<AgentTrace> {
    let spec = format!("{ref_name}:traces.jsonl");
    let mut raw = Command::new("git")
        .args(["show", &spec])
        .current_dir(repo_root)
        .output()
        .ok()
        .filter(|o| o.status.success())
        .map(|o| String::from_utf8_lossy(&o.stdout).to_string())
        .unwrap_or_default();

    if raw.lines().all(|l| l.trim().is_empty()) {
        if let Ok(Some(api_raw)) =
            store::fetch_ref_content_via_api(repo_root, ref_name, "traces.jsonl")
        {
            raw = api_raw;
        }
    }

    store::parse_traces_from_jsonl(&raw)
}

fn print_remote_developer_health(
    store: &Store,
    remote_refs: &[(String, String)],
    since_cutoff: Option<DateTime<Utc>>,
) {
    const STALE: chrono::Duration = chrono::Duration::days(7);

    let trace_refs: Vec<_> = remote_refs
        .iter()
        .filter(|(_, r)| r.starts_with("refs/agentdiff/traces/"))
        .collect();
    if trace_refs.is_empty() {
        return;
    }

    let now = Utc::now();

    let mut dev_map: HashMap<String, (usize, DateTime<Utc>)> = HashMap::new();
    let mut per_ref: Vec<(String, usize, Option<DateTime<Utc>>)> = Vec::new();

    for (_, ref_name) in &trace_refs {
        let traces = load_traces_from_ref(&store.repo_root, ref_name);
        let mut last: Option<DateTime<Utc>> = None;
        for t in &traces {
            let ts = t.timestamp;
            last = Some(match last {
                None => ts,
                Some(p) => p.max(ts),
            });
            let author = t
                .agentdiff_metadata()
                .and_then(|m| m.author)
                .unwrap_or_else(|| "unknown".to_string());
            let entry = dev_map.entry(author).or_insert((0, ts));
            entry.0 += 1;
            if ts > entry.1 {
                entry.1 = ts;
            }
        }
        per_ref.push(((*ref_name).clone(), traces.len(), last));
    }

    println!();
    println!("{}", "  REMOTE TRACE BRANCHES".dimmed());
    let bhdr = format!("  {:<42} {:<8} {}", "REF (truncated)", "TRACES", "LAST TRACE");
    println!("{}", bhdr.dimmed());
    println!("  {}", "".repeat(76).dimmed());

    per_ref.sort_by(|a, b| match (a.2, b.2) {
        (Some(ta), Some(tb)) => tb.cmp(&ta).then_with(|| a.0.cmp(&b.0)),
        (Some(_), None) => std::cmp::Ordering::Less,
        (None, Some(_)) => std::cmp::Ordering::Greater,
        (None, None) => a.0.cmp(&b.0),
    });

    for (ref_name, count, last_ts) in &per_ref {
        let short_ref: std::borrow::Cow<'_, str> = if ref_name.len() > 42 {
            ref_name[..42].into()
        } else {
            ref_name.as_str().into()
        };
        let (last_str, status_px) = match last_ts {
            None => ("(no traces)".to_string(), prefix(warn())),
            Some(ts) => {
                let age = now.signed_duration_since(*ts);
                let s = if age.num_days() > 0 {
                    format!("{}d ago", age.num_days())
                } else if age.num_hours() > 0 {
                    format!("{}h ago", age.num_hours())
                } else {
                    "just now".to_string()
                };
                let px = if age > STALE {
                    prefix(warn())
                } else {
                    prefix(ok())
                };
                (s, px)
            }
        };
        println!(
            "  {} {:<42} {:<8} {}",
            status_px,
            short_ref,
            count,
            last_str
        );
    }

    if dev_map.is_empty() {
        return;
    }

    let mut devs: Vec<_> = dev_map.iter().collect();
    devs.sort_by(|a, b| b.1.1.cmp(&a.1.1));

    let devs: Vec<_> = devs
        .into_iter()
        .filter(|(_, (_, last_seen))| {
            since_cutoff.map_or(true, |cutoff| *last_seen >= cutoff)
        })
        .collect();

    if devs.is_empty() {
        return;
    }

    println!();
    println!("{}", "  DEVELOPERS (from remote traces)".dimmed());
    let hdr = format!("  {:<32} {:<10} {}", "DEVELOPER", "TRACES", "LAST ACTIVE");
    println!("{}", hdr.dimmed());
    println!("  {}", "".repeat(60).dimmed());

    for (author, (count, last_seen)) in devs {
        let age = now.signed_duration_since(*last_seen);
        let age_str = if age.num_days() > 0 {
            format!("{}d ago", age.num_days())
        } else if age.num_hours() > 0 {
            format!("{}h ago", age.num_hours())
        } else {
            "just now".to_string()
        };
        let status_prefix = if age.num_days() > 7 {
            prefix(warn())
        } else {
            prefix(ok())
        };
        println!(
            "  {} {:<32} {:<10} {}",
            status_prefix,
            author,
            count,
            age_str
        );
    }
}

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