bohay 0.8.2

Next-Gen Agents multiplexer
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
//! Agent integrations (M6): install a hook into an agent's config so it reports
//! its native session id back to bohay over the socket, enabling resume.
//! See docs/10 §integrations.

use std::fs;
use std::path::{Path, PathBuf};

use anyhow::{anyhow, Result};
use serde_json::{json, Value};

/// The `sessionStart` hook script (bash). Extracts the agent's session id from the
/// hook payload on stdin and reports it via the `bohay` CLI (which talks to the
/// socket using the pane's injected `BOHAY_*` env). Shared by Claude and Copilot —
/// their hook formats are compatible (docs/23). The id key varies, so we try the
/// common ones.
fn agent_hook_script(agent: &str) -> String {
    format!(
        r#"#!/usr/bin/env bash
# bohay {agent} integration — reports the session id for native resume, and
# (docs/24 NOTCH-6) forwards lifecycle events (permission prompt / turn end) for
# the notch companion. Branches on the hook's event name.
[ -n "$BOHAY_ENV" ] || exit 0
[ -n "$BOHAY_SOCKET_PATH" ] || exit 0
command -v bohay >/dev/null 2>&1 || exit 0
command -v python3 >/dev/null 2>&1 || exit 0
input="$(cat)"
evt="$(printf '%s' "$input" | python3 -c 'import sys,json
try:
    d=json.load(sys.stdin); print(d.get("hook_event_name") or "")
except Exception: print("")' 2>/dev/null)"
case "$evt" in
  Notification|Stop|SubagentStop)
    msg="$(printf '%s' "$input" | python3 -c 'import sys,json
try:
    d=json.load(sys.stdin); print((d.get("message") or "")[:200])
except Exception: print("")' 2>/dev/null)"
    bohay pane report-event --agent {agent} --kind "$evt" --message "$msg" >/dev/null 2>&1
    ;;
  *)
    sid="$(printf '%s' "$input" | python3 -c 'import sys,json
try:
    d=json.load(sys.stdin); print(d.get("session_id") or d.get("sessionId") or d.get("id") or "")
except Exception: print("")' 2>/dev/null)"
    [ -n "$sid" ] && bohay pane report --agent {agent} --session "$sid" >/dev/null 2>&1
    ;;
esac
exit 0
"#
    )
}

/// The opencode plugin (docs/23): opencode uses JS/TS **plugins**, not shell hooks,
/// so we ship a tiny dependency-free plugin that reports the session id on
/// `session.created`/`session.updated`.
const OPENCODE_PLUGIN: &str = r#"// bohay opencode integration (docs/23) — reports the session id for native resume.
// Auto-installed at <config>/opencode/plugin/bohay.js by `bohay integration install opencode`.
import { spawn } from "node:child_process"

export const bohay = async () => {
  let last = ""
  const report = (id) => {
    if (!id || id === last || !process.env.BOHAY_SOCKET_PATH) return
    last = id
    try {
      spawn("bohay", ["pane", "report", "--agent", "opencode", "--session", String(id)], {
        stdio: "ignore",
        detached: true,
      }).unref()
    } catch {}
  }
  return {
    event: async ({ event }) => {
      if (event?.type === "session.created" || event?.type === "session.updated") {
        const p = event.properties || {}
        report(p.info?.id ?? p.sessionID ?? p.id ?? p.session?.id)
      }
    },
  }
}
"#;

pub fn run(args: &[String]) -> Result<i32> {
    match (
        args.get(2).map(String::as_str),
        args.get(3).map(String::as_str),
    ) {
        (Some("install"), Some(agent)) if AGENTS.contains(&agent) => {
            install(agent)?;
            println!("installed bohay {agent} integration");
            Ok(0)
        }
        (Some("uninstall"), Some(agent)) if AGENTS.contains(&agent) => {
            uninstall(agent)?;
            println!("removed bohay {agent} integration (the {agent} agent itself is untouched)");
            Ok(0)
        }
        (Some("install" | "uninstall"), Some(other)) => Err(anyhow!(
            "unsupported agent: {other} (supported: {})",
            AGENTS.join(", ")
        )),
        _ => Err(anyhow!(
            "usage: bohay integration <install|uninstall> <{}>",
            AGENTS.join("|")
        )),
    }
}

fn home() -> PathBuf {
    crate::platform::home_dir().unwrap_or_default()
}

fn claude_config_dir() -> PathBuf {
    if let Some(d) = std::env::var_os("CLAUDE_CONFIG_DIR") {
        return PathBuf::from(d);
    }
    home().join(".claude")
}

fn copilot_config_dir() -> PathBuf {
    // Copilot CLI reads `~/.copilot`; `BOHAY_COPILOT_DIR` overrides it (tests).
    if let Some(d) = std::env::var_os("BOHAY_COPILOT_DIR") {
        return PathBuf::from(d);
    }
    home().join(".copilot")
}

fn codex_config_dir() -> PathBuf {
    // Codex CLI reads `~/.codex`; `CODEX_HOME` overrides it (a real Codex env var).
    if let Some(d) = std::env::var_os("CODEX_HOME") {
        return PathBuf::from(d);
    }
    home().join(".codex")
}

/// Kimi Code CLI's data dir: `~/.kimi-code`, overridable with `KIMI_CODE_HOME`
/// (a real Kimi env var). Its `config.toml` holds the user's API keys, so we
/// edit it format-preserving (docs/23), never a lossy round-trip.
fn kimi_config_dir() -> PathBuf {
    if let Some(d) = std::env::var_os("KIMI_CODE_HOME") {
        return PathBuf::from(d);
    }
    home().join(".kimi-code")
}

fn kimi_config_path() -> PathBuf {
    kimi_config_dir().join("config.toml")
}

/// opencode's global plugin dir: `$XDG_CONFIG_HOME/opencode/plugin`, else
/// `~/.config/opencode/plugin` (docs/23). opencode auto-loads `*.js`/`*.ts` here.
fn opencode_plugin_dir() -> PathBuf {
    let cfg = std::env::var_os("XDG_CONFIG_HOME")
        .map(PathBuf::from)
        .unwrap_or_else(|| home().join(".config"));
    cfg.join("opencode").join("plugin")
}

fn opencode_plugin_path() -> PathBuf {
    opencode_plugin_dir().join("bohay.js")
}

/// Where + how an agent's shell hook is configured (docs/23). `file` is the JSON
/// config file inside `dir`; `event` is the hook key; `matcher` is an optional
/// group matcher (Codex wants `startup|resume`).
struct HookSpec {
    dir: PathBuf,
    file: &'static str,
    event: &'static str,
    matcher: Option<&'static str>,
}

fn hook_spec(agent: &str) -> Option<HookSpec> {
    Some(match agent {
        "claude" => HookSpec {
            dir: claude_config_dir(),
            file: "settings.json",
            event: "SessionStart",
            matcher: None,
        },
        "copilot" => HookSpec {
            dir: copilot_config_dir(),
            file: "settings.json",
            event: "sessionStart",
            matcher: None,
        },
        "codex" => HookSpec {
            dir: codex_config_dir(),
            file: "hooks.json",
            event: "SessionStart",
            matcher: Some("startup|resume"),
        },
        _ => return None,
    })
}

/// Write the shared `SessionStart` hook script into `agent`'s config dir and
/// register it under the agent's event key. Idempotent (replaces any prior bohay
/// entry). Used for Claude / Copilot / Codex (compatible hook formats, docs/23).
fn install_shell_hook(agent: &str) -> Result<PathBuf> {
    let spec = hook_spec(agent).ok_or_else(|| anyhow!("no shell hook for {agent}"))?;
    fs::create_dir_all(&spec.dir)?;
    let script = spec.dir.join("bohay-agent-hook.sh");
    fs::write(&script, agent_hook_script(agent))?;
    set_executable(&script)?;

    let cfg_path = spec.dir.join(spec.file);
    let mut cfg: Value = match fs::read_to_string(&cfg_path) {
        Ok(s) => serde_json::from_str(&s).unwrap_or_else(|_| json!({})),
        Err(_) => json!({}),
    };
    register_hook(
        &mut cfg,
        spec.event,
        spec.matcher,
        &script.to_string_lossy(),
    );
    fs::write(&cfg_path, serde_json::to_string_pretty(&cfg)?)?;
    Ok(spec.dir)
}

pub fn install_claude() -> Result<PathBuf> {
    let dir = install_shell_hook("claude")?;
    // Also register the same (branching) script under lifecycle events so the
    // notch companion gets precise permission/turn-end signals (docs/24 NOTCH-6).
    let cfg_path = dir.join("settings.json");
    let script = dir.join("bohay-agent-hook.sh");
    let mut cfg: Value = fs::read_to_string(&cfg_path)
        .ok()
        .and_then(|s| serde_json::from_str(&s).ok())
        .unwrap_or_else(|| json!({}));
    for evt in ["Notification", "Stop"] {
        register_hook(&mut cfg, evt, None, &script.to_string_lossy());
    }
    fs::write(&cfg_path, serde_json::to_string_pretty(&cfg)?)?;
    Ok(dir)
}

pub fn install_copilot() -> Result<PathBuf> {
    install_shell_hook("copilot")
}

pub fn install_codex() -> Result<PathBuf> {
    install_shell_hook("codex")
}

/// Install the opencode plugin (NI-4). No shell hook — write the JS plugin.
pub fn install_opencode() -> Result<PathBuf> {
    let dir = opencode_plugin_dir();
    fs::create_dir_all(&dir)?;
    fs::write(opencode_plugin_path(), OPENCODE_PLUGIN)?;
    Ok(dir)
}

/// The Kimi hook events we register (docs/23): `SessionStart` (matcher
/// `startup|resume`) reports the session id for resume; `Notification` + `Stop`
/// feed the notch companion precise lifecycle signals. Kimi's `[[hooks]]` table
/// accepts only `event`/`matcher`/`command`/`timeout`, so we write nothing else.
const KIMI_HOOK_EVENTS: &[(&str, Option<&str>)] = &[
    ("SessionStart", Some("startup|resume")),
    ("Notification", None),
    ("Stop", None),
];

/// True if a `[[hooks]]` entry's `command` points at bohay's hook script.
fn kimi_entry_is_bohay(t: &toml_edit::Table) -> bool {
    t.get("command")
        .and_then(|v| v.as_str())
        .map(|c| c.contains("bohay-agent-hook"))
        .unwrap_or(false)
}

/// Drop every bohay `[[hooks]]` entry in place (idempotent reinstall/uninstall),
/// leaving the user's own hooks and the rest of the file untouched.
fn kimi_strip_bohay(arr: &mut toml_edit::ArrayOfTables) {
    let doomed: Vec<usize> = arr
        .iter()
        .enumerate()
        .filter(|(_, t)| kimi_entry_is_bohay(t))
        .map(|(i, _)| i)
        .collect();
    for i in doomed.into_iter().rev() {
        arr.remove(i);
    }
}

/// Install the Kimi Code hook. Writes the shared `bohay-agent-hook.sh` and adds
/// our `[[hooks]]` entries to `config.toml` **format-preserving** (toml_edit),
/// so the user's API keys, comments, and layout survive. Idempotent.
pub fn install_kimi() -> Result<PathBuf> {
    use toml_edit::{value, ArrayOfTables, DocumentMut, Item, Table};
    let dir = kimi_config_dir();
    fs::create_dir_all(&dir)?;
    let script = dir.join("bohay-agent-hook.sh");
    fs::write(&script, agent_hook_script("kimi"))?;
    set_executable(&script)?;
    let cmd = script.to_string_lossy().into_owned();

    let cfg_path = kimi_config_path();
    let mut doc: DocumentMut = fs::read_to_string(&cfg_path)
        .ok()
        .and_then(|s| s.parse().ok())
        .unwrap_or_default();
    // Get (or create) the `hooks` array-of-tables, coercing a wrong-typed value.
    let hooks = doc
        .as_table_mut()
        .entry("hooks")
        .or_insert(Item::ArrayOfTables(ArrayOfTables::new()));
    if !hooks.is_array_of_tables() {
        *hooks = Item::ArrayOfTables(ArrayOfTables::new());
    }
    let arr = hooks.as_array_of_tables_mut().unwrap();
    kimi_strip_bohay(arr);
    for (event, matcher) in KIMI_HOOK_EVENTS {
        let mut t = Table::new();
        t["event"] = value(*event);
        if let Some(m) = matcher {
            t["matcher"] = value(*m);
        }
        t["command"] = value(cmd.clone());
        arr.push(t);
    }
    fs::write(&cfg_path, doc.to_string())?;
    Ok(dir)
}

/// Agents the integration hook supports (for the Settings UI + CLI).
pub const AGENTS: &[&str] = &["claude", "copilot", "codex", "opencode", "kimi"];

/// Install the integration for `agent` (used by the Settings tab + CLI).
pub fn install(agent: &str) -> Result<()> {
    match agent {
        "claude" => install_claude().map(|_| ()),
        "copilot" => install_copilot().map(|_| ()),
        "codex" => install_codex().map(|_| ()),
        "opencode" => install_opencode().map(|_| ()),
        "kimi" => install_kimi().map(|_| ()),
        other => Err(anyhow!("no integration for {other}")),
    }
}

/// Remove bohay's integration for `agent`. Deletes **only what `install` added** —
/// the `bohay-agent-hook.sh` script + bohay's single hook entry (other entries and
/// the config file itself are left intact), or the opencode plugin file. **Never
/// touches the agent binary, its config, or its sessions.** Idempotent.
pub fn uninstall(agent: &str) -> Result<()> {
    if agent == "opencode" {
        let _ = fs::remove_file(opencode_plugin_path());
        return Ok(());
    }
    if agent == "kimi" {
        let _ = fs::remove_file(kimi_config_dir().join("bohay-agent-hook.sh"));
        // Strip only bohay's `[[hooks]]` entries, format-preserving; the user's
        // API keys, comments, and own hooks stay exactly as they were.
        let cfg_path = kimi_config_path();
        if let Ok(s) = fs::read_to_string(&cfg_path) {
            if let Ok(mut doc) = s.parse::<toml_edit::DocumentMut>() {
                if let Some(arr) = doc
                    .as_table_mut()
                    .get_mut("hooks")
                    .and_then(|h| h.as_array_of_tables_mut())
                {
                    kimi_strip_bohay(arr);
                }
                let _ = fs::write(&cfg_path, doc.to_string());
            }
        }
        return Ok(());
    }
    let spec = hook_spec(agent).ok_or_else(|| anyhow!("no integration for {agent}"))?;
    let _ = fs::remove_file(spec.dir.join("bohay-agent-hook.sh"));
    // Strip bohay's entry from the hook array, keeping everything else in the file.
    let cfg_path = spec.dir.join(spec.file);
    if let Ok(s) = fs::read_to_string(&cfg_path) {
        if let Ok(mut v) = serde_json::from_str::<Value>(&s) {
            // Strip bohay's entry from the primary event and, for Claude, the
            // extra lifecycle events install_claude added (docs/24 NOTCH-6).
            let mut events = vec![spec.event];
            if agent == "claude" {
                events.extend(["Notification", "Stop"]);
            }
            for evt in events {
                if let Some(arr) = v
                    .get_mut("hooks")
                    .and_then(|h| h.get_mut(evt))
                    .and_then(|a| a.as_array_mut())
                {
                    arr.retain(|group| !group_mentions_bohay(group));
                }
            }
            if let Ok(out) = serde_json::to_string_pretty(&v) {
                let _ = fs::write(&cfg_path, out);
            }
        }
    }
    Ok(())
}

/// Whether the integration is currently installed for `agent`.
pub fn is_installed(agent: &str) -> bool {
    if agent == "opencode" {
        return opencode_plugin_path().exists();
    }
    if agent == "kimi" {
        let Ok(s) = fs::read_to_string(kimi_config_path()) else {
            return false;
        };
        let Ok(doc) = s.parse::<toml_edit::DocumentMut>() else {
            return false;
        };
        return doc
            .get("hooks")
            .and_then(|h| h.as_array_of_tables())
            .map(|arr| arr.iter().any(kimi_entry_is_bohay))
            .unwrap_or(false);
    }
    let Some(spec) = hook_spec(agent) else {
        return false;
    };
    let Ok(s) = fs::read_to_string(spec.dir.join(spec.file)) else {
        return false;
    };
    let Ok(v) = serde_json::from_str::<Value>(&s) else {
        return false;
    };
    v.get("hooks")
        .and_then(|h| h.get(spec.event))
        .and_then(|a| a.as_array())
        .map(|arr| arr.iter().any(group_mentions_bohay))
        .unwrap_or(false)
}

/// Insert a command hook under `hooks.<event>` pointing at `script` (with an
/// optional group `matcher`), removing any prior bohay entry first.
fn register_hook(settings: &mut Value, event: &str, matcher: Option<&str>, script: &str) {
    if !settings.is_object() {
        *settings = json!({});
    }
    let hooks = settings
        .as_object_mut()
        .unwrap()
        .entry("hooks")
        .or_insert_with(|| json!({}));
    if !hooks.is_object() {
        *hooks = json!({});
    }
    let session_start = hooks
        .as_object_mut()
        .unwrap()
        .entry(event.to_string())
        .or_insert_with(|| json!([]));
    if !session_start.is_array() {
        *session_start = json!([]);
    }
    let arr = session_start.as_array_mut().unwrap();
    // Drop any previous bohay entries (idempotent reinstall).
    arr.retain(|group| !group_mentions_bohay(group));
    let mut group = json!({ "hooks": [ { "type": "command", "command": script } ] });
    if let Some(m) = matcher {
        group["matcher"] = json!(m);
    }
    arr.push(group);
}

fn group_mentions_bohay(group: &Value) -> bool {
    group
        .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.contains("bohay-agent-hook"))
                    .unwrap_or(false)
            })
        })
        .unwrap_or(false)
}

#[cfg(unix)]
fn set_executable(path: &Path) -> Result<()> {
    use std::os::unix::fs::PermissionsExt;
    let mut perms = fs::metadata(path)?.permissions();
    perms.set_mode(0o755);
    fs::set_permissions(path, perms)?;
    Ok(())
}

#[cfg(not(unix))]
fn set_executable(_path: &Path) -> Result<()> {
    Ok(())
}

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

    #[test]
    fn install_writes_hook_and_settings() {
        let _env = crate::persist::TEST_ENV_LOCK
            .lock()
            .unwrap_or_else(|e| e.into_inner());
        let tmp = std::env::temp_dir().join(format!("bohay-claude-{}", std::process::id()));
        let _ = fs::remove_dir_all(&tmp);
        std::env::set_var("CLAUDE_CONFIG_DIR", &tmp);

        install_claude().unwrap();
        install_claude().unwrap(); // idempotent

        let script = tmp.join("bohay-agent-hook.sh");
        assert!(script.exists());
        let settings: Value =
            serde_json::from_str(&fs::read_to_string(tmp.join("settings.json")).unwrap()).unwrap();
        let groups = settings["hooks"]["SessionStart"].as_array().unwrap();
        // Only one bohay entry despite installing twice.
        let count = groups.iter().filter(|g| group_mentions_bohay(g)).count();
        assert_eq!(count, 1);
        assert!(is_installed("claude"));

        std::env::remove_var("CLAUDE_CONFIG_DIR");
        let _ = fs::remove_dir_all(&tmp);
    }

    #[test]
    fn copilot_hook_registers_under_session_start_camelcase() {
        let _env = crate::persist::TEST_ENV_LOCK
            .lock()
            .unwrap_or_else(|e| e.into_inner());
        let tmp = std::env::temp_dir().join(format!("bohay-copilot-{}", std::process::id()));
        let _ = fs::remove_dir_all(&tmp);
        std::env::set_var("BOHAY_COPILOT_DIR", &tmp);

        install_copilot().unwrap();
        install_copilot().unwrap(); // idempotent

        let script = fs::read_to_string(tmp.join("bohay-agent-hook.sh")).unwrap();
        assert!(script.contains("--agent copilot"), "reports as copilot");
        let settings: Value =
            serde_json::from_str(&fs::read_to_string(tmp.join("settings.json")).unwrap()).unwrap();
        // Copilot uses the camelCase event key (docs/23).
        let groups = settings["hooks"]["sessionStart"].as_array().unwrap();
        assert_eq!(groups.iter().filter(|g| group_mentions_bohay(g)).count(), 1);
        assert!(is_installed("copilot"));

        std::env::remove_var("BOHAY_COPILOT_DIR");
        let _ = fs::remove_dir_all(&tmp);
    }

    #[test]
    fn uninstall_removes_only_bohays_hook_not_the_agent_config() {
        let _env = crate::persist::TEST_ENV_LOCK
            .lock()
            .unwrap_or_else(|e| e.into_inner());
        let tmp = std::env::temp_dir().join(format!("bohay-uninst-{}", std::process::id()));
        let _ = fs::remove_dir_all(&tmp);
        std::env::set_var("CLAUDE_CONFIG_DIR", &tmp);
        fs::create_dir_all(&tmp).unwrap();
        // Pre-existing user config with an unrelated SessionStart hook + other keys.
        fs::write(
            tmp.join("settings.json"),
            r#"{"model":"opus","hooks":{"SessionStart":[{"hooks":[{"type":"command","command":"echo mine"}]}]}}"#,
        )
        .unwrap();

        install_claude().unwrap();
        assert!(is_installed("claude"));
        assert!(tmp.join("bohay-agent-hook.sh").exists());

        uninstall("claude").unwrap();
        assert!(!is_installed("claude"), "bohay hook removed");
        assert!(
            !tmp.join("bohay-agent-hook.sh").exists(),
            "bohay script removed"
        );
        // The user's own hook + other settings survive; the file is intact.
        let v: Value =
            serde_json::from_str(&fs::read_to_string(tmp.join("settings.json")).unwrap()).unwrap();
        assert_eq!(v["model"].as_str(), Some("opus"), "unrelated keys kept");
        let groups = v["hooks"]["SessionStart"].as_array().unwrap();
        assert_eq!(groups.len(), 1, "the user's own hook is kept");
        assert!(!group_mentions_bohay(&groups[0]));

        // Idempotent: uninstalling again is a no-op, never errors.
        uninstall("claude").unwrap();

        std::env::remove_var("CLAUDE_CONFIG_DIR");
        let _ = fs::remove_dir_all(&tmp);
    }

    #[test]
    fn uninstall_opencode_removes_the_plugin() {
        let _env = crate::persist::TEST_ENV_LOCK
            .lock()
            .unwrap_or_else(|e| e.into_inner());
        let tmp = std::env::temp_dir().join(format!("bohay-uninst-oc-{}", std::process::id()));
        let _ = fs::remove_dir_all(&tmp);
        std::env::set_var("XDG_CONFIG_HOME", &tmp);
        install_opencode().unwrap();
        assert!(is_installed("opencode"));
        uninstall("opencode").unwrap();
        assert!(!is_installed("opencode"), "plugin removed");
        uninstall("opencode").unwrap(); // idempotent
        std::env::remove_var("XDG_CONFIG_HOME");
        let _ = fs::remove_dir_all(&tmp);
    }

    #[test]
    fn codex_hook_installs_to_hooks_json_with_matcher() {
        let _env = crate::persist::TEST_ENV_LOCK
            .lock()
            .unwrap_or_else(|e| e.into_inner());
        let tmp = std::env::temp_dir().join(format!("bohay-codex-{}", std::process::id()));
        let _ = fs::remove_dir_all(&tmp);
        std::env::set_var("CODEX_HOME", &tmp);

        install_codex().unwrap();
        install_codex().unwrap(); // idempotent

        let script = fs::read_to_string(tmp.join("bohay-agent-hook.sh")).unwrap();
        assert!(script.contains("--agent codex"), "reports as codex");
        // Codex writes `hooks.json` (not settings.json), SessionStart with a matcher.
        let hooks: Value =
            serde_json::from_str(&fs::read_to_string(tmp.join("hooks.json")).unwrap()).unwrap();
        let groups = hooks["hooks"]["SessionStart"].as_array().unwrap();
        let bohay: Vec<&Value> = groups.iter().filter(|g| group_mentions_bohay(g)).collect();
        assert_eq!(bohay.len(), 1);
        assert_eq!(bohay[0]["matcher"].as_str(), Some("startup|resume"));
        assert!(is_installed("codex"));

        std::env::remove_var("CODEX_HOME");
        let _ = fs::remove_dir_all(&tmp);
    }

    #[test]
    fn kimi_hook_preserves_config_and_is_reversible() {
        let _env = crate::persist::TEST_ENV_LOCK
            .lock()
            .unwrap_or_else(|e| e.into_inner());
        let tmp = std::env::temp_dir().join(format!("bohay-kimi-{}", std::process::id()));
        let _ = fs::remove_dir_all(&tmp);
        fs::create_dir_all(&tmp).unwrap();
        std::env::set_var("KIMI_CODE_HOME", &tmp);
        // Pre-existing config with a secret + a comment + the user's own hook.
        fs::write(
            tmp.join("config.toml"),
            "# my kimi config\ndefault_model = \"kimi-code/k3\"\n\n\
             [providers.\"managed:kimi-code\"]\napi_key = \"sk-secret-123\"\n\n\
             [[hooks]]\nevent = \"PreToolUse\"\ncommand = \"echo mine\"\n",
        )
        .unwrap();

        install_kimi().unwrap();
        install_kimi().unwrap(); // idempotent
        assert!(is_installed("kimi"));
        assert!(tmp.join("bohay-agent-hook.sh").exists());

        let after = fs::read_to_string(tmp.join("config.toml")).unwrap();
        // The secret, comment, and user's own hook all survive the edit.
        assert!(after.contains("sk-secret-123"), "api key preserved");
        assert!(after.contains("# my kimi config"), "comment preserved");
        assert!(after.contains("echo mine"), "user's own hook kept");
        // Our three events landed exactly once each despite installing twice.
        let doc: toml_edit::DocumentMut = after.parse().unwrap();
        let hooks = doc["hooks"].as_array_of_tables().unwrap();
        let bohay = hooks.iter().filter(|t| kimi_entry_is_bohay(t)).count();
        assert_eq!(bohay, 3, "SessionStart + Notification + Stop, no dupes");
        let sess = hooks
            .iter()
            .find(|t| t.get("event").and_then(|v| v.as_str()) == Some("SessionStart"))
            .unwrap();
        assert_eq!(sess["matcher"].as_str(), Some("startup|resume"));

        uninstall("kimi").unwrap();
        assert!(!is_installed("kimi"), "bohay hooks removed");
        assert!(!tmp.join("bohay-agent-hook.sh").exists());
        let cleaned = fs::read_to_string(tmp.join("config.toml")).unwrap();
        assert!(cleaned.contains("sk-secret-123"), "secret still intact");
        assert!(cleaned.contains("echo mine"), "user's hook still intact");
        assert!(
            !cleaned.contains("bohay-agent-hook"),
            "no bohay hooks remain"
        );
        uninstall("kimi").unwrap(); // idempotent

        std::env::remove_var("KIMI_CODE_HOME");
        let _ = fs::remove_dir_all(&tmp);
    }

    #[test]
    fn opencode_installs_a_plugin_file() {
        let _env = crate::persist::TEST_ENV_LOCK
            .lock()
            .unwrap_or_else(|e| e.into_inner());
        let tmp = std::env::temp_dir().join(format!("bohay-opencode-{}", std::process::id()));
        let _ = fs::remove_dir_all(&tmp);
        std::env::set_var("XDG_CONFIG_HOME", &tmp);

        install_opencode().unwrap();
        let plugin = tmp.join("opencode").join("plugin").join("bohay.js");
        let js = fs::read_to_string(&plugin).unwrap();
        assert!(js.contains("session.created"), "hooks the session event");
        assert!(js.contains("--agent"), "reports the session");
        assert!(js.contains("opencode"));
        assert!(is_installed("opencode"));

        std::env::remove_var("XDG_CONFIG_HOME");
        let _ = fs::remove_dir_all(&tmp);
    }
}