agentdiff 0.1.13

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
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
use crate::config::{Config, RepoConfig};
use anyhow::{Context, Result, bail};
use colored::Colorize;
use std::{fs, path::Path, process::Command};

const CLAUDE_CAPTURE_SCRIPT: &str = include_str!("../scripts/capture-claude.py");
const CURSOR_CAPTURE_SCRIPT: &str = include_str!("../scripts/capture-cursor.py");
const CODEX_CAPTURE_SCRIPT: &str = include_str!("../scripts/capture-codex.py");
const WINDSURF_CAPTURE_SCRIPT: &str = include_str!("../scripts/capture-windsurf.py");
const OPENCODE_CAPTURE_SCRIPT: &str = include_str!("../scripts/capture-opencode.py");
const OPENCODE_PLUGIN_TEMPLATE: &str = include_str!("../scripts/opencode-agentdiff.ts");
const ANTIGRAVITY_CAPTURE_SCRIPT: &str = include_str!("../scripts/capture-antigravity.py");
const COPILOT_CAPTURE_SCRIPT: &str = include_str!("../scripts/capture-copilot.py");
const COPILOT_EXT_PACKAGE_JSON: &str =
    include_str!("../scripts/vscode-extension/package.json");
const COPILOT_EXT_JS_TEMPLATE: &str =
    include_str!("../scripts/vscode-extension/extension.js");
const PREPARE_LEDGER_SCRIPT: &str = include_str!("../scripts/prepare-ledger.py");
const FINALIZE_LEDGER_SCRIPT: &str = include_str!("../scripts/finalize-ledger.py");
const RECORD_CONTEXT_SCRIPT: &str = include_str!("../scripts/record-context.py");
const WRITE_NOTE_SCRIPT: &str = include_str!("../scripts/write-note.py");

/// Configure global agent hooks — run once per machine, no git repo required.
pub fn run_configure(
    config: &mut Config,
    no_claude: bool,
    no_cursor: bool,
    no_codex: bool,
    no_antigravity: bool,
    no_windsurf: bool,
    no_opencode: bool,
    no_copilot: bool,
) -> Result<()> {
    println!("{}", "agentdiff configure".bold().cyan());
    println!();

    // Check Python 3 availability — capture scripts require it.
    check_python3()?;

    // Step 1 — create global dirs
    step_create_dirs(config)?;

    // Step 2 — install Python scripts into ~/.agentdiff/scripts/
    step_install_scripts(config)?;

    // Step 3 — configure Claude Code ~/.claude/settings.json
    if !no_claude {
        step_configure_claude(config)?;
    }

    // Step 4 — configure Cursor ~/.cursor/hooks.json
    if !no_cursor {
        step_configure_cursor(config)?;
    }

    // Step 5 — configure Codex ~/.codex/config.toml notify hook
    if !no_codex {
        step_configure_codex(config)?;
    }

    // Step 6 — configure Gemini / Antigravity hooks
    if !no_antigravity {
        step_configure_antigravity(config)?;
    }

    // Step 7 — configure Windsurf globally (~/.codeium/windsurf/hooks.json)
    if !no_windsurf {
        step_configure_windsurf(config)?;
    }

    // Step 8 — configure OpenCode globally (~/.config/opencode/plugins/)
    if !no_opencode {
        step_configure_opencode(config)?;
    }

    // Step 9 — install VS Code Copilot extension
    if !no_copilot {
        step_configure_copilot(config)?;
    }

    // Save updated config
    config.save()?;
    println!(
        "{} Config written to {}",
        "ok".green(),
        Config::config_path().display()
    );

    println!();
    println!("{}", "agentdiff configure complete.".bold().green());
    println!(
        "{}",
        "Run 'agentdiff init' inside each repo you want to track.".dimmed()
    );
    Ok(())
}

/// Initialize agentdiff in this repository — installs git hooks and creates the ledger.
/// Run `agentdiff configure` first to set up global agent hooks.
pub fn run_init(
    repo_root: &Path,
    config: &mut Config,
    no_git_hook: bool,
    migrate: bool,
) -> Result<()> {
    println!("{}", "agentdiff init".bold().cyan());
    println!("Repo: {}", repo_root.display());
    println!();

    // Warn if configure hasn't been run yet (scripts dir empty or missing).
    let scripts_dir = config.scripts_root();
    let capture_claude = scripts_dir.join("capture-claude.py");
    if !capture_claude.exists() {
        println!(
            "{} Agent hooks not configured yet. Run 'agentdiff configure' first to set up global hooks.",
            "!".yellow()
        );
        println!();
    }

    // Step 1 — install git hooks
    if !no_git_hook {
        step_install_git_hook(repo_root, config)?;
    }

    // Step 2 — register repo in global config and create ledger/session dirs
    step_register_repo(repo_root, config)?;

    // Step 3 — save updated config
    config.save()?;
    println!(
        "{} Config written to {}",
        "ok".green(),
        Config::config_path().display()
    );

    // In impl-1, migration is intentionally disabled by default.
    if migrate {
        println!(
            "{} Legacy migration is not part of impl-1; skipping.",
            "!".yellow()
        );
    }

    println!();
    println!("{}", "agentdiff init complete.".bold().green());
    Ok(())
}

fn check_python3() -> Result<()> {
    // On Windows, `python3` may not exist; `python` is the common name.
    let python_cmd = if cfg!(windows) { "python" } else { "python3" };
    let output = Command::new(python_cmd).arg("--version").output();
    match output {
        Ok(out) if out.status.success() => {
            let ver = String::from_utf8_lossy(&out.stdout);
            let ver = ver.trim();
            println!("{} {python_cmd} found: {ver}", "ok".green());
            Ok(())
        }
        _ => Err(anyhow::anyhow!(
            "{python_cmd} not found on PATH.\n\
             agentdiff capture scripts require Python 3.\n\
             Install Python 3 and ensure it is on your PATH, then re-run 'agentdiff configure'."
        )),
    }
}

fn step_create_dirs(config: &Config) -> Result<()> {
    let dirs = [
        config.spillover_root(),
        config.scripts_root(),
        Config::config_path().parent().unwrap().to_path_buf(),
    ];
    for dir in &dirs {
        fs::create_dir_all(dir).with_context(|| format!("creating {}", dir.display()))?;
        println!("{} mkdir {}", "ok".green().dimmed(), dir.display());
    }
    Ok(())
}

fn step_install_scripts(config: &Config) -> Result<()> {
    let scripts = [
        ("capture-claude.py", CLAUDE_CAPTURE_SCRIPT),
        ("capture-cursor.py", CURSOR_CAPTURE_SCRIPT),
        ("capture-codex.py", CODEX_CAPTURE_SCRIPT),
        ("capture-windsurf.py", WINDSURF_CAPTURE_SCRIPT),
        ("capture-opencode.py", OPENCODE_CAPTURE_SCRIPT),
        ("capture-antigravity.py", ANTIGRAVITY_CAPTURE_SCRIPT),
        ("capture-copilot.py", COPILOT_CAPTURE_SCRIPT),
        ("prepare-ledger.py", PREPARE_LEDGER_SCRIPT),
        ("finalize-ledger.py", FINALIZE_LEDGER_SCRIPT),
        ("record-context.py", RECORD_CONTEXT_SCRIPT),
        ("write-note.py", WRITE_NOTE_SCRIPT),
    ];
    for (name, content) in &scripts {
        let dest = config.scripts_root().join(name);
        let existing = fs::read_to_string(&dest).unwrap_or_default();
        if existing != *content {
            fs::write(&dest, content).with_context(|| format!("writing {}", dest.display()))?;
            println!("{} installed {}", "ok".green(), dest.display());
        } else {
            println!("{} up-to-date {}", "--".dimmed(), dest.display());
        }
    }
    Ok(())
}

fn step_install_git_hook(repo_root: &Path, config: &Config) -> Result<()> {
    let hooks_dir = repo_root.join(".git").join("hooks");
    if !hooks_dir.exists() {
        bail!("Not a git repository (no .git/hooks directory)");
    }

    let pre_commit_path = hooks_dir.join("pre-commit");
    let post_commit_path = hooks_dir.join("post-commit");
    let scripts_dir = config.scripts_root();
    let session_log = Config::repo_session_log(repo_root);
    let pending_context = Config::repo_pending_context(repo_root);
    let pending_ledger = Config::repo_pending_ledger(repo_root);
    let ledger_path = Config::repo_ledger_path(repo_root);
    let lockfile = Config::repo_lockfile(repo_root);
    let auto_amend_ledger = if config.auto_amend_ledger_enabled() {
        "1"
    } else {
        "0"
    };

    let pre_commit_content = format!(
        r#"#!/usr/bin/env bash
# agentdiff pre-commit hook — managed by agentdiff init
# DO NOT EDIT — regenerate with: agentdiff init

set -euo pipefail

REPO_ROOT="{repo_root}"
SESSION_LOG="{session_log}"
PENDING_CONTEXT="{pending_context}"
PENDING_LEDGER="{pending_ledger}"
SCRIPTS_DIR="{scripts_dir}"

mkdir -p "$(dirname "$PENDING_CONTEXT")"
python3 "$SCRIPTS_DIR/prepare-ledger.py" "$REPO_ROOT" "$SESSION_LOG" "$PENDING_CONTEXT" "$PENDING_LEDGER"
exit 0
"#,
        repo_root = repo_root.display(),
        session_log = session_log.display(),
        pending_context = pending_context.display(),
        pending_ledger = pending_ledger.display(),
        scripts_dir = scripts_dir.display(),
    );

    let post_commit_content = format!(
        r#"#!/usr/bin/env bash
# agentdiff post-commit hook — managed by agentdiff init
# DO NOT EDIT — regenerate with: agentdiff init

set -euo pipefail

REPO_ROOT="{repo_root}"
PENDING_CONTEXT="{pending_context}"
PENDING_LEDGER="{pending_ledger}"
LEDGER_PATH="{ledger_path}"
LEDGER_REL=".agentdiff/ledger.jsonl"
AUTO_AMEND_LEDGER="{auto_amend_ledger}"
LOCKFILE="{lockfile}"
SCRIPTS_DIR="{scripts_dir}"

[ -f "$LOCKFILE" ] && exit 0

mkdir -p "$(dirname "$LOCKFILE")"
touch "$LOCKFILE"
trap 'rm -f "$LOCKFILE"' EXIT

python3 "$SCRIPTS_DIR/finalize-ledger.py" "$REPO_ROOT" "$PENDING_LEDGER" "$PENDING_CONTEXT" "$LEDGER_PATH"

if [ "$AUTO_AMEND_LEDGER" = "1" ] && [ -f "$LEDGER_PATH" ]; then
  if git -C "$REPO_ROOT" add "$LEDGER_REL"; then
    if ! git -C "$REPO_ROOT" diff --cached --quiet -- "$LEDGER_REL"; then
      if git -C "$REPO_ROOT" commit --amend --no-edit --no-verify >/dev/null 2>&1; then
        echo "agentdiff: ledger included via auto-amend"
      else
        echo "agentdiff: warning: auto-amend failed; ledger remains staged for manual commit" >&2
      fi
    fi
  else
    echo "agentdiff: warning: unable to stage ledger for auto-amend" >&2
  fi
fi
exit 0
"#,
        repo_root = repo_root.display(),
        pending_context = pending_context.display(),
        pending_ledger = pending_ledger.display(),
        ledger_path = ledger_path.display(),
        auto_amend_ledger = auto_amend_ledger,
        lockfile = lockfile.display(),
        scripts_dir = scripts_dir.display(),
    );

    install_managed_hook(
        &pre_commit_path,
        "agentdiff pre-commit hook",
        &pre_commit_content,
    )?;
    install_managed_hook(
        &post_commit_path,
        "agentdiff post-commit hook",
        &post_commit_content,
    )?;

    println!(
        "{} installed git hooks (pre-commit, post-commit)",
        "ok".green()
    );
    println!(
        "{} same-commit ledger auto-amend: {}",
        "ok".green(),
        if config.auto_amend_ledger_enabled() {
            "enabled"
        } else {
            "disabled"
        }
    );
    Ok(())
}

fn install_managed_hook(path: &Path, marker: &str, content: &str) -> Result<()> {
    if path.exists() {
        let existing = fs::read_to_string(path)?;
        if existing.contains(marker) {
            fs::write(path, content)?;
        } else {
            let combined = format!("{}\n\n{}", existing.trim_end(), content);
            fs::write(path, combined)?;
            println!("{} appended to existing {}", "ok".green(), path.display());
        }
    } else {
        fs::write(path, content)?;
    }

    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        fs::set_permissions(path, fs::Permissions::from_mode(0o755))?;
    }
    Ok(())
}

#[allow(dead_code)]
fn step_configure_git_notes(repo_root: &Path) -> Result<()> {
    let set_pairs = [
        ("notes.rewrite.amend", "true"),
        ("notes.rewrite.rebase", "true"),
        ("notes.rewriteRef", "refs/notes/agentdiff"),
        ("notes.rewriteMode", "overwrite"),
    ];

    for (key, value) in set_pairs {
        let status = Command::new("git")
            .args(["config", key, value])
            .current_dir(repo_root)
            .status()
            .with_context(|| format!("setting git config {key}"))?;
        if status.success() {
            println!("{} git config {}={}", "ok".green().dimmed(), key, value);
        }
    }

    // Add notes fetch refspec once.
    let fetch_spec = "+refs/notes/agentdiff:refs/notes/agentdiff";
    let fetch_output = Command::new("git")
        .args(["config", "--get-all", "remote.origin.fetch"])
        .current_dir(repo_root)
        .output()?;
    let existing = String::from_utf8_lossy(&fetch_output.stdout);
    if !existing.lines().any(|line| line.trim() == fetch_spec) {
        let status = Command::new("git")
            .args(["config", "--add", "remote.origin.fetch", fetch_spec])
            .current_dir(repo_root)
            .status()
            .context("adding remote.origin.fetch notes refspec")?;
        if status.success() {
            println!("{} added notes fetch refspec", "ok".green().dimmed());
        }
    } else {
        println!("{} notes fetch refspec already present", "--".dimmed());
    }

    // One-time fetch of notes from origin, only when URL is configured.
    let origin_url = Command::new("git")
        .args(["config", "--get", "remote.origin.url"])
        .current_dir(repo_root)
        .output()
        .ok()
        .and_then(|o| {
            if o.status.success() {
                let s = String::from_utf8_lossy(&o.stdout).trim().to_string();
                if s.is_empty() { None } else { Some(s) }
            } else {
                None
            }
        });

    if origin_url.is_some() {
        let has_remote_notes_output = Command::new("git")
            .args([
                "-c",
                "credential.helper=",
                "ls-remote",
                "--exit-code",
                "origin",
                "refs/notes/agentdiff",
            ])
            .current_dir(repo_root)
            .output();
        let has_remote_notes = has_remote_notes_output
            .as_ref()
            .map(|o| o.status.success())
            .unwrap_or(false);

        if has_remote_notes {
            let fetch_status = Command::new("git")
                .args([
                    "-c",
                    "credential.helper=",
                    "fetch",
                    "origin",
                    "refs/notes/agentdiff:refs/notes/agentdiff",
                ])
                .current_dir(repo_root)
                .status();
            match fetch_status {
                Ok(status) if status.success() => {
                    println!("{} fetched refs/notes/agentdiff", "ok".green().dimmed())
                }
                _ => println!(
                    "{} unable to fetch refs/notes/agentdiff from origin (continuing)",
                    "!".yellow()
                ),
            }
        } else {
            println!(
                "{} no remote refs/notes/agentdiff yet (nothing to fetch)",
                "--".dimmed()
            );
        }
    }

    Ok(())
}

fn step_configure_claude(config: &Config) -> Result<()> {
    let settings_path = dirs::home_dir()
        .unwrap()
        .join(".claude")
        .join("settings.json");
    let scripts_dir = config.scripts_root();
    let capture_script = scripts_dir.join("capture-claude.py");

    let raw = fs::read_to_string(&settings_path).unwrap_or_else(|_| "{}".to_string());
    let mut settings: serde_json::Value =
        serde_json::from_str(&raw).context("parsing ~/.claude/settings.json")?;

    let hooks = settings
        .as_object_mut()
        .unwrap()
        .entry("hooks")
        .or_insert(serde_json::json!({}))
        .as_object_mut()
        .unwrap()
        .entry("PostToolUse")
        .or_insert(serde_json::json!([]))
        .as_array_mut()
        .unwrap();

    let new_hook = serde_json::json!({
        "matcher": "Edit|Write|MultiEdit",
        "hooks": [{
            "type": "command",
            "command": format!("python3 {}", capture_script.display())
        }]
    });

    let mut changed = false;
    let mut found = false;
    for hook in hooks.iter_mut() {
        let Some(hs) = hook.get_mut("hooks").and_then(|v| v.as_array_mut()) else {
            continue;
        };
        for inner in hs.iter_mut() {
            let Some(cmd_val) = inner.get_mut("command") else {
                continue;
            };
            let Some(cmd) = cmd_val.as_str() else {
                continue;
            };
            if cmd.contains("capture-claude.py") {
                found = true;
                let wanted = format!("python3 {}", capture_script.display());
                if cmd != wanted {
                    *cmd_val = serde_json::Value::String(wanted);
                    changed = true;
                }
            }
        }
    }

    if !found {
        hooks.push(new_hook);
        changed = true;
    }

    if changed {
        let updated = serde_json::to_string_pretty(&settings)?;
        if let Some(parent) = settings_path.parent() {
            fs::create_dir_all(parent)?;
        }
        fs::write(&settings_path, updated)?;
        println!(
            "{} Claude Code hook configured in {}",
            "ok".green(),
            settings_path.display()
        );
    } else {
        println!("{} Claude Code hook already present", "--".dimmed());
    }
    Ok(())
}

fn step_configure_cursor(config: &Config) -> Result<()> {
    let hooks_path = dirs::home_dir().unwrap().join(".cursor").join("hooks.json");
    if !hooks_path.exists() {
        println!(
            "{} ~/.cursor/hooks.json not found — skipping Cursor setup",
            "!".yellow()
        );
        return Ok(());
    }

    let capture_script = config.scripts_root().join("capture-cursor.py");
    let raw = fs::read_to_string(&hooks_path)?;
    let mut hooks_cfg: serde_json::Value =
        serde_json::from_str(&raw).context("parsing ~/.cursor/hooks.json")?;

    let hooks = hooks_cfg
        .as_object_mut()
        .unwrap()
        .entry("hooks")
        .or_insert(serde_json::json!({}))
        .as_object_mut()
        .unwrap();

    let capture_cmd = format!("python3 {}", capture_script.display());
    let events = ["afterFileEdit", "afterTabFileEdit", "beforeSubmitPrompt"];

    let mut changed = false;
    for event in events {
        let arr = hooks
            .entry(event)
            .or_insert(serde_json::json!([]))
            .as_array_mut()
            .unwrap();

        let mut found = false;
        for hook in arr.iter_mut() {
            let Some(cmd_val) = hook.get_mut("command") else {
                continue;
            };
            let Some(cmd) = cmd_val.as_str() else {
                continue;
            };
            if cmd.contains("capture-cursor.py") {
                found = true;
                if cmd != capture_cmd {
                    *cmd_val = serde_json::Value::String(capture_cmd.clone());
                    changed = true;
                }
            }
        }

        if !found {
            arr.push(serde_json::json!({ "command": capture_cmd }));
            changed = true;
        }

        // De-duplicate exact command duplicates while preserving order.
        let mut seen = std::collections::HashSet::new();
        arr.retain(|hook| {
            let Some(cmd) = hook.get("command").and_then(|c| c.as_str()) else {
                return true;
            };
            if seen.contains(cmd) {
                changed = true;
                false
            } else {
                seen.insert(cmd.to_string());
                true
            }
        });
    }

    if changed {
        fs::write(&hooks_path, serde_json::to_string_pretty(&hooks_cfg)?)?;
        println!(
            "{} Cursor hooks registered in {}",
            "ok".green(),
            hooks_path.display()
        );
    } else {
        println!("{} Cursor hooks already present", "--".dimmed());
    }
    Ok(())
}

fn step_configure_codex(config: &Config) -> Result<()> {
    let codex_dir = dirs::home_dir().unwrap().join(".codex");
    let config_path = codex_dir.join("config.toml");
    if !codex_dir.exists() && !config_path.exists() {
        println!("{} ~/.codex not found — skipping Codex setup", "!".yellow());
        return Ok(());
    }

    let capture_script = config.scripts_root().join("capture-codex.py");
    let raw = fs::read_to_string(&config_path).unwrap_or_default();
    let mut cfg_val: toml::Value = if raw.trim().is_empty() {
        toml::Value::Table(Default::default())
    } else {
        toml::from_str(&raw).context("parsing ~/.codex/config.toml")?
    };

    let table = cfg_val
        .as_table_mut()
        .context("Codex config root must be a table")?;
    let mut changed = false;

    let current_notify = table.get("notify").and_then(toml_array_to_strings);
    let wanted_base = vec![
        "python3".to_string(),
        capture_script.to_string_lossy().to_string(),
    ];

    let next_notify = match current_notify {
        None => wanted_base.clone(),
        Some(existing) => {
            if existing
                .iter()
                .any(|part| part.contains("capture-codex.py"))
            {
                if let Some(forward_idx) = existing.iter().position(|p| p == "--forward") {
                    let forward = existing.get(forward_idx + 1).cloned().unwrap_or_default();
                    if forward.is_empty() {
                        wanted_base.clone()
                    } else {
                        let mut with_forward = wanted_base.clone();
                        with_forward.push("--forward".to_string());
                        with_forward.push(forward);
                        with_forward
                    }
                } else {
                    wanted_base.clone()
                }
            } else if existing.is_empty() {
                wanted_base.clone()
            } else {
                let forward = serde_json::to_string(&existing)?;
                let mut chained = wanted_base.clone();
                chained.push("--forward".to_string());
                chained.push(forward);
                chained
            }
        }
    };

    if table
        .get("notify")
        .and_then(toml_array_to_strings)
        .unwrap_or_default()
        != next_notify
    {
        table.insert("notify".to_string(), string_array_to_toml(&next_notify));
        changed = true;
    }

    // Ensure Codex hooks are enabled so notify is actually emitted.
    let features = table
        .entry("features".to_string())
        .or_insert(toml::Value::Table(Default::default()));
    let features_table = features
        .as_table_mut()
        .context("~/.codex/config.toml [features] must be a table")?;
    if features_table.get("codex_hooks").and_then(|v| v.as_bool()) != Some(true) {
        features_table.insert("codex_hooks".to_string(), toml::Value::Boolean(true));
        changed = true;
    }

    if changed {
        fs::create_dir_all(&codex_dir)?;
        fs::write(&config_path, toml::to_string_pretty(&cfg_val)?)?;
        println!(
            "{} Codex notify hook configured in {}",
            "ok".green(),
            config_path.display()
        );
    } else {
        println!("{} Codex notify hook already present", "--".dimmed());
    }
    Ok(())
}

fn step_configure_antigravity(config: &Config) -> Result<()> {
    let gemini_dir = dirs::home_dir().unwrap().join(".gemini");
    let settings_path = gemini_dir.join("settings.json");
    if !gemini_dir.exists() && !settings_path.exists() {
        println!(
            "{} ~/.gemini not found — skipping Gemini/Antigravity setup",
            "!".yellow()
        );
        return Ok(());
    }

    let capture_script = config.scripts_root().join("capture-antigravity.py");
    let capture_cmd = format!("python3 {}", capture_script.display());

    let raw = fs::read_to_string(&settings_path).unwrap_or_else(|_| "{}".to_string());
    let mut cfg: serde_json::Value =
        serde_json::from_str(&raw).context("parsing ~/.gemini/settings.json")?;
    let root = cfg
        .as_object_mut()
        .context("~/.gemini/settings.json root must be an object")?;
    let hooks = root
        .entry("hooks")
        .or_insert(serde_json::json!({}))
        .as_object_mut()
        .context("~/.gemini/settings.json hooks must be an object")?;

    let mut changed = false;
    let events = ["BeforeTool", "AfterTool"];

    for event in events {
        let arr = hooks
            .entry(event)
            .or_insert(serde_json::json!([]))
            .as_array_mut()
            .context("Gemini hook event must be an array")?;

        let mut found_matcher_idx: Option<usize> = None;
        for (idx, item) in arr.iter().enumerate() {
            let matcher = item.get("matcher").and_then(|m| m.as_str()).unwrap_or("");
            if matcher == "write_file|replace" {
                found_matcher_idx = Some(idx);
                break;
            }
        }

        if found_matcher_idx.is_none() {
            arr.push(serde_json::json!({
                "matcher": "write_file|replace",
                "hooks": [{
                    "type": "command",
                    "command": capture_cmd
                }]
            }));
            changed = true;
            continue;
        }

        if let Some(idx) = found_matcher_idx {
            let Some(obj) = arr[idx].as_object_mut() else {
                continue;
            };
            let inner = obj
                .entry("hooks")
                .or_insert(serde_json::json!([]))
                .as_array_mut()
                .context("Gemini hooks entry must contain hooks array")?;

            let mut found_cmd = false;
            for hook in inner.iter_mut() {
                let Some(cmd_val) = hook.get_mut("command") else {
                    continue;
                };
                let Some(cmd) = cmd_val.as_str() else {
                    continue;
                };
                if cmd.contains("capture-antigravity.py") {
                    found_cmd = true;
                    if cmd != capture_cmd {
                        *cmd_val = serde_json::Value::String(capture_cmd.clone());
                        changed = true;
                    }
                }
            }

            if !found_cmd {
                inner.push(serde_json::json!({
                    "type": "command",
                    "command": capture_cmd
                }));
                changed = true;
            }

            let mut seen = std::collections::HashSet::new();
            inner.retain(|hook| {
                let Some(cmd) = hook.get("command").and_then(|c| c.as_str()) else {
                    return true;
                };
                if seen.contains(cmd) {
                    changed = true;
                    false
                } else {
                    seen.insert(cmd.to_string());
                    true
                }
            });
        }
    }

    let tools_obj = root
        .entry("tools")
        .or_insert(serde_json::json!({}))
        .as_object_mut()
        .context("~/.gemini/settings.json tools must be an object")?;
    if tools_obj
        .get("enableHooks")
        .and_then(|v| v.as_bool())
        .unwrap_or(false)
        != true
    {
        tools_obj.insert("enableHooks".to_string(), serde_json::Value::Bool(true));
        changed = true;
    }

    if changed {
        fs::create_dir_all(&gemini_dir)?;
        fs::write(&settings_path, serde_json::to_string_pretty(&cfg)?)?;
        println!(
            "{} Gemini/Antigravity hooks configured in {}",
            "ok".green(),
            settings_path.display()
        );
    } else {
        println!(
            "{} Gemini/Antigravity hooks already present",
            "--".dimmed()
        );
    }
    Ok(())
}

fn step_configure_windsurf(config: &Config) -> Result<()> {
    // Use the Windsurf user-level global config (~/.codeium/windsurf/hooks.json).
    // This applies to all repos without needing per-repo setup.
    let hooks_path = dirs::home_dir()
        .unwrap()
        .join(".codeium")
        .join("windsurf")
        .join("hooks.json");
    let capture_script = config.scripts_root().join("capture-windsurf.py");
    let capture_cmd = format!("python3 {}", capture_script.display());

    let raw = fs::read_to_string(&hooks_path).unwrap_or_else(|_| "{}".to_string());
    let mut hooks_cfg: serde_json::Value =
        serde_json::from_str(&raw).context("parsing .windsurf/hooks.json")?;
    let hooks = hooks_cfg
        .as_object_mut()
        .unwrap()
        .entry("hooks")
        .or_insert(serde_json::json!({}))
        .as_object_mut()
        .unwrap();

    let events = ["post_write_code", "post_cascade_response_with_transcript"];
    let mut changed = false;

    for event in events {
        let arr = hooks
            .entry(event)
            .or_insert(serde_json::json!([]))
            .as_array_mut()
            .unwrap();

        let mut found = false;
        for hook in arr.iter_mut() {
            let Some(cmd_val) = hook.get_mut("command") else {
                continue;
            };
            let Some(cmd) = cmd_val.as_str() else {
                continue;
            };
            if cmd.contains("capture-windsurf.py") {
                found = true;
                if cmd != capture_cmd {
                    *cmd_val = serde_json::Value::String(capture_cmd.clone());
                    changed = true;
                }
            }
        }

        if !found {
            arr.push(serde_json::json!({
                "type": "command",
                "command": capture_cmd
            }));
            changed = true;
        }

        let mut seen = std::collections::HashSet::new();
        arr.retain(|hook| {
            let Some(cmd) = hook.get("command").and_then(|c| c.as_str()) else {
                return true;
            };
            if seen.contains(cmd) {
                changed = true;
                false
            } else {
                seen.insert(cmd.to_string());
                true
            }
        });
    }

    if changed {
        if let Some(parent) = hooks_path.parent() {
            fs::create_dir_all(parent)?;
        }
        fs::write(&hooks_path, serde_json::to_string_pretty(&hooks_cfg)?)?;
        println!(
            "{} Windsurf hooks configured in {}",
            "ok".green(),
            hooks_path.display()
        );
    } else {
        println!(
            "{} Windsurf hooks already present in {}",
            "--".dimmed(),
            hooks_path.display()
        );
    }
    Ok(())
}

fn step_configure_opencode(config: &Config) -> Result<()> {
    // Use the OpenCode global plugins directory (~/.config/opencode/plugins/).
    // This applies to all repos without needing per-repo setup.
    // dirs::config_dir() returns ~/.config on Linux, ~/Library/Application Support on macOS.
    let plugins_dir = dirs::config_dir()
        .unwrap_or_else(|| dirs::home_dir().unwrap().join(".config"))
        .join("opencode")
        .join("plugins");
    let plugin_path = plugins_dir.join("agentdiff.ts");

    let capture_script = config.scripts_root().join("capture-opencode.py");
    let plugin_content = OPENCODE_PLUGIN_TEMPLATE.replace(
        "__AGENTDIFF_CAPTURE_OPENCODE__",
        &capture_script.to_string_lossy(),
    );

    let existing = fs::read_to_string(&plugin_path).unwrap_or_default();
    if !existing.is_empty()
        && !existing.contains("agentdiff plugin for OpenCode")
        && existing != plugin_content
    {
        println!(
            "{} {} exists and is not managed by agentdiff — skipping",
            "!".yellow(),
            plugin_path.display()
        );
        return Ok(());
    }

    if existing != plugin_content {
        fs::create_dir_all(&plugins_dir)?;
        fs::write(&plugin_path, plugin_content)?;
        println!(
            "{} OpenCode plugin configured in {}",
            "ok".green(),
            plugin_path.display()
        );
    } else {
        println!(
            "{} OpenCode plugin already present in {}",
            "--".dimmed(),
            plugin_path.display()
        );
    }

    Ok(())
}

fn step_configure_copilot(config: &Config) -> Result<()> {
    let capture_script = config.scripts_root().join("capture-copilot.py");
    let ext_js = COPILOT_EXT_JS_TEMPLATE.replace(
        "__AGENTDIFF_CAPTURE_COPILOT__",
        &capture_script.to_string_lossy(),
    );

    // VS Code loads extensions placed directly in ~/.vscode/extensions/<name>-<version>/
    // This works on Linux, macOS, and Windows without any build step or vsce.
    let mut installed_any = false;
    for vscode_dir in vscode_extension_dirs() {
        let ext_dir = vscode_dir.join("agentdiff-copilot-0.1.0");
        if let Err(e) = fs::create_dir_all(&ext_dir) {
            println!(
                "{} cannot create {}: {}",
                "!".yellow(),
                ext_dir.display(),
                e
            );
            continue;
        }

        let pkg_path = ext_dir.join("package.json");
        let js_path = ext_dir.join("extension.js");

        let existing_pkg = fs::read_to_string(&pkg_path).unwrap_or_default();
        let existing_js = fs::read_to_string(&js_path).unwrap_or_default();

        let mut changed = false;
        if existing_pkg != COPILOT_EXT_PACKAGE_JSON {
            fs::write(&pkg_path, COPILOT_EXT_PACKAGE_JSON)
                .with_context(|| format!("writing {}", pkg_path.display()))?;
            changed = true;
        }
        if existing_js != ext_js {
            fs::write(&js_path, &ext_js)
                .with_context(|| format!("writing {}", js_path.display()))?;
            changed = true;
        }

        if changed {
            println!(
                "{} VS Code Copilot extension installed in {}",
                "ok".green(),
                ext_dir.display()
            );
            installed_any = true;
        } else {
            println!(
                "{} VS Code Copilot extension already up-to-date in {}",
                "--".dimmed(),
                ext_dir.display()
            );
            installed_any = true;
        }
    }

    if !installed_any {
        println!(
            "{} VS Code extensions directory not found — skipping Copilot setup",
            "!".yellow()
        );
        println!("    Checked: ~/.vscode-server/extensions, ~/.vscode/extensions, ~/.vscode-insiders/extensions");
        println!(
            "    To install manually: mkdir -p ~/.vscode-server/extensions/agentdiff-copilot-0.1.0 && cp {script_dir}/vscode-extension/* ~/.vscode-server/extensions/agentdiff-copilot-0.1.0/",
            script_dir = capture_script.parent().unwrap_or(capture_script.as_path()).display()
        );
    } else {
        println!("{} Restart VS Code to activate the agentdiff Copilot extension", "!".yellow());
    }

    Ok(())
}

/// Returns VS Code extension directories that exist on this machine.
///
/// Priority order:
/// 1. `~/.vscode-server/extensions`          — WSL2 / Remote-SSH extension host (Linux side).
///    Extensions here run inside WSL, so `python3` and file paths resolve correctly.
/// 2. `~/.vscode-server-insiders/extensions` — VS Code Insiders server variant.
/// 3. `~/.vscode/extensions`                 — VS Code stable on Linux / macOS / Windows.
/// 4. `~/.vscode-insiders/extensions`        — VS Code Insiders on Linux / macOS.
fn vscode_extension_dirs() -> Vec<std::path::PathBuf> {
    let home = match dirs::home_dir() {
        Some(h) => h,
        None => return Vec::new(),
    };

    let candidates = vec![
        // WSL2 / Remote-SSH: server-side extension host (highest priority on Linux)
        home.join(".vscode-server").join("extensions"),
        home.join(".vscode-server-insiders").join("extensions"),
        // Regular VS Code on Linux / macOS / Windows
        home.join(".vscode").join("extensions"),
        home.join(".vscode-insiders").join("extensions"),
    ];

    candidates
        .into_iter()
        .filter(|p| p.exists())
        .collect()
}

fn toml_array_to_strings(v: &toml::Value) -> Option<Vec<String>> {
    let arr = v.as_array()?;
    arr.iter()
        .map(|x| x.as_str().map(|s| s.to_string()))
        .collect()
}

fn string_array_to_toml(parts: &[String]) -> toml::Value {
    toml::Value::Array(
        parts
            .iter()
            .map(|p| toml::Value::String(p.clone()))
            .collect(),
    )
}

fn step_register_repo(repo_root: &Path, config: &mut Config) -> Result<()> {
    let slug = Config::slug_for(repo_root);
    let already = config.repos.iter().any(|r| r.slug == slug);
    if !already {
        config.repos.push(RepoConfig {
            path: repo_root.to_path_buf(),
            slug,
        });
        println!("{} Repo registered in config", "ok".green());
    }

    fs::create_dir_all(Config::repo_session_dir(repo_root))?;
    fs::create_dir_all(Config::repo_ledger_dir(repo_root))?;
    let ledger = Config::repo_ledger_path(repo_root);
    if !ledger.exists() {
        fs::write(&ledger, "")?;
    }
    Ok(())
}