reliary-agent 0.8.8

Grammar-free symbol intelligence — CLI and MCP server.
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
use std::io::{self, Write};
use std::path::PathBuf;
use std::fs;
use std::process::Command;
use serde_json::Value;

fn ok(msg: &str) { println!("  \x1b[32m✓\x1b[0m {}", msg); }

// Embed gate.js at compile time

/// Atomic write: write to tmp, sync, rename. Prevents partial write corruption.
/// Bug 47: clean up tmp file on write or rename failure (was leaking).
fn atomic_write(path: &str, content: &str) -> bool {
    let tmp = format!("{}.tmp.{}", path, std::process::id());
    let write_ok = std::fs::write(&tmp, content).is_ok();
    if !write_ok {
        let _ = std::fs::remove_file(&tmp); // clean up partial write
        return false;
    }
    let rename_ok = std::fs::rename(&tmp, path).is_ok();
    if !rename_ok {
        let _ = std::fs::remove_file(&tmp); // clean up orphaned tmp
        return false;
    }
    true
}
const EMBEDDED_GATE_JS: &str = include_str!("../pi/gate.js");

fn ask_yes_no(prompt: &str, default: bool) -> bool {
    let def_str = if default { "[Y/n]" } else { "[y/N]" };
    print!("{} {}: ", prompt, def_str);
    let _ = io::stdout().flush();

    let mut input = String::new();
    if io::stdin().read_line(&mut input).is_ok() {  // GUARDED: intentional
        let input = input.trim().to_lowercase();
        if input.is_empty() {
            return default;
        }
        return input == "y" || input == "yes";
    }
    default
}

/// Print a dry-run action and return true to skip the actual work.
fn dry_run_action(label: &str) -> bool {
    println!("  \x1b[36m[dry-run]\x1b[0m would {}", label);
    true
}

fn home_dir() -> Option<PathBuf> {
    if let Some(p) = test_home_override() { return Some(p); }
    dirs::home_dir()
}

fn test_home_override() -> Option<PathBuf> {
    TEST_HOME_OVERRIDE.with(|cell| cell.borrow().clone())
}

thread_local! {
    static TEST_HOME_OVERRIDE: std::cell::RefCell<Option<PathBuf>> = const { std::cell::RefCell::new(None) };
}

/// Test-only: override `home_dir()` for the current thread. Returns a guard
/// that restores the previous value on drop.
#[cfg(test)]
pub(crate) fn set_test_home(path: PathBuf) -> TestHomeGuard {
    let prev = TEST_HOME_OVERRIDE.with(|cell| cell.borrow().clone());
    TEST_HOME_OVERRIDE.with(|cell| *cell.borrow_mut() = Some(path));
    TestHomeGuard { prev }
}

#[cfg(test)]
pub(crate) struct TestHomeGuard {
    prev: Option<PathBuf>,
}

#[cfg(test)]
impl Drop for TestHomeGuard {
    fn drop(&mut self) {
        TEST_HOME_OVERRIDE.with(|cell| *cell.borrow_mut() = self.prev.clone());
    }
}

fn get_data_dir() -> Option<PathBuf> {
    #[cfg(target_os = "windows")]
    {
        dirs::data_local_dir()
    }
    #[cfg(not(target_os = "windows"))]
    {
        home_dir().map(|h| h.join(".local/share"))
    }
}

/// Path of the short-alias shim that the `--help` text advertises.
fn rel_shim_path() -> Option<PathBuf> {
    home_dir().map(|h| {
        h.join(".local/bin")
            .join(if cfg!(windows) { "rel.cmd" } else { "rel" })
    })
}

/// Install the `rel` short alias so the hint in `--help` is true rather than
/// aspirational. A symlink to the running binary on Unix; a tiny `.cmd`
/// wrapper on Windows (non-symlink is the safe default there). Best-effort:
/// a failure is reported but never aborts `init`.
fn install_rel_shim(dry_run: bool) -> bool {
    let Some(shim) = rel_shim_path() else { return false };
    let exe = std::env::current_exe().unwrap_or_else(|_| PathBuf::from("reliary"));

    if dry_run {
        dry_run_action(&format!("install 'rel' shim → {}", shim.display()));
        return true;
    }

    let Some(parent) = shim.parent() else { return false };
    if fs::create_dir_all(parent).is_err() {
        return false;
    }
    // Replace any existing shim so a stale or wrong target cannot survive.
    let _ = fs::remove_file(&shim);

    #[cfg(unix)]
    let created = std::os::unix::fs::symlink(&exe, &shim).is_ok();

    #[cfg(windows)]
    let created = fs::write(&shim, format!("@echo off\r\n\"{}\" %*\r\n", exe.display())).is_ok();

    if created {
        ok(&format!("Installed 'rel' alias → {}", shim.display()));
        if !path_contains(parent) {
            println!(
                "  \x1b[33m!\x1b[0m {} is not on your PATH — add it to use 'rel'",
                parent.display()
            );
        }
    }
    created
}

/// Whether `dir` appears in `PATH`.
fn path_contains(dir: &std::path::Path) -> bool {
    std::env::var("PATH")
        .map(|p| std::env::split_paths(&p).any(|d| d == dir))
        .unwrap_or(false)
}

/// Remove the short-alias shim. Symmetric with [`install_rel_shim`].
fn remove_rel_shim() {
    if let Some(shim) = rel_shim_path() {
        // A dangling symlink has no regular-file metadata, so check the link
        // itself too, or we would leave a broken `rel` behind.
        if (shim.exists() || shim.symlink_metadata().is_ok())
            && fs::remove_file(&shim).is_ok()
        {
            ok("Removed 'rel' alias");
        }
    }
}

pub fn run(dry_run: bool) {
    let bold = "\x1b[1m";
    let dim = "\x1b[2m";
    let reset = "\x1b[0m";

    println!();
    println!("{}  ╭────────────────────────────────────────────╮{}", bold, reset);
    println!("{}  │       Reliary Agent Setup Wizard           │{}", bold, reset);
    println!("{}  ╰────────────────────────────────────────────╯{}", bold, reset);
    println!();
    println!("{}  This will configure reliary for your code agents.{}", dim, reset);
    println!("{}  Each integration is optional -- say n to skip any.{}", dim, reset);
    if dry_run {
        println!("{}  \x1b[36m[dry-run]\x1b[0m{} Showing what would be installed, no changes will be made.", dim, reset);
    }
    println!();

    let mut configured_agents = 0;

    // 1. Pi Agent
    let pi_bin = home_dir().map(|h| h.join(".local/bin/pi")).unwrap_or_else(|| PathBuf::from("pi"));
    let has_pi = pi_bin.exists() || std::env::var("PATH").map(|p| {
        p.split(':').any(|dir| std::path::Path::new(dir).join("pi").exists())
    }).unwrap_or(false);
    
    if has_pi {
        // Idempotency: detect if gate.js already installed
        let gate_exists = home_dir()
            .map(|h| h.join(".local/share/reliary/gate.js"))
            .map(|p| p.exists())
            .unwrap_or(false);
        let msg = if gate_exists {
            "Found Pi Agent + existing gate.js installation. Re-install?"
        } else {
            "Found Pi Agent. Install Reliary extension?"
        };
        if dry_run { dry_run_action("install Pi Agent gate.js"); } else if ask_yes_no(msg, true) {
            if let Some(data_dir) = get_data_dir() {
                let target_dir = data_dir.join("reliary");
                if fs::create_dir_all(&target_dir).is_ok() {
                    let target_path = target_dir.join("gate.js");
                    let content = EMBEDDED_GATE_JS.as_bytes();
                    let tmp = format!("{}.tmp.{}", target_path.display(), std::process::id());
                    if std::fs::write(&tmp, content).is_ok()
                        && std::fs::rename(&tmp, &target_path).is_ok()
                    {
                        let pi_cmd = if pi_bin.exists() { pi_bin.to_str().unwrap_or("pi") } else { "pi" };
                        let status = Command::new(pi_cmd)
                            .args(["install", target_path.to_str().unwrap_or("/dev/null")])
                            .output();
                        
                        if let Ok(output) = status {
                            if output.status.success() {
                                ok("Installed gate.js");
                                configured_agents += 1;
                            } else {
                                println!("  \x1b[31m✗\x1b[0m Failed to run `pi install`\n");
                            }
                        } else {
                            println!("  \x1b[31m✗\x1b[0m Failed to run `pi install`\n");
                        }
                    } else {
                        println!("  \x1b[31m✗\x1b[0m Failed to write gate.js\n");
                    }
                } else {
                    println!("  \x1b[31m✗\x1b[0m Failed to create directory {:?}\n", target_dir);
                }
            } else {
                println!("  \x1b[31m✗\x1b[0m Could not determine data directory\n");
            }
        } else {
            println!("  \x1b[33m-\x1b[0m Skipped\n");
        }
    }

    // 2. Claude Code
    if let Some(home) = home_dir() {
        let claude_cfg = home.join(".claude.json");
        let claude_hooks_dir = home.join(".claude/hooks");
        if claude_cfg.exists() {
            if dry_run { dry_run_action("add Reliary MCP server to Claude Code (~/.claude.json)"); } else if ask_yes_no("Found Claude Code config. Add Reliary MCP server?", true) {
                    if inject_mcp_server(&claude_cfg, "reliary", "mcpServers") {
                        ok("Updated ~/.claude.json");
                        configured_agents += 1;
                    } else {
                        println!("  \x1b[31m✗\x1b[0m Failed to update ~/.claude.json\n");
                    }
            } else {
                println!("  \x1b[33m-\x1b[0m Skipped\n");
            }
            // Install Claude Code hooks (session reminder + bash sift)
            if dry_run { dry_run_action("install Claude Code hooks (~/.claude/hooks/reliary-*)"); } else if ask_yes_no("Install Claude Code hooks? (session reminder + bash output compression)", true) {
                if install_claude_hooks(&claude_hooks_dir) {
                    ok("Installed Claude Code hooks (~/.claude/hooks/reliary-*)");
                } else {
                    println!("  \x1b[31m✗\x1b[0m Failed to install hooks\n");
                }
            }
        }
    }

    // 3. OpenCode
    if let Some(home) = home_dir() {
        let opencode_cfg = if cfg!(target_os = "windows") {
            dirs::config_dir().map(|d| d.join("opencode").join("opencode.json"))
        } else if cfg!(target_os = "macos") {
            Some(home.join("Library/Application Support/opencode/opencode.json"))
        } else {
            Some(home.join(".config/opencode/opencode.json"))
        };

        if let Some(cfg_path) = opencode_cfg {
            if cfg_path.exists() {
                if dry_run { dry_run_action("add Reliary MCP server to OpenCode (~/.config/opencode/opencode.json)"); } else if ask_yes_no("Found OpenCode config. Add Reliary MCP server?", true) {
                    if inject_mcp_server(&cfg_path, "reliary", "mcp") {
                        ok("Updated opencode.json");
                        configured_agents += 1;
                    } else {
                        println!("  \x1b[31m✗\x1b[0m Failed to update opencode.json\n");
                    }
                } else {
                    println!("  \x1b[33m-\x1b[0m Skipped\n");
                }

                // Offer to install the OpenCode plugin (regen-on-edit hook)
                if dry_run { dry_run_action("install reliary-opencode plugin in opencode.json"); } else if ask_yes_no("Install reliary-opencode plugin? (auto-reindex + regen pack after every write/edit)", true) {
                    let exe_path = std::env::current_exe().unwrap_or_else(|_| std::path::PathBuf::from("reliary"));
                    match inject_opencode_plugin(&cfg_path, &exe_path) {
                        Ok(true) => ok("Installed reliary-opencode plugin in opencode.json"),
                        Ok(false) => println!("  \x1b[33m-\x1b[0m Plugin already present, skipped\n"),
                        Err(e) => println!("  \x1b[31m✗\x1b[0m Plugin install failed: {}\n", e),
                    }
                } else {
                    println!("  \x1b[33m-\x1b[0m Plugin install skipped\n");
                }
            }
        }
    }
    
    // 4. Cline
    if let Some(home) = home_dir() {
        let cline_cfg = if cfg!(target_os = "windows") {
            dirs::data_dir().map(|d| d.join("Code").join("User").join("globalStorage").join("rooveterinery.cline").join("cline_mcp_settings.json"))
        } else if cfg!(target_os = "macos") {
            Some(home.join("Library/Application Support/Code/User/globalStorage/rooveterinery.cline/cline_mcp_settings.json"))
        } else {
            Some(home.join(".config/Code/User/globalStorage/rooveterinery.cline/cline_mcp_settings.json"))
        };

        if let Some(cfg_path) = cline_cfg {
            if cfg_path.exists() {
                if dry_run { dry_run_action("add Reliary MCP server to Cline"); } else if ask_yes_no("Found Cline config. Add Reliary MCP server?", true) {
                    if inject_mcp_server(&cfg_path, "reliary", "mcpServers") {
                        ok("Updated cline MCP settings");
                        configured_agents += 1;
                    } else {
                        println!("  \x1b[31m✗\x1b[0m Failed to update cline_mcp_settings.json\n");
                    }
                } else {
                    println!("  \x1b[33m-\x1b[0m Skipped\n");
                }
            }
        }
    }

    if configured_agents == 0 {
        println!("  {} No agents were configured. You can run `reliary init` again later.", dim);
    }

    // Short alias 'rel', advertised in `reliary --help`.
    if dry_run {
        dry_run_action("install 'rel' shim");
    } else if ask_yes_no("Install the short 'rel' command alias?", true) {
        install_rel_shim(false);
    } else {
        println!("  \x1b[33m-\x1b[0m Skipped\n");
    }

    // ── Summary ──
    let dim = "\x1b[2m";
    let reset = "\x1b[0m";
    println!();
    println!("{}  ╭────────────────────────────────────────────╮{}", bold, reset);
    if configured_agents > 0 {
        println!("{}  │   {} agent(s) configured.       ✓          │{}", dim, configured_agents, reset);
    }
    println!("{}  │   Next: {}reliary doctor{}              │{}", dim, bold, dim, reset);
    println!("{}  │   Then: {}reliary trust .{}               │{}", dim, bold, dim, reset);
    println!("{}  ╰────────────────────────────────────────────╯{}", bold, reset);
    println!();
}

/// Install Claude Code hook scripts (SessionStart reminder + Bash sift).
fn install_claude_hooks(hooks_dir: &PathBuf) -> bool {
    if let Err(e) = fs::create_dir_all(hooks_dir) {
        eprintln!("  Failed to create hooks dir: {}", e);
        return false;
    }
    // Embed hook scripts at compile time
    const REMINDER_SCRIPT: &str = include_str!("../hooks/claude-session-reminder.sh");
    // V14: also install the sift pretooluse hook for bash auto-rewrite (RTK parity).
    const SIFT_PRETOOLUSE: &str = include_str!("../hooks/claude-pretooluse.sh");
    let reminder_path = hooks_dir.join("reliary-session-reminder");
    let sift_path = hooks_dir.join("reliary-sift-pretooluse");
    if let Err(e) = fs::write(&reminder_path, REMINDER_SCRIPT) {
        eprintln!("  Failed to write reminder hook: {}", e);
        return false;
    }
    if let Err(e) = fs::write(&sift_path, SIFT_PRETOOLUSE) {
        eprintln!("  Failed to write sift pretooluse hook: {}", e);
        return false;
    }
    // Make executable
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        for p in [&reminder_path, &sift_path] {
            if let Ok(meta) = fs::metadata(p) {
                let mut perms = meta.permissions();
                perms.set_mode(0o755);
                let _ = fs::set_permissions(p, perms);
            }
        }
    }
    // Register both hooks in ~/.claude/settings.json so they fire automatically.
    // V61: register_claude_hooks returns false when settings.json is malformed —
    // init must not report success with a dead hook.
    if !register_claude_hooks() {
        eprintln!("\u{26A0}\u{FE0F} Failed to register hooks in ~/.claude/settings.json (malformed JSON?)");
        return false;
    }
    true
}

/// Shape of one hook entry to register: event, matcher, command.
struct HookSpec {
    event: &'static str,
    matcher: &'static str,
    command: &'static str,
}

const CLAUDE_HOOKS: &[HookSpec] = &[
    HookSpec {
        event: "PreToolUse",
        matcher: "Bash",
        command: "~/.claude/hooks/reliary-sift-pretooluse",
    },
    HookSpec {
        event: "SessionStart",
        matcher: "startup|resume|clear|compact",
        command: "~/.claude/hooks/reliary-session-reminder",
    },
];

/// V14: Inject the reliary hooks into ~/.claude/settings.json. Idempotent.
/// Generalised from the sift-only version so install and uninstall stay
/// symmetric for every entry this function can add.
fn register_claude_hooks() -> bool {
    let home = match dirs::home_dir() {
        Some(h) => h,
        None => return false,
    };
    let settings_path = home.join(".claude/settings.json");
    let mut v: serde_json::Value = match fs::read_to_string(&settings_path) {
        Ok(c) => match serde_json::from_str(&c) {
            Ok(v) => v,
            Err(_) => return false,
        },
        Err(_) => {
            // File doesn't exist — create it with just the hooks.
            serde_json::json!({ "hooks": {} })
        }
    };

    let hooks = v
        .as_object_mut()
        .map(|obj| obj.entry("hooks").or_insert(serde_json::json!({})));
    let Some(hooks) = hooks.and_then(|h| h.as_object_mut()) else {
        return false;
    };

    for spec in CLAUDE_HOOKS {
        let already = hooks
            .get(spec.event)
            .and_then(|e| e.as_array())
            .map(|arr| {
                arr.iter().any(|entry| {
                    entry.get("matcher").and_then(|m| m.as_str()) == Some(spec.matcher)
                        && entry
                            .get("hooks")
                            .and_then(|h| h.as_array())
                            .map(|hs| {
                                hs.iter().any(|h| {
                                    h.get("command")
                                        .and_then(|c| c.as_str())
                                        .map(|c| c == spec.command)
                                        .unwrap_or(false)
                                })
                            })
                            .unwrap_or(false)
                })
            })
            .unwrap_or(false);
        if already {
            continue;
        }
        let arr = hooks
            .entry(spec.event)
            .or_insert(serde_json::json!([]));
        if let Some(arr) = arr.as_array_mut() {
            arr.push(serde_json::json!({
                "matcher": spec.matcher,
                "hooks": [{ "type": "command", "command": spec.command }]
            }));
        }
    }

    match serde_json::to_string_pretty(&v) {
        Ok(new_content) => atomic_write(&settings_path.to_string_lossy(), &new_content),
        Err(_) => false,
    }
}

fn inject_mcp_server(cfg_path: &PathBuf, server_name: &str, mcp_key: &str) -> bool {
    let content = match fs::read_to_string(cfg_path) {
        Ok(c) => c,
        Err(_) => return false,
    };

    let mut v: Value = match serde_json::from_str(&content) {
        Ok(v) => v,
        Err(_) => return false,
    };

    if let Some(obj) = v.as_object_mut() {
        let mcp_servers = obj.entry(mcp_key).or_insert(serde_json::json!({}));
        if let Some(servers) = mcp_servers.as_object_mut() {
            let exe_path = std::env::current_exe().unwrap_or_else(|_| PathBuf::from("reliary-agent"));
            let exe_str = exe_path.to_string_lossy().to_string();

            servers.insert(server_name.to_string(), serde_json::json!({
                "command": exe_str,
                "args": ["mcp"]
            }));

            if let Ok(new_content) = serde_json::to_string_pretty(&v) {
                return atomic_write(&cfg_path.to_string_lossy(), &new_content);
            }
        }
    }
    false
}

/// Inject the reliary-opencode-plugin entry into opencode.json's "plugin" array.
///
/// Locates the plugin source relative to the running binary (sibling directory at
/// `<install_root>/../../opencode-plugin`), checks it's built (dist/index.js exists),
/// and appends the absolute path to the config's "plugin" array. Preserves existing
/// entries; removes any prior `@reliary/opencode` (the deprecated v0.x plugin) entry.
///
/// Returns:
///   - Ok(true) if the plugin was added (or already present)
///   - Ok(false) if the user declined the prompt or the plugin source wasn't found
///   - Err on filesystem/parse failure
fn inject_opencode_plugin(cfg_path: &PathBuf, exe_path: &std::path::Path) -> Result<bool, String> {
    // Locate the plugin dist/. Binary layout:
    //   <repo>/target/release/reliary
    //   <repo>/opencode-plugin/dist/index.js
    // The exe is at `<repo>/target/release/reliary`; ancestors[3] gives `<repo>`.
    let repo_root = exe_path
        .ancestors()
        .nth(3)
        .ok_or("cannot determine repo root")?;
    let plugin_src = repo_root.join("opencode-plugin");
    let plugin_dist = plugin_src.join("dist").join("index.js");

    if !plugin_dist.exists() {
        return Err(format!(
            "plugin not built: {}\n  Run `cd {} && npm install && npm run build` to build it.",
            plugin_dist.display(),
            plugin_src.display()
        ));
    }
    let plugin_path = plugin_dist.canonicalize()
        .map_err(|e| format!("canonicalize {}: {}", plugin_dist.display(), e))?
        .to_string_lossy().to_string();

    let content = fs::read_to_string(cfg_path)
        .map_err(|e| format!("read {}: {}", cfg_path.display(), e))?;
    let mut v: Value = serde_json::from_str(&content)
        .map_err(|e| format!("parse {}: {}", cfg_path.display(), e))?;

    if let Some(obj) = v.as_object_mut() {
        let plugin_arr = obj.entry("plugin").or_insert(serde_json::json!([]));
        if let Some(arr) = plugin_arr.as_array_mut() {
            // Drop any deprecated entry (the old `@reliary/opencode` v0.x plugin)
            arr.retain(|v| {
                let s = v.as_str().unwrap_or("");
                !s.contains("@reliary/opencode") || s.ends_with("opencode-plugin/dist/index.js")
            });
            // Idempotent: only add if not present
            let already = arr.iter().any(|v| {
                v.as_str().map(|s| s == plugin_path).unwrap_or(false)
            });
            if !already {
                arr.push(serde_json::Value::String(plugin_path.clone()));
            }
            if let Ok(new_content) = serde_json::to_string_pretty(&v) {
                if atomic_write(&cfg_path.to_string_lossy(), &new_content) {
                    return Ok(true);
                }
                return Err(format!("failed to write {}", cfg_path.display()));
            }
        }
    }
    Ok(false)
}

pub fn uninstall() {
    println!("\nReliary Uninstall");
    println!("-----------------");

    let mut removed_agents = 0;

    // 1. Pi Agent
    // Detect Pi by file existence only (no `pi --version` exec which can hang
    // on stdin or fail with wrong exit code).
    let pi_bin = home_dir().map(|h| h.join(".local/bin/pi")).unwrap_or_else(|| PathBuf::from("pi"));
    let pi_in_path = std::env::var("PATH").map(|p| {
        p.split(':').any(|dir| std::path::Path::new(dir).join("pi").exists())
    }).unwrap_or(false);
    let has_pi = pi_bin.exists() || pi_in_path;
    
    if has_pi {
        println!("Removing Pi Agent extension...");
        if let Some(data_dir) = get_data_dir() {
            let target_dir = data_dir.join("reliary");
            let target_path = target_dir.join("gate.js");
            
            if target_path.exists() {
                // Use 'pi -e' to remove the extension, or fall back to direct file removal.
                // The 'pi uninstall' subcommand may not exist in all Pi versions, so we
                // try the Pi command but always remove the file regardless of its result.
                let pi_cmd = if pi_bin.exists() { pi_bin.to_str().unwrap_or("pi") } else { "pi" };
                // Use proper command args instead of shell command to avoid quoting issues
                let _ = Command::new(pi_cmd)
                    .arg("-e")
                    .arg(format!("rm {}", target_path.to_str().unwrap_or("")))
                    .status();
                
                let _ = fs::remove_file(&target_path);
                
                // Attempt to remove directory if empty
                if let Ok(entries) = fs::read_dir(&target_dir) {
                    if entries.count() == 0 {
                        let _ = fs::remove_dir(&target_dir);
                    }
                }
                
                ok("Removed gate.js");
                removed_agents += 1;
            } else {
                println!("- gate.js not found\n");
            }
        }
    }

    // 2. Claude Code
    println!("Removing MCP integrations...");
    if let Some(home) = home_dir() {
        let claude_cfg = home.join(".claude.json");
        if claude_cfg.exists() && remove_mcp_server(&claude_cfg, "reliary", "mcpServers") {
            ok("Removed Reliary from Claude Code");
            removed_agents += 1;
        }
    }

    // 3. OpenCode
    if let Some(home) = home_dir() {
        let opencode_cfg = if cfg!(target_os = "windows") {
            dirs::config_dir().map(|d| d.join("opencode").join("opencode.json"))
        } else if cfg!(target_os = "macos") {
            Some(home.join("Library/Application Support/opencode/opencode.json"))
        } else {
            Some(home.join(".config/opencode/opencode.json"))
        };

        if let Some(cfg_path) = opencode_cfg {
            if cfg_path.exists()
                && remove_mcp_server(&cfg_path, "reliary", "mcp") {
                    ok("Removed Reliary from OpenCode");
                    removed_agents += 1;
                }
        }
    }
    
    // 4. Cline
    if let Some(home) = home_dir() {
        let cline_cfg = if cfg!(target_os = "windows") {
            dirs::data_dir().map(|d| d.join("Code").join("User").join("globalStorage").join("rooveterinery.cline").join("cline_mcp_settings.json"))
        } else if cfg!(target_os = "macos") {
            Some(home.join("Library/Application Support/Code/User/globalStorage/rooveterinery.cline/cline_mcp_settings.json"))
        } else {
            Some(home.join(".config/Code/User/globalStorage/rooveterinery.cline/cline_mcp_settings.json"))
        };

        if let Some(cfg_path) = cline_cfg {
            if cfg_path.exists() && remove_mcp_server(&cfg_path, "reliary", "mcpServers") {
                ok("Removed Reliary from Cline");
                removed_agents += 1;
            }
        }
    }

    // 5. Claude Code hooks
    println!("Removing Claude Code hooks...");
    if let Some(home) = home_dir() {
        // V61: also strip hook entries from ~/.claude/settings.json —
        // uninstall only removed the hook FILES before, leaving a broken
        // command reference that fires on every Bash call.
        remove_claude_hooks(&home);
        let hooks_dir = home.join(".claude/hooks");
        if hooks_dir.exists() {
            let reminder_path = hooks_dir.join("reliary-session-reminder");
            let sift_path = hooks_dir.join("reliary-sift-pretooluse");
            let mut hooks_removed = 0;
            for path in [reminder_path, sift_path] {
                if path.exists() {
                    let _ = fs::remove_file(&path);
                    hooks_removed += 1;
                }
            }
            // Legacy installs may still carry the removed code-gate file.
            let legacy_gate = hooks_dir.join("reliary-code-gate");
            if legacy_gate.exists() {
                let _ = fs::remove_file(&legacy_gate);
                hooks_removed += 1;
            }
            if hooks_removed > 0 {
                ok(&format!("Removed {} Claude Code hook(s)", hooks_removed));
                removed_agents += 1;
            }
        }
    }

    if removed_agents == 0 {
        println!("- No MCP integrations found or modified");
    }
    println!();

    // Short alias shim (mirror of install_rel_shim).
    remove_rel_shim();

    // 5. Config
    if ask_yes_no("Do you want to delete global configuration files? (~/.reliary)", false) {
        if let Some(home) = home_dir() {
            let config_dir = home.join(".reliary");
            if config_dir.exists() {
                if fs::remove_dir_all(&config_dir).is_ok() {
                    ok("Deleted ~/.reliary");
                } else {
                    println!("✗ Failed to delete ~/.reliary\n");
                }
            } else {
                println!("- ~/.reliary not found\n");
            }
        }
    } else {
        println!("- Skipped\n");
    }

    println!("Uninstall complete. You can now safely run `cargo uninstall reliary-agent`.");
}

/// V61: strip every reliary hook entry from ~/.claude/settings.json.
/// Mirror of register_claude_hooks — uninstall must remove what install
/// created (it previously only deleted the sift entry, leaving a broken
/// command reference that fired on every Bash tool call).
fn remove_claude_hooks(home: &std::path::Path) {
    let settings_path = home.join(".claude/settings.json");
    if !settings_path.exists() { return; }
    let content = match fs::read_to_string(&settings_path) {
        Ok(c) => c,
        Err(_) => return,
    };
    let mut v: serde_json::Value = match serde_json::from_str(&content) {
        Ok(v) => v,
        Err(_) => return,
    };
    let mut removed = false;
    if let Some(hooks) = v.get_mut("hooks").and_then(|h| h.as_object_mut()) {
        for spec in CLAUDE_HOOKS {
            let Some(arr) = hooks.get_mut(spec.event).and_then(|e| e.as_array_mut()) else {
                continue;
            };
            let before = arr.len();
            arr.retain(|entry| {
                !entry.get("hooks").and_then(|h| h.as_array())
                    .map(|hs| {
                        hs.iter().any(|h| {
                            h.get("command").and_then(|c| c.as_str())
                                .map(|c| c == spec.command)
                                .unwrap_or(false)
                        })
                    })
                    .unwrap_or(false)
            });
            if arr.len() != before { removed = true; }
        }
        // Drop emptied event arrays to keep settings clean.
        let empty: Vec<String> = hooks.iter()
            .filter(|(_, e)| e.as_array().map(|a| a.is_empty()).unwrap_or(false))
            .map(|(k, _)| k.clone())
            .collect();
        for k in empty { hooks.remove(&k); }
    }
    if !removed { return; }
    // Drop the hooks object itself when nothing is left.
    let hooks_empty = v.get("hooks")
        .and_then(|h| h.as_object())
        .map(|h| h.is_empty())
        .unwrap_or(false);
    if hooks_empty {
        if let Some(obj) = v.as_object_mut() { obj.remove("hooks"); }
    }
    if let Ok(new_content) = serde_json::to_string_pretty(&v) {
        let _ = atomic_write(&settings_path.to_string_lossy(), &new_content);
    }
}

fn remove_mcp_server(cfg_path: &PathBuf, server_name: &str, mcp_key: &str) -> bool {
    let content = match fs::read_to_string(cfg_path) {
        Ok(c) => c,
        Err(_) => return false,
    };
    
    let mut v: Value = match serde_json::from_str(&content) {
        Ok(v) => v,
        Err(_) => return false,
    };

    if let Some(obj) = v.as_object_mut() {
        if let Some(mcp_servers) = obj.get_mut(mcp_key).and_then(|m| m.as_object_mut()) {
            if mcp_servers.remove(server_name).is_some() {
                if let Ok(new_content) = serde_json::to_string_pretty(&v) {
                    return atomic_write(&cfg_path.to_string_lossy(), &new_content);
                }
            }
        }
    }
    false
}

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

        /// Serialize init tests — they share HOME and env vars which are process-global
        static INIT_TEST_LOCK: Mutex<()> = Mutex::new(());

        fn with_temp_home<F>(test: F)
    where
        F: FnOnce(PathBuf),
    {
        let _lock = INIT_TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());  // GUARDED: intentional — test serialization lock, must hold across HOME mutation
        use std::sync::atomic::{AtomicU32, Ordering};
        static COUNTER: AtomicU32 = AtomicU32::new(0);
        let dir = std::env::temp_dir().join(format!("reliary_init_test_{}_{}", std::process::id(), COUNTER.fetch_add(1, Ordering::SeqCst)));
        let _ = std::fs::create_dir_all(dir.join(".reliary"));  // GUARDED: intentional — test-only code
        let _home_guard = set_test_home(dir.clone());

        // Clear RELIARY_* env vars to avoid interference
        let old_pi_key = std::env::var("OPENAI_API_KEY").ok();  // GUARDED: intentional
        let old_anthro_key = std::env::var("ANTHROPIC_API_KEY").ok();  // GUARDED: intentional
        std::env::remove_var("OPENAI_API_KEY");
        std::env::remove_var("ANTHROPIC_API_KEY");

        test(dir.clone());

        // Restore env
        if let Some(k) = old_pi_key { std::env::set_var("OPENAI_API_KEY", k); }
        if let Some(k) = old_anthro_key { std::env::set_var("ANTHROPIC_API_KEY", k); }
        let _ = std::fs::remove_dir_all(&dir);  // GUARDED: intentional — test code, lock held across I/O
    }
    #[test]
    fn test_inject_mcp_server_stdio() {
        with_temp_home(|home| {
            let cfg_path = home.join("test_mcp_config.json");
            std::fs::write(&cfg_path, r#"{}"#).unwrap();

            let result = inject_mcp_server(&cfg_path, "reliary_test", "mcp");
            assert!(result, "should inject MCP server entry");

            let content = std::fs::read_to_string(&cfg_path).unwrap();
            let v: Value = serde_json::from_str(&content).unwrap();
            let servers = v.get("mcp").and_then(|m| m.as_object()).unwrap();
            assert!(servers.contains_key("reliary_test"));
            let entry = servers.get("reliary_test").unwrap();
            assert_eq!(entry.get("command").and_then(|c| c.as_str()).unwrap_or(""), std::env::current_exe().unwrap().to_str().unwrap());
            assert_eq!(entry.get("args").and_then(|a| a.as_array()).map(|a| a[0].as_str().unwrap_or("")).unwrap_or(""), "mcp");
        });
    }

    #[test]
    fn test_remove_mcp_server() {
        with_temp_home(|home| {
            let cfg_path = home.join("test_remove_config.json");
            std::fs::write(&cfg_path, r#"{"mcp":{"reliary_test":{"command":"/bin/reliary-agent","args":["mcp"]}}}"#).unwrap();

            let result = remove_mcp_server(&cfg_path, "reliary_test", "mcp");
            assert!(result, "should remove MCP server entry");

            let content = std::fs::read_to_string(&cfg_path).unwrap();
            assert!(!content.contains("reliary_test"), "should no longer contain the server");
        });
    }

    #[test]
    fn test_inject_mcp_server_existing_servers() {
        with_temp_home(|home| {
            let cfg_path = home.join("test_existing_config.json");
            std::fs::write(&cfg_path, r#"{"mcp":{"existing":{"command":"/bin/old"}}}"#).unwrap();

            inject_mcp_server(&cfg_path, "reliary_new", "mcp");
            let content = std::fs::read_to_string(&cfg_path).unwrap();
            let v: Value = serde_json::from_str(&content).unwrap();
            let servers = v.get("mcp").and_then(|m| m.as_object()).unwrap();
            assert!(servers.contains_key("existing"), "existing server should survive");
            assert!(servers.contains_key("reliary_new"), "new server should be added");
        });
    }
}