tracevault-cli 0.23.1

CLI tool for Visdom Trace - AI code tracing and attribution
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
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
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
use crate::api_client::ApiClient;
use crate::config::{user_config_path_in, TracevaultConfig, UserContext};
use crate::context::Context;
use std::fs;
use std::io::{self, BufRead, IsTerminal, Write};
use std::path::{Path, PathBuf};

/// Which Claude Code settings file to install hooks into.
#[derive(Debug, Clone, Copy, PartialEq, Eq, clap::ValueEnum)]
pub enum ClaudeSettingsTarget {
    /// .claude/settings.json — typically committed/shared with the team.
    Shared,
    /// .claude/settings.local.json — personal, conventionally git-ignored.
    Local,
}

impl ClaudeSettingsTarget {
    pub fn filename(self) -> &'static str {
        match self {
            ClaudeSettingsTarget::Shared => "settings.json",
            ClaudeSettingsTarget::Local => "settings.local.json",
        }
    }

    pub fn gitignore_entry(self) -> &'static str {
        match self {
            ClaudeSettingsTarget::Shared => ".claude/settings.json",
            ClaudeSettingsTarget::Local => ".claude/settings.local.json",
        }
    }
}

/// Resolve which settings file to use. If the caller passed an explicit
/// choice, honor it. Otherwise prompt interactively when stdin is a TTY,
/// or fall back to Shared for non-interactive callers (CI, scripts, tests).
fn resolve_claude_target(
    explicit: Option<ClaudeSettingsTarget>,
) -> io::Result<ClaudeSettingsTarget> {
    if let Some(target) = explicit {
        return Ok(target);
    }
    if !io::stdin().is_terminal() {
        return Ok(ClaudeSettingsTarget::Shared);
    }

    let stdout = io::stdout();
    let mut stdout = stdout.lock();
    write!(
        stdout,
        "Install Claude Code hooks into [s]hared (.claude/settings.json) or [l]ocal (.claude/settings.local.json)? [s]: "
    )?;
    stdout.flush()?;

    let mut line = String::new();
    io::stdin().lock().read_line(&mut line)?;
    let answer = line.trim().to_lowercase();
    Ok(match answer.as_str() {
        "l" | "local" => ClaudeSettingsTarget::Local,
        _ => ClaudeSettingsTarget::Shared,
    })
}

pub fn git_remote_url(project_root: &Path) -> Option<String> {
    std::process::Command::new("git")
        .args(["remote", "get-url", "origin"])
        .current_dir(project_root)
        .output()
        .ok()
        .filter(|o| o.status.success())
        .map(|o| String::from_utf8_lossy(&o.stdout).trim().to_string())
        .filter(|s| !s.is_empty())
}

fn parse_github_org(remote_url: &str) -> Option<String> {
    // SSH: git@github.com:VirtusLab/visdom-ai-tracing.git
    if let Some(path) = remote_url.strip_prefix("git@github.com:") {
        return path.split('/').next().map(String::from);
    }
    // HTTPS: https://github.com/VirtusLab/visdom-ai-tracing.git
    if let Some(path) = remote_url
        .strip_prefix("https://github.com/")
        .or_else(|| remote_url.strip_prefix("http://github.com/"))
    {
        return path.split('/').next().map(String::from);
    }
    None
}

/// If `dir` is inside a LINKED git worktree (not the primary checkout), return
/// the primary worktree root. Returns `None` for the primary checkout or a
/// non-git directory.
///
/// A linked worktree has `git --git-dir` (e.g. `<primary>/.git/worktrees/<name>`)
/// distinct from `git --git-common-dir` (`<primary>/.git`); in the primary
/// checkout the two are equal. The primary root is the parent of the common dir.
fn linked_worktree_primary(dir: &Path) -> Option<PathBuf> {
    let output = std::process::Command::new("git")
        .args(["rev-parse", "--git-dir", "--git-common-dir"])
        .current_dir(dir)
        .output()
        .ok()?;
    if !output.status.success() {
        return None;
    }
    let stdout = String::from_utf8(output.stdout).ok()?;
    let mut lines = stdout.lines();
    let git_dir = lines.next()?.trim();
    let git_common_dir = lines.next()?.trim();

    // git may return paths relative to `dir`; join (absolute args replace the
    // base) then canonicalize so the comparison is robust.
    let git_dir = dir.join(git_dir).canonicalize().ok()?;
    let git_common_dir = dir.join(git_common_dir).canonicalize().ok()?;

    if git_dir == git_common_dir {
        None // primary checkout
    } else {
        git_common_dir.parent().map(Path::to_path_buf)
    }
}

pub async fn init_in_directory(
    project_root: &Path,
    server_url: Option<&str>,
    claude_settings: Option<ClaudeSettingsTarget>,
    no_gitignore: bool,
    user_context: UserContext,
) -> Result<ClaudeSettingsTarget, io::Error> {
    // Check for git repository
    if !project_root.join(".git").exists() {
        return Err(io::Error::new(
            io::ErrorKind::NotFound,
            "Not a git repository. Run 'git init' first.",
        ));
    }

    // Refuse to initialize from a linked worktree: TraceVault's `.tracevault/`,
    // git hooks, and Claude settings all belong in the primary checkout (which
    // every worktree shares). Initializing here would create a stray
    // `.tracevault/` in this worktree instead. (In a linked worktree `.git` is a
    // file, so the check above passes — this guard is what catches it.)
    if let Some(primary) = linked_worktree_primary(project_root) {
        return Err(io::Error::new(
            io::ErrorKind::Unsupported,
            format!(
                "Run `tracevault init` from the primary checkout ({}), not a linked worktree. \
                 cd there and run it again — all worktrees share the primary's .tracevault/.",
                primary.display()
            ),
        ));
    }

    let target = resolve_claude_target(claude_settings)?;

    // Create .tracevault/ directory
    let config_dir = TracevaultConfig::config_dir(project_root);
    fs::create_dir_all(&config_dir)?;
    fs::create_dir_all(config_dir.join("sessions"))?;
    fs::create_dir_all(config_dir.join("cache"))?;
    // Self-contained ignore for runtime artifacts, so this `.tracevault/` is
    // safe even if the root .gitignore doesn't cover it.
    TracevaultConfig::ensure_gitignore(&config_dir)?;

    // Keep tracevault's files local — update root .gitignore (unless opted out)
    if !no_gitignore {
        update_root_gitignore(project_root, target)?;
    }

    // Register repo on server if authenticated, server URL known, and git remote available
    let remote_url = git_remote_url(project_root);
    if remote_url.is_none() {
        eprintln!("Warning: no git remote 'origin' configured. Skipping server registration.");
        eprintln!("Run 'git remote add origin <url>' then 'tracevault sync' to register.");
    }

    // Extract org slug from GitHub remote URL
    let org_slug = remote_url.as_deref().and_then(parse_github_org);

    // Write config (include server_url and org_slug if available)
    let mut config = TracevaultConfig::default();
    if let Some(url) = server_url {
        config.server_url = Some(url.to_string());
    }
    config.org_slug = org_slug.clone();
    config.user_context = Some(user_context);
    fs::write(
        TracevaultConfig::config_path(project_root),
        config.to_toml(),
    )?;

    // Install Claude Code hooks into the chosen settings file
    install_claude_hooks(project_root, target)?;

    // Install git hooks
    install_git_hook(project_root)?;
    install_post_commit_hook(project_root)?;

    // Detect AI tools in the project
    let detected = crate::hooks::detect_tools(project_root);
    for tool in &detected {
        println!("  Detected: {}", tool.name());
    }

    let (resolved_url, resolved_token) = crate::api_client::resolve_credentials(project_root);
    let effective_url = server_url.map(String::from).or(resolved_url);

    if resolved_token.is_none() {
        eprintln!("Not logged in. Run 'tracevault login' to register this repo with the server.");
    } else if let (Some(url), Some(remote), Some(slug)) = (effective_url, remote_url, org_slug) {
        let client = ApiClient::new(&url, resolved_token.as_deref());
        let repo_name = git_repo_name(project_root);

        match client
            .register_repo(
                &slug,
                crate::api_client::RegisterRepoRequest {
                    repo_name,
                    github_url: Some(remote),
                },
            )
            .await
        {
            Ok(resp) => {
                println!("Repo registered on server (id: {})", resp.repo_id);
                // Save repo_id to config
                if let Some(mut cfg) = TracevaultConfig::load(project_root) {
                    cfg.repo_id = Some(resp.repo_id.to_string());
                    let _ = fs::write(TracevaultConfig::config_path(project_root), cfg.to_toml());
                }
            }
            Err(e) => {
                let msg = e.to_string();
                if msg.contains("404") {
                    eprintln!("Warning: organization '{}' not found on the server.", slug);
                    eprintln!(
                        "Create it first at your TraceVault instance, then run 'tracevault sync'."
                    );
                } else if msg.contains("403") {
                    eprintln!("Warning: you are not a member of organization '{}'.", slug);
                } else {
                    eprintln!("Warning: could not register repo on server: {e}");
                }
            }
        }
    }

    Ok(target)
}

fn update_root_gitignore(
    project_root: &Path,
    target: ClaudeSettingsTarget,
) -> Result<(), io::Error> {
    let path = project_root.join(".gitignore");
    let existing = if path.exists() {
        fs::read_to_string(&path)?
    } else {
        String::new()
    };

    // Only ignore what `init` actually creates or modifies: the `.tracevault/`
    // directory and the single Claude settings file we wrote hooks into. The
    // other settings file is left untouched, so we don't add it here.
    let needed: Vec<&str> = [".tracevault/", target.gitignore_entry()]
        .iter()
        .copied()
        .filter(|entry| !existing.lines().any(|line| line.trim() == *entry))
        .collect();

    if needed.is_empty() {
        return Ok(());
    }

    let mut content = existing;
    if !content.is_empty() && !content.ends_with('\n') {
        content.push('\n');
    }
    content.push_str("\n# TraceVault — local only, do not commit\n");
    for entry in needed {
        content.push_str(entry);
        content.push('\n');
    }

    fs::write(path, content)
}

fn git_repo_name(project_root: &Path) -> String {
    std::process::Command::new("git")
        .args(["rev-parse", "--show-toplevel"])
        .current_dir(project_root)
        .output()
        .ok()
        .filter(|o| o.status.success())
        .map(|o| String::from_utf8_lossy(&o.stdout).trim().to_string())
        .as_deref()
        .and_then(|p| p.rsplit('/').next())
        .map(String::from)
        .unwrap_or_else(|| "unknown".into())
}

const HOOK_MARKER: &str = "# tracevault:enforce";
const OLD_HOOK_MARKER: &str = "# tracevault:auto-push";

fn install_git_hook(project_root: &Path) -> Result<(), io::Error> {
    let hooks_dir = project_root.join(".git/hooks");
    fs::create_dir_all(&hooks_dir)?;

    let hook_path = hooks_dir.join("pre-push");
    let tracevault_block = format!(
        "{HOOK_MARKER}\ntracevault sync 2>/dev/null || true\ntracevault check || {{ echo \"tracevault: policy check failed\"; exit 1; }}\n"
    );

    if hook_path.exists() {
        let existing = fs::read_to_string(&hook_path)?;

        // Already has new-style hook
        if existing.contains(HOOK_MARKER) {
            return Ok(());
        }

        // Replace old-style hook block if present
        if existing.contains(OLD_HOOK_MARKER) {
            let mut new_content = String::new();
            let mut skip = false;
            for line in existing.lines() {
                if line.contains(OLD_HOOK_MARKER) {
                    skip = true;
                    continue;
                }
                if skip {
                    // Skip old tracevault lines (they start with "tracevault " or are empty continuations)
                    if line.starts_with("tracevault ") {
                        continue;
                    }
                    skip = false;
                }
                new_content.push_str(line);
                new_content.push('\n');
            }
            if !new_content.ends_with('\n') {
                new_content.push('\n');
            }
            new_content.push_str(&tracevault_block);
            fs::write(&hook_path, new_content)?;
        } else {
            // Append to existing hook
            let mut content = existing;
            if !content.ends_with('\n') {
                content.push('\n');
            }
            content.push_str(&tracevault_block);
            fs::write(&hook_path, content)?;
        }
    } else {
        let content = format!("#!/bin/sh\n{tracevault_block}");
        fs::write(&hook_path, content)?;
    }

    // Make executable
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        let mut perms = fs::metadata(&hook_path)?.permissions();
        perms.set_mode(0o755);
        fs::set_permissions(&hook_path, perms)?;
    }

    Ok(())
}

const POST_COMMIT_MARKER: &str = "# tracevault:post-commit";

fn install_post_commit_hook(project_root: &Path) -> Result<(), io::Error> {
    let hooks_dir = project_root.join(".git/hooks");
    fs::create_dir_all(&hooks_dir)?;

    let hook_path = hooks_dir.join("post-commit");
    let tracevault_block = format!("{POST_COMMIT_MARKER}\ntracevault commit-push 2>/dev/null &\n");

    if hook_path.exists() {
        let existing = fs::read_to_string(&hook_path)?;

        if existing.contains(POST_COMMIT_MARKER) {
            return Ok(());
        }

        let mut content = existing;
        if !content.ends_with('\n') {
            content.push('\n');
        }
        content.push_str(&tracevault_block);
        fs::write(&hook_path, content)?;
    } else {
        let content = format!("#!/bin/sh\n{tracevault_block}");
        fs::write(&hook_path, content)?;
    }

    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        let mut perms = fs::metadata(&hook_path)?.permissions();
        perms.set_mode(0o755);
        fs::set_permissions(&hook_path, perms)?;
    }

    Ok(())
}

fn install_claude_hooks(
    project_root: &Path,
    target: ClaudeSettingsTarget,
) -> Result<(), io::Error> {
    let claude_dir = project_root.join(".claude");
    fs::create_dir_all(&claude_dir)?;

    let filename = target.filename();
    let settings_path = claude_dir.join(filename);
    let mut settings: serde_json::Value = if settings_path.exists() {
        let content = fs::read_to_string(&settings_path)?;
        serde_json::from_str(&content).map_err(|e| {
            io::Error::new(
                io::ErrorKind::InvalidData,
                format!("Failed to parse .claude/{filename}: {e}"),
            )
        })?
    } else {
        serde_json::json!({})
    };

    let hooks = tracevault_hooks();

    // Merge hooks into existing settings
    let settings_obj = settings.as_object_mut().ok_or_else(|| {
        io::Error::new(
            io::ErrorKind::InvalidData,
            format!(".claude/{filename} is not a JSON object"),
        )
    })?;

    settings_obj.insert("hooks".to_string(), hooks);

    let formatted = serde_json::to_string_pretty(&settings)
        .map_err(|e| io::Error::other(format!("Failed to serialize settings: {e}")))?;
    fs::write(&settings_path, formatted)?;

    Ok(())
}

pub fn tracevault_hooks() -> serde_json::Value {
    serde_json::json!({
        "PreToolUse": [{
            "matcher": "Write|Edit|Bash",
            "hooks": [{
                "type": "command",
                "command": "tracevault stream --event pre-tool-use",
                "timeout": 10,
                "statusMessage": "TraceVault: streaming pre-tool event"
            }]
        }],
        "PostToolUse": [{
            "matcher": "",
            "hooks": [{
                "type": "command",
                "command": "tracevault stream --event post-tool-use",
                "timeout": 10,
                "statusMessage": "TraceVault: streaming post-tool event"
            }]
        }],
        "Notification": [{
            "matcher": "",
            "hooks": [{
                "type": "command",
                "command": "tracevault stream --event notification",
                "timeout": 10,
                "statusMessage": "TraceVault: streaming notification"
            }]
        }],
        "Stop": [{
            "matcher": "",
            "hooks": [{
                "type": "command",
                "command": "tracevault stream --event stop",
                "timeout": 10,
                "statusMessage": "TraceVault: finalizing session"
            }]
        }],
        "SessionStart": [{
            "matcher": "",
            "hooks": [{
                "type": "command",
                "command": "tracevault session-start",
                "timeout": 10,
                "statusMessage": "TraceVault: session start"
            }]
        }],
        "UserPromptSubmit": [{
            "matcher": "",
            "hooks": [{
                "type": "command",
                "command": "tracevault user-prompt",
                "timeout": 10,
                "statusMessage": "TraceVault: policy reinforcement"
            }]
        }]
    })
}

const GLOBAL_CLAUDE_MD_MARKER: &str = "<!-- tracevault:workspace-mode -->";

const GLOBAL_CLAUDE_MD_BLOCK: &str = "\
<!-- tracevault:workspace-mode -->
## TraceVault (workspace mode)
You may be running detached from any single repository. Before working on a repo, run
`tracevault repo switch <path>` to bind tracing and fetch that repo's policies; treat its
output as binding. Use `--path <path>` on `tracevault repo status` for a one-off. Repos must
already be registered with TraceVault.
";

/// Join a constant file name to `base` and confirm the result stays directly
/// within `base` — defense-in-depth so a path derived from the environment
/// ($HOME) cannot escape the intended `.claude` directory.
fn safe_join(base: &Path, name: &str) -> io::Result<PathBuf> {
    let joined = base.join(name);
    if joined.parent() != Some(base) {
        return Err(io::Error::new(
            io::ErrorKind::InvalidInput,
            format!(
                "refusing to access path outside {}: {}",
                base.display(),
                joined.display()
            ),
        ));
    }
    Ok(joined)
}

/// Merge our hook entries into an existing `settings["hooks"]` object, per
/// event, appending our entry only if no existing entry in that event's
/// array already has an inner hook with the same `command` (dedupe → makes
/// repeated calls idempotent). Never removes or overwrites the user's other
/// hooks or top-level keys.
///
/// Absent values (no top-level `settings`, no `hooks` key, no entry for a
/// given event) are created fresh — that's fine. But if an EXISTING value
/// has an unexpected shape (top-level `settings` isn't an object, an
/// existing `hooks` isn't an object, or an existing per-event value isn't an
/// array), this errors loudly instead of silently discarding it, mirroring
/// `install_claude_hooks`'s treatment of a non-object settings file.
fn merge_hooks(
    settings: &mut serde_json::Value,
    ours: &serde_json::Value,
    path: &Path,
) -> io::Result<()> {
    if settings.is_null() {
        *settings = serde_json::json!({});
    }
    let settings_obj = settings.as_object_mut().ok_or_else(|| {
        io::Error::new(
            io::ErrorKind::InvalidData,
            format!("{} is not a JSON object", path.display()),
        )
    })?;

    let hooks_value = settings_obj
        .entry("hooks")
        .or_insert_with(|| serde_json::json!({}));
    let hooks_obj = hooks_value.as_object_mut().ok_or_else(|| {
        io::Error::new(
            io::ErrorKind::InvalidData,
            format!("{} \"hooks\" key is not a JSON object", path.display()),
        )
    })?;

    let Some(ours_obj) = ours.as_object() else {
        return Ok(());
    };

    for (event, our_array) in ours_obj {
        let Some(our_entries) = our_array.as_array() else {
            continue;
        };

        let existing_value = hooks_obj
            .entry(event.clone())
            .or_insert_with(|| serde_json::json!([]));
        let existing_array = existing_value.as_array_mut().ok_or_else(|| {
            io::Error::new(
                io::ErrorKind::InvalidData,
                format!(
                    "{} \"hooks\".\"{event}\" is not a JSON array",
                    path.display()
                ),
            )
        })?;

        for our_entry in our_entries {
            let our_command = entry_command(our_entry);
            let already_present = existing_array
                .iter()
                .any(|existing_entry| entry_contains_command(existing_entry, our_command));
            if !already_present {
                existing_array.push(our_entry.clone());
            }
        }
    }

    Ok(())
}

/// Extract the inner `hooks[0].command` string from a hook-event entry, if
/// present.
fn entry_command(entry: &serde_json::Value) -> Option<&str> {
    entry.get("hooks")?.get(0)?.get("command")?.as_str()
}

/// Whether any of `entry`'s inner `hooks[].command` values equal `cmd`. Unlike
/// `entry_command` (which only looks at `hooks[0]`), this scans the whole
/// inner `hooks` array — a user entry can have our command anywhere in it
/// (e.g. `hooks[1]`), and missing that would let a duplicate get appended.
fn entry_contains_command(entry: &serde_json::Value, cmd: Option<&str>) -> bool {
    let Some(hooks) = entry.get("hooks").and_then(|h| h.as_array()) else {
        return false;
    };
    hooks
        .iter()
        .any(|hook| hook.get("command").and_then(|c| c.as_str()) == cmd)
}

/// Install TraceVault's Claude Code hooks into `claude_dir/settings.json`
/// (deep-merged, never clobbering existing hooks/keys) and append a
/// workspace-mode instruction block to `claude_dir/CLAUDE.md` (idempotent,
/// guarded by a marker comment). Intended for `tracevault init --global`,
/// installing once for all Claude Code sessions rather than per-repo.
pub fn install_global_hooks(claude_dir: &Path) -> io::Result<()> {
    fs::create_dir_all(claude_dir)?;

    // Canonicalize the base to defend against path traversal attacks.
    // safe_join will verify that target files stay directly within this base.
    let base = claude_dir.canonicalize()?;

    // --- settings.json: deep-merge hooks ---
    let settings_path = safe_join(&base, "settings.json")?;
    let mut settings: serde_json::Value = if settings_path.exists() {
        let content = fs::read_to_string(&settings_path)?;
        serde_json::from_str(&content).map_err(|e| {
            io::Error::new(
                io::ErrorKind::InvalidData,
                format!("Failed to parse {}: {e}", settings_path.display()),
            )
        })?
    } else {
        serde_json::json!({})
    };

    merge_hooks(&mut settings, &tracevault_hooks(), &settings_path)?;

    let formatted = serde_json::to_string_pretty(&settings)
        .map_err(|e| io::Error::other(format!("Failed to serialize settings: {e}")))?;
    // Write to a temp file in the same directory, then atomically rename over
    // the target so a crash or ENOSPC mid-write can never truncate/corrupt
    // the user's global settings.json.
    let tmp_settings_path = settings_path.with_extension("json.tmp");
    fs::write(&tmp_settings_path, formatted)?;
    fs::rename(&tmp_settings_path, &settings_path)?;

    // --- CLAUDE.md: append instruction block, idempotently ---
    let claude_md_path = safe_join(&base, "CLAUDE.md")?;
    let existing = if claude_md_path.exists() {
        fs::read_to_string(&claude_md_path)?
    } else {
        String::new()
    };

    if !existing.contains(GLOBAL_CLAUDE_MD_MARKER) {
        let mut content = existing;
        if !content.is_empty() && !content.ends_with('\n') {
            content.push('\n');
        }
        if !content.is_empty() {
            content.push('\n');
        }
        content.push_str(GLOBAL_CLAUDE_MD_BLOCK);
        fs::write(&claude_md_path, content)?;
    }

    Ok(())
}

/// Write the user-level `config.toml` (under `config_root`) carrying the
/// user-context setting. An EXISTING user config is preserved (this mirrors the
/// "don't clobber" contract of the global-hooks install): `requested` overrides
/// the user_context when a flag was given; otherwise an existing value is kept,
/// and only a first-time setup (no existing config) defaults to enabled.
/// When the resulting layer is enabled, the referenced context file is seeded
/// (empty) if absent — a custom `--user-context <path>` seeds THAT file, a
/// disabled layer seeds nothing. Returns the active context file path (the file
/// the hook will read), or `None` when the layer is disabled. Used by `init --global`.
pub fn write_global_user_config_in(
    config_root: &Path,
    requested: Option<UserContext>,
) -> io::Result<Option<PathBuf>> {
    fs::create_dir_all(config_root)?;
    // Preserve an existing (valid) user config; malformed/missing → start fresh.
    let mut config = crate::config::try_load_user_config_in(config_root)
        .ok()
        .flatten()
        .unwrap_or_default();
    // Requested flag wins; else keep an existing value; else first-run default = enabled.
    let user_context = requested
        .or_else(|| config.user_context.clone())
        .unwrap_or(UserContext::Toggle(true));
    let active = user_context
        .resolve()
        .map(|_| user_context.path_in(config_root));
    config.user_context = Some(user_context);
    fs::write(user_config_path_in(config_root), config.to_toml())?;
    if let Some(ref ctx_path) = active {
        if !ctx_path.exists() {
            Context::default().save_to(ctx_path)?;
        }
    }
    Ok(active)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn linked_worktree_primary_detects_linked_and_primary() {
        let tmp = tempfile::tempdir().unwrap();
        let repo = tmp.path().join("repo");
        let wt = tmp.path().join("linked-wt");
        std::fs::create_dir_all(&repo).unwrap();
        crate::test_helpers::init_git_repo(&repo);
        crate::test_helpers::add_worktree(&repo, &wt);

        // Primary checkout → None.
        assert!(
            linked_worktree_primary(&repo).is_none(),
            "primary checkout must not be flagged as a linked worktree"
        );
        // Linked worktree → Some(primary root).
        let primary = linked_worktree_primary(&wt).expect("linked worktree must be detected");
        assert_eq!(
            primary.canonicalize().unwrap(),
            repo.canonicalize().unwrap(),
            "must resolve to the primary repo root"
        );
    }

    #[test]
    fn linked_worktree_primary_none_for_non_git() {
        let tmp = tempfile::tempdir().unwrap();
        assert!(
            linked_worktree_primary(tmp.path()).is_none(),
            "a non-git directory is not a linked worktree"
        );
    }

    #[test]
    fn parse_github_org_ssh() {
        assert_eq!(
            parse_github_org("git@github.com:myorg/myrepo.git"),
            Some("myorg".into())
        );
    }

    #[test]
    fn parse_github_org_https() {
        assert_eq!(
            parse_github_org("https://github.com/myorg/myrepo"),
            Some("myorg".into())
        );
    }

    #[test]
    fn parse_github_org_non_github_returns_none() {
        assert_eq!(parse_github_org("https://gitlab.com/org/repo"), None);
    }

    #[test]
    fn parse_github_org_invalid() {
        assert_eq!(parse_github_org("not-a-url"), None);
    }

    #[test]
    fn merge_hooks_into_empty_settings_adds_all_our_events() {
        let mut settings = serde_json::json!({});
        let ours = tracevault_hooks();
        merge_hooks(&mut settings, &ours, Path::new("settings.json")).unwrap();

        let hooks = settings.get("hooks").unwrap();
        for event in [
            "PreToolUse",
            "PostToolUse",
            "Notification",
            "Stop",
            "SessionStart",
            "UserPromptSubmit",
        ] {
            assert!(hooks.get(event).is_some(), "missing event {event}");
        }
    }

    #[test]
    fn merge_hooks_preserves_unrelated_user_hook_and_keys() {
        let mut settings = serde_json::json!({
            "model": "opus",
            "hooks": {
                "PreToolUse": [{
                    "matcher": "",
                    "hooks": [{ "type": "command", "command": "my-own-hook" }]
                }]
            }
        });
        let ours = tracevault_hooks();
        merge_hooks(&mut settings, &ours, Path::new("settings.json")).unwrap();

        // Other top-level keys preserved.
        assert_eq!(settings.get("model").unwrap(), "opus");

        let pre_tool_use = settings["hooks"]["PreToolUse"].as_array().unwrap();
        // Both the user's own hook and ours are present.
        assert_eq!(pre_tool_use.len(), 2);
        let commands: Vec<&str> = pre_tool_use
            .iter()
            .map(|e| entry_command(e).unwrap())
            .collect();
        assert!(commands.contains(&"my-own-hook"));
        assert!(commands.contains(&"tracevault stream --event pre-tool-use"));

        // Other events were added fresh.
        assert!(settings["hooks"]["SessionStart"].is_array());
    }

    #[test]
    fn merge_hooks_is_idempotent() {
        let mut settings = serde_json::json!({});
        let ours = tracevault_hooks();
        merge_hooks(&mut settings, &ours, Path::new("settings.json")).unwrap();
        merge_hooks(&mut settings, &ours, Path::new("settings.json")).unwrap();

        for event in [
            "PreToolUse",
            "PostToolUse",
            "Notification",
            "Stop",
            "SessionStart",
            "UserPromptSubmit",
        ] {
            let arr = settings["hooks"][event].as_array().unwrap();
            assert_eq!(arr.len(), 1, "event {event} should not be duplicated");
        }
    }

    #[test]
    fn merge_hooks_dedupes_command_found_in_non_first_inner_hook() {
        // The existing PreToolUse entry has our command at hooks[1], not
        // hooks[0] — a naive `entry_command` (hooks[0] only) comparison would
        // miss it and append a duplicate entry.
        let mut settings = serde_json::json!({
            "hooks": {
                "PreToolUse": [{
                    "matcher": "Write|Edit|Bash",
                    "hooks": [
                        { "type": "command", "command": "other" },
                        { "type": "command", "command": "tracevault stream --event pre-tool-use" }
                    ]
                }]
            }
        });
        let ours = tracevault_hooks();
        merge_hooks(&mut settings, &ours, Path::new("settings.json")).unwrap();

        let pre_tool_use = settings["hooks"]["PreToolUse"].as_array().unwrap();
        assert_eq!(
            pre_tool_use.len(),
            1,
            "must not append a duplicate PreToolUse entry when our command is present at hooks[1]"
        );
    }

    #[test]
    fn merge_hooks_errors_on_non_object_top_level() {
        let mut settings = serde_json::json!([1, 2, 3]);
        let ours = tracevault_hooks();
        let err = merge_hooks(&mut settings, &ours, Path::new("settings.json")).unwrap_err();
        assert_eq!(err.kind(), io::ErrorKind::InvalidData);
        assert!(err.to_string().contains("settings.json"));
    }

    #[test]
    fn merge_hooks_errors_on_non_object_top_level_number() {
        let mut settings = serde_json::json!(42);
        let ours = tracevault_hooks();
        let err = merge_hooks(&mut settings, &ours, Path::new("settings.json")).unwrap_err();
        assert_eq!(err.kind(), io::ErrorKind::InvalidData);
    }

    #[test]
    fn merge_hooks_errors_on_non_object_hooks() {
        let mut settings = serde_json::json!({ "hooks": "not-an-object" });
        let ours = tracevault_hooks();
        let err = merge_hooks(&mut settings, &ours, Path::new("settings.json")).unwrap_err();
        assert_eq!(err.kind(), io::ErrorKind::InvalidData);
        assert!(err.to_string().contains("hooks"));
    }

    #[test]
    fn merge_hooks_errors_on_non_array_event_value() {
        let mut settings = serde_json::json!({
            "hooks": {
                "PreToolUse": "not-an-array"
            }
        });
        let ours = tracevault_hooks();
        let err = merge_hooks(&mut settings, &ours, Path::new("settings.json")).unwrap_err();
        assert_eq!(err.kind(), io::ErrorKind::InvalidData);
        assert!(err.to_string().contains("PreToolUse"));
    }

    #[test]
    fn install_global_hooks_writes_settings_and_claude_md() {
        let tmp = tempfile::tempdir().unwrap();
        let claude_dir = tmp.path().join(".claude");

        install_global_hooks(&claude_dir).unwrap();

        let settings_content = fs::read_to_string(claude_dir.join("settings.json")).unwrap();
        let settings: serde_json::Value = serde_json::from_str(&settings_content).unwrap();
        for event in ["SessionStart", "UserPromptSubmit", "PreToolUse"] {
            assert!(settings["hooks"].get(event).is_some(), "missing {event}");
        }

        let claude_md = fs::read_to_string(claude_dir.join("CLAUDE.md")).unwrap();
        assert!(claude_md.contains(GLOBAL_CLAUDE_MD_MARKER));
    }

    #[test]
    fn install_global_hooks_is_idempotent() {
        let tmp = tempfile::tempdir().unwrap();
        let claude_dir = tmp.path().join(".claude");

        install_global_hooks(&claude_dir).unwrap();
        install_global_hooks(&claude_dir).unwrap();

        let settings_content = fs::read_to_string(claude_dir.join("settings.json")).unwrap();
        let settings: serde_json::Value = serde_json::from_str(&settings_content).unwrap();
        for event in [
            "PreToolUse",
            "PostToolUse",
            "Notification",
            "Stop",
            "SessionStart",
            "UserPromptSubmit",
        ] {
            let arr = settings["hooks"][event].as_array().unwrap();
            assert_eq!(arr.len(), 1, "event {event} should not be duplicated");
        }

        let claude_md = fs::read_to_string(claude_dir.join("CLAUDE.md")).unwrap();
        let marker_count = claude_md.matches(GLOBAL_CLAUDE_MD_MARKER).count();
        assert_eq!(
            marker_count, 1,
            "CLAUDE.md marker should appear exactly once"
        );
    }

    #[test]
    fn install_global_hooks_preserves_existing_claude_md_content() {
        let tmp = tempfile::tempdir().unwrap();
        let claude_dir = tmp.path().join(".claude");
        fs::create_dir_all(&claude_dir).unwrap();
        fs::write(
            claude_dir.join("CLAUDE.md"),
            "# My existing notes\nSome content.\n",
        )
        .unwrap();

        install_global_hooks(&claude_dir).unwrap();

        let claude_md = fs::read_to_string(claude_dir.join("CLAUDE.md")).unwrap();
        assert!(claude_md.contains("# My existing notes"));
        assert!(claude_md.contains(GLOBAL_CLAUDE_MD_MARKER));
    }

    #[test]
    fn install_global_hooks_settings_write_is_atomic_no_tmp_leftover() {
        let tmp = tempfile::tempdir().unwrap();
        let claude_dir = tmp.path().join(".claude");

        install_global_hooks(&claude_dir).unwrap();

        let settings_content = fs::read_to_string(claude_dir.join("settings.json")).unwrap();
        let settings: serde_json::Value = serde_json::from_str(&settings_content).unwrap();
        assert!(settings["hooks"].get("SessionStart").is_some());

        let leftover = fs::read_dir(&claude_dir)
            .unwrap()
            .filter_map(|e| e.ok())
            .any(|e| e.file_name().to_string_lossy().ends_with(".tmp"));
        assert!(
            !leftover,
            "no settings.json.tmp should remain after install"
        );
    }

    #[test]
    fn install_global_hooks_errors_on_non_object_settings_and_does_not_overwrite() {
        let tmp = tempfile::tempdir().unwrap();
        let claude_dir = tmp.path().join(".claude");
        fs::create_dir_all(&claude_dir).unwrap();
        let settings_path = claude_dir.join("settings.json");
        let original = "[1, 2, 3]";
        fs::write(&settings_path, original).unwrap();

        let err = install_global_hooks(&claude_dir).unwrap_err();
        assert_eq!(err.kind(), io::ErrorKind::InvalidData);

        // The original (malformed-for-our-purposes but validly-JSON) file
        // must be untouched, and no stray tmp file left behind.
        let after = fs::read_to_string(&settings_path).unwrap();
        assert_eq!(after, original);
        let leftover = fs::read_dir(&claude_dir)
            .unwrap()
            .filter_map(|e| e.ok())
            .any(|e| e.file_name().to_string_lossy().ends_with(".tmp"));
        assert!(!leftover, "no tmp file should be left behind on error");
    }

    #[test]
    fn install_global_hooks_errors_on_non_object_hooks_key_and_does_not_overwrite() {
        let tmp = tempfile::tempdir().unwrap();
        let claude_dir = tmp.path().join(".claude");
        fs::create_dir_all(&claude_dir).unwrap();
        let settings_path = claude_dir.join("settings.json");
        let original = r#"{"hooks": "oops"}"#;
        fs::write(&settings_path, original).unwrap();

        let err = install_global_hooks(&claude_dir).unwrap_err();
        assert_eq!(err.kind(), io::ErrorKind::InvalidData);

        let after = fs::read_to_string(&settings_path).unwrap();
        assert_eq!(after, original);
    }

    #[test]
    fn install_global_hooks_errors_on_non_array_event_and_does_not_overwrite() {
        let tmp = tempfile::tempdir().unwrap();
        let claude_dir = tmp.path().join(".claude");
        fs::create_dir_all(&claude_dir).unwrap();
        let settings_path = claude_dir.join("settings.json");
        let original = r#"{"hooks": {"PreToolUse": "oops"}}"#;
        fs::write(&settings_path, original).unwrap();

        let err = install_global_hooks(&claude_dir).unwrap_err();
        assert_eq!(err.kind(), io::ErrorKind::InvalidData);

        let after = fs::read_to_string(&settings_path).unwrap();
        assert_eq!(after, original);
    }

    #[test]
    fn safe_join_allows_direct_child_file() {
        let tmp = tempfile::tempdir().unwrap();
        let base = tmp.path();
        let result = safe_join(base, "settings.json").unwrap();
        assert_eq!(result.parent(), Some(base));
        assert_eq!(result.file_name().unwrap(), "settings.json");
    }

    #[test]
    fn safe_join_rejects_path_traversal() {
        let tmp = tempfile::tempdir().unwrap();
        let base = tmp.path();
        let result = safe_join(base, "../evil");
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert_eq!(err.kind(), io::ErrorKind::InvalidInput);
        assert!(err.to_string().contains("refusing to access path outside"));
    }

    #[test]
    fn safe_join_rejects_absolute_path() {
        let tmp = tempfile::tempdir().unwrap();
        let base = tmp.path();
        let result = safe_join(base, "/etc/passwd");
        // An absolute path joined to a base will replace the base,
        // so parent will not equal base.
        assert!(result.is_err());
    }

    #[test]
    fn write_global_user_config_enables_and_creates_context() {
        let root = tempfile::tempdir().unwrap();
        let active = write_global_user_config_in(root.path(), None).unwrap();
        let cfg = crate::config::try_load_user_config_in(root.path())
            .unwrap()
            .unwrap();
        // Validate against the INJECTED root (resolve_in), not tv_config_root():
        // a first-run default enables at the `context.json` under `root`.
        assert_eq!(
            cfg.user_context.unwrap().resolve_in(root.path()),
            Some(crate::config::default_user_context_path_in(root.path()))
        );
        let ctx = crate::config::default_user_context_path_in(root.path());
        assert_eq!(active, Some(ctx.clone()));
        assert!(ctx.exists());
    }

    #[test]
    fn write_global_user_config_can_disable() {
        let root = tempfile::tempdir().unwrap();
        let active = write_global_user_config_in(
            root.path(),
            Some(crate::config::UserContext::Toggle(false)),
        )
        .unwrap();
        let cfg = crate::config::try_load_user_config_in(root.path())
            .unwrap()
            .unwrap();
        assert!(matches!(
            cfg.user_context,
            Some(crate::config::UserContext::Toggle(false))
        ));
        assert_eq!(active, None, "disabled → no active context path");
        // A disabled layer must NOT seed a context file.
        assert!(
            !crate::config::default_user_context_path_in(root.path()).exists(),
            "disabled user context must not create a context.json"
        );
    }

    #[test]
    fn write_global_user_config_custom_path_seeds_that_file_only() {
        let root = tempfile::tempdir().unwrap();
        let custom = root.path().join("custom-ctx.json");
        let active = write_global_user_config_in(
            root.path(),
            Some(crate::config::UserContext::Path(
                custom.to_string_lossy().into_owned(),
            )),
        )
        .unwrap();
        assert_eq!(active, Some(custom.clone()));
        assert!(custom.exists(), "the configured custom path must be seeded");
        assert!(
            !crate::config::default_user_context_path_in(root.path()).exists(),
            "the default context.json must NOT be created when a custom path is configured"
        );
    }

    #[test]
    fn write_global_user_config_preserves_existing_on_rerun_without_flags() {
        let root = tempfile::tempdir().unwrap();
        let custom = root.path().join("custom-ctx.json");
        // First run pins a custom path.
        write_global_user_config_in(
            root.path(),
            Some(crate::config::UserContext::Path(
                custom.to_string_lossy().into_owned(),
            )),
        )
        .unwrap();
        // Re-run with NO flags must NOT reset it back to the default.
        let active = write_global_user_config_in(root.path(), None).unwrap();
        assert_eq!(
            active,
            Some(custom.clone()),
            "re-run without flags must preserve the custom path"
        );
        let cfg = crate::config::try_load_user_config_in(root.path())
            .unwrap()
            .unwrap();
        assert_eq!(cfg.user_context.unwrap().path_in(root.path()), custom);
    }
}