afterburner 0.1.3

Afterburner - JS ~> WASM Sandboxed Execution VM
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
// SPDX-License-Identifier: BUSL-1.1
// Copyright (c) 2026 vertexclique
// Licensed under the Business Source License 1.1.
// Change Date: 4 years after this version's release. Change License: Apache-2.0.

//! Per-assistant hook wiring - idempotently merge (and cleanly remove) the
//! `burn agent hook` pre-tool hook in each AI assistant's config file.
//!
//! Every supported host exposes a blocking pre-tool hook driven by an
//! external command over a `stdin JSON -> stdout decision` contract:
//!
//! | Host         | Config                          | Event                  |
//! |--------------|---------------------------------|------------------------|
//! | Claude Code  | `~/.claude/settings.json`       | `PreToolUse`           |
//! | OpenAI Codex | `~/.codex/config.toml`          | `[[hooks.PreToolUse]]` |
//! | Gemini CLI   | `~/.gemini/settings.json`       | `BeforeTool`           |
//! | Cursor       | `~/.cursor/hooks.json`          | `beforeShellExecution` |
//! | Copilot CLI  | `~/.copilot/hooks/burn.json`    | `preToolUse`           |
//! | Antigravity  | `~/.gemini/config/hooks.json`   | `PreToolUse` (named group) |
//!
//! The hook command bakes in **this binary's absolute path** (not the bare
//! name) so the assistant can spawn it regardless of `PATH`. Idempotence
//! and uninstall both key off the [`MARKER`] substring in the serialized
//! entry; user config around our entries is never touched, and a malformed
//! file is reported and left unchanged rather than clobbered.

use std::fmt::Write as _;
use std::path::{Path, PathBuf};

/// The substring that identifies our entries in any host config - the
/// uninstall filter and the duplicate-suppression check both match on it.
pub const MARKER: &str = "burn agent hook";

/// One supported assistant: stable key, friendly label, PATH binary names,
/// and its config dir under `$HOME` (used for detection).
pub struct HostSpec {
    pub key: &'static str,
    pub label: &'static str,
    pub bins: &'static [&'static str],
    pub config_dir: &'static str,
}

/// The assistants `burn agent` knows how to wire.
pub const HOSTS: &[HostSpec] = &[
    HostSpec {
        key: "claude-code",
        label: "Claude Code",
        bins: &["claude"],
        config_dir: ".claude",
    },
    HostSpec {
        key: "codex",
        label: "OpenAI Codex",
        bins: &["codex"],
        config_dir: ".codex",
    },
    HostSpec {
        key: "gemini",
        label: "Gemini CLI",
        bins: &["gemini"],
        config_dir: ".gemini",
    },
    HostSpec {
        key: "cursor",
        label: "Cursor",
        bins: &["cursor", "cursor-agent"],
        config_dir: ".cursor",
    },
    HostSpec {
        key: "copilot",
        label: "GitHub Copilot",
        bins: &["copilot"],
        config_dir: ".copilot",
    },
    HostSpec {
        key: "antigravity",
        label: "Antigravity (agy)",
        bins: &["agy"],
        config_dir: ".gemini/config",
    },
];

/// Look up a host by key.
pub fn spec(key: &str) -> Option<&'static HostSpec> {
    HOSTS.iter().find(|h| h.key == key)
}

/// The absolute path of the running `burn` binary, baked into the hook
/// command. Falls back to the bare name if the exe path can't be resolved.
#[must_use]
pub fn self_path() -> String {
    std::env::current_exe()
        .ok()
        .and_then(|p| p.to_str().map(ToString::to_string))
        .unwrap_or_else(|| "burn".to_string())
}

fn home() -> PathBuf {
    #[cfg(windows)]
    let var = std::env::var("USERPROFILE").or_else(|_| std::env::var("HOME"));
    #[cfg(not(windows))]
    let var = std::env::var("HOME");
    PathBuf::from(var.unwrap_or_else(|_| ".".to_string()))
}

/// Whether `bin` is on `$PATH` (handles a `.exe` suffix on Windows).
fn on_path(bin: &str) -> bool {
    let Some(paths) = std::env::var_os("PATH") else {
        return false;
    };
    std::env::split_paths(&paths)
        .any(|dir| dir.join(bin).is_file() || dir.join(format!("{bin}.exe")).is_file())
}

/// Whether an assistant looks installed: its binary is on `$PATH`, or its
/// config dir already exists under `$HOME`.
#[must_use]
pub fn is_installed(h: &HostSpec) -> bool {
    h.bins.iter().any(|b| on_path(b)) || home().join(h.config_dir).is_dir()
}

/// Where a host's hook config lives. `user=false` targets the project's
/// config for hosts that support repo-local hooks (Claude Code, Gemini);
/// the rest are user-scoped only.
fn config_path(key: &str, user: bool) -> PathBuf {
    let base = if user {
        home()
    } else {
        std::env::current_dir().unwrap_or_else(|_| PathBuf::from("."))
    };
    match key {
        "claude-code" => base.join(".claude").join("settings.json"),
        "gemini" => base.join(".gemini").join("settings.json"),
        // Codex/Cursor/Copilot hooks only fire from user scope.
        "codex" => home().join(".codex").join("config.toml"),
        "cursor" => home().join(".cursor").join("hooks.json"),
        "copilot" => home().join(".copilot").join("hooks").join("burn.json"),
        "antigravity" => home().join(".gemini").join("config").join("hooks.json"),
        _ => base.join(".burn-agent-unknown"),
    }
}

/// The hook command for a host, with the absolute binary path baked in.
fn hook_command(key: &str) -> String {
    format!("{} agent hook --host {key}", self_path())
}

// ── JSON hosts (Claude Code / Gemini) ────────────────────────────────────────

/// Idempotently merge a `{matcher, hooks:[{type:command,…}]}` entry into
/// `hooks.<hooks_key>` of a JSON settings file. Always repairs in place: an
/// existing entry carrying [`MARKER`] (possibly with a stale binary path) is
/// dropped before the fresh one is added, so the array never accumulates
/// duplicates. Returns a human status line; never errors on a malformed
/// file (reports + leaves it untouched).
///
/// # Errors
/// Propagates directory-create / write I/O failure.
pub fn install_json_hook(
    path: &Path,
    hooks_key: &str,
    matcher: &str,
    command: &str,
) -> std::io::Result<String> {
    if let Some(parent) = path.parent() {
        std::fs::create_dir_all(parent)?;
    }
    let text = std::fs::read_to_string(path).unwrap_or_default();
    let mut root: serde_json::Value = if text.trim().is_empty() {
        serde_json::json!({})
    } else {
        match serde_json::from_str(&text) {
            Ok(v) => v,
            Err(_) => {
                return Ok(format!(
                    "! {} is not valid JSON - left unchanged; add the hook manually",
                    path.display()
                ));
            }
        }
    };
    let Some(root_obj) = root.as_object_mut() else {
        return Ok(format!(
            "! {} is not a JSON object - left unchanged",
            path.display()
        ));
    };
    let hooks = root_obj
        .entry("hooks")
        .or_insert_with(|| serde_json::json!({}));
    let Some(hooks_obj) = hooks.as_object_mut() else {
        return Ok(format!(
            "! {} `hooks` is not an object - left unchanged",
            path.display()
        ));
    };
    let arr = hooks_obj
        .entry(hooks_key)
        .or_insert_with(|| serde_json::json!([]));
    let Some(arr_vec) = arr.as_array_mut() else {
        return Ok(format!(
            "! {} hooks.{hooks_key} is not a list - left unchanged",
            path.display()
        ));
    };
    let present = arr_vec.iter().any(|e| e.to_string().contains(MARKER));
    arr_vec.retain(|e| !e.to_string().contains(MARKER));
    arr_vec.push(serde_json::json!({
        "matcher": matcher,
        "hooks": [{ "type": "command", "command": command }],
    }));
    let body = serde_json::to_string_pretty(&root).map_err(std::io::Error::other)?;
    std::fs::write(path, body + "\n")?;
    let verb = if present { "refreshed" } else { "wired" };
    Ok(format!("+ {verb} hook -> {}", path.display()))
}

/// Remove our entry from `hooks.<hooks_key>` of a JSON settings file,
/// pruning the array (and the `hooks` object) when they end up empty so an
/// uninstall leaves no husk behind. Unrelated entries and top-level keys
/// are preserved verbatim.
///
/// # Errors
/// Propagates write I/O failure.
pub fn uninstall_json_hook(path: &Path, hooks_key: &str) -> std::io::Result<String> {
    let Ok(text) = std::fs::read_to_string(path) else {
        return Ok(format!("= nothing to remove: {}", path.display()));
    };
    let Ok(mut root) = serde_json::from_str::<serde_json::Value>(&text) else {
        return Ok(format!(
            "! {} is not valid JSON - left unchanged",
            path.display()
        ));
    };
    let mut removed = false;
    if let Some(arr) = root
        .get_mut("hooks")
        .and_then(|h| h.get_mut(hooks_key))
        .and_then(|a| a.as_array_mut())
    {
        let before = arr.len();
        arr.retain(|e| !e.to_string().contains(MARKER));
        removed = arr.len() != before;
    }
    if !removed {
        return Ok(format!("= no {MARKER} entry in {}", path.display()));
    }
    // Prune empty containers our removal created.
    if let Some(hooks) = root.get_mut("hooks").and_then(|h| h.as_object_mut())
        && hooks
            .get(hooks_key)
            .and_then(|a| a.as_array())
            .is_some_and(Vec::is_empty)
    {
        hooks.remove(hooks_key);
    }
    if root
        .get("hooks")
        .and_then(|h| h.as_object())
        .is_some_and(serde_json::Map::is_empty)
    {
        root.as_object_mut().map(|o| o.remove("hooks"));
    }
    let body = serde_json::to_string_pretty(&root).map_err(std::io::Error::other)?;
    std::fs::write(path, body + "\n")?;
    Ok(format!("- removed hook <- {}", path.display()))
}

// ── Codex (TOML, sentinel-fenced) ────────────────────────────────────────────

/// Marker lines fencing the managed region of Codex's `config.toml`, so
/// install/uninstall can splice it in and out without a TOML parser.
const CODEX_SENTINEL_START: &str = "# >>> burn agent (managed) >>>";
const CODEX_SENTINEL_END: &str = "# <<< burn agent (managed) <<<";

/// Escape a value for a TOML basic string (`"..."`).
fn toml_escape(s: &str) -> String {
    s.replace('\\', "\\\\").replace('"', "\\\"")
}

/// The managed `config.toml` region: separator, start sentinel, the
/// `PreToolUse` hook block, end sentinel.
fn codex_managed_block(command: &str) -> String {
    let cmd = toml_escape(command);
    let mut block = String::new();
    let _ = writeln!(block);
    let _ = writeln!(block, "{CODEX_SENTINEL_START}");
    let _ = write!(
        block,
        "[[hooks.PreToolUse]]\nmatcher = \"^Bash$\"\n[[hooks.PreToolUse.hooks]]\ntype = \"command\"\ncommand = \"{cmd}\"\n"
    );
    let _ = writeln!(block, "{CODEX_SENTINEL_END}");
    block
}

/// Remove the inclusive sentinel region (and a single preceding blank
/// line) if present; otherwise return the text unchanged.
fn strip_codex_managed_region(text: &str) -> String {
    let (Some(s), Some(e)) = (
        text.find(CODEX_SENTINEL_START),
        text.find(CODEX_SENTINEL_END),
    ) else {
        return text.to_string();
    };
    if e < s {
        return text.to_string();
    }
    let region_end = text[e..].find('\n').map_or(text.len(), |nl| e + nl + 1);
    let mut out = text[..s].trim_end_matches('\n').to_string();
    out.push_str(&text[region_end..]);
    out
}

/// Write the `PreToolUse` hook block into `~/.codex/config.toml`, fenced by
/// managed sentinels. Splices a prior managed region out first (repairing a
/// stale binary path); a hook present *outside* the sentinels (hand-added)
/// is reported, never duplicated or deleted.
///
/// # Errors
/// Propagates I/O failure.
pub fn install_codex_hook(path: &Path, command: &str) -> std::io::Result<String> {
    if let Some(parent) = path.parent() {
        std::fs::create_dir_all(parent)?;
    }
    let existing = std::fs::read_to_string(path).unwrap_or_default();
    let present = existing.contains(MARKER);
    let base = strip_codex_managed_region(&existing);
    if present && base == existing {
        return Ok(format!(
            "! {} has a {MARKER} entry outside the managed block - remove it by hand first",
            path.display()
        ));
    }
    let body = format!(
        "{}{}",
        base.trim_end_matches('\n'),
        codex_managed_block(command)
    );
    std::fs::write(path, body)?;
    let verb = if present { "refreshed" } else { "wired" };
    Ok(format!("+ {verb} hook -> {}", path.display()))
}

/// Splice the managed region out of `~/.codex/config.toml`.
///
/// # Errors
/// Propagates write I/O failure.
pub fn uninstall_codex_hook(path: &Path) -> std::io::Result<String> {
    let Ok(existing) = std::fs::read_to_string(path) else {
        return Ok(format!("= nothing to remove: {}", path.display()));
    };
    let base = strip_codex_managed_region(&existing);
    if base == existing {
        return Ok(format!("= no {MARKER} entry in {}", path.display()));
    }
    std::fs::write(path, base)?;
    Ok(format!("- removed hook <- {}", path.display()))
}

// ── Cursor (hooks.json, bare command entries) ────────────────────────────────

/// Idempotently write the `beforeShellExecution` hook into
/// `~/.cursor/hooks.json`. Same repair-in-place semantics as
/// [`install_json_hook`]; preserves unrelated hooks and top-level keys.
///
/// # Errors
/// Propagates directory-create / write I/O failure.
pub fn install_cursor_hook(path: &Path, command: &str) -> std::io::Result<String> {
    if let Some(parent) = path.parent() {
        std::fs::create_dir_all(parent)?;
    }
    let text = std::fs::read_to_string(path).unwrap_or_default();
    let mut root: serde_json::Value = if text.trim().is_empty() {
        serde_json::json!({ "version": 1 })
    } else {
        match serde_json::from_str(&text) {
            Ok(v) => v,
            Err(_) => {
                return Ok(format!(
                    "! {} is not valid JSON - left unchanged; add the hook manually",
                    path.display()
                ));
            }
        }
    };
    let Some(root_obj) = root.as_object_mut() else {
        return Ok(format!(
            "! {} is not a JSON object - left unchanged",
            path.display()
        ));
    };
    let hooks = root_obj
        .entry("hooks")
        .or_insert_with(|| serde_json::json!({}));
    let Some(hooks_obj) = hooks.as_object_mut() else {
        return Ok(format!(
            "! {} `hooks` is not an object - left unchanged",
            path.display()
        ));
    };
    let arr = hooks_obj
        .entry("beforeShellExecution")
        .or_insert_with(|| serde_json::json!([]));
    let Some(arr_vec) = arr.as_array_mut() else {
        return Ok(format!(
            "! {} hooks.beforeShellExecution is not a list - left unchanged",
            path.display()
        ));
    };
    let present = arr_vec.iter().any(|e| e.to_string().contains(MARKER));
    arr_vec.retain(|e| !e.to_string().contains(MARKER));
    arr_vec.push(serde_json::json!({ "command": command }));
    let body = serde_json::to_string_pretty(&root).map_err(std::io::Error::other)?;
    std::fs::write(path, body + "\n")?;
    let verb = if present { "refreshed" } else { "wired" };
    Ok(format!("+ {verb} hook -> {}", path.display()))
}

// ── Copilot (dedicated hooks file) ───────────────────────────────────────────

/// Write `~/.copilot/hooks/burn.json` - a file we own entirely, so install
/// is a plain (over)write and uninstall deletes the file.
///
/// # Errors
/// Propagates directory-create / write I/O failure.
pub fn install_copilot_hook(path: &Path, command: &str) -> std::io::Result<String> {
    if let Some(parent) = path.parent() {
        std::fs::create_dir_all(parent)?;
    }
    let present = path.exists();
    let root = serde_json::json!({
        "version": 1,
        "hooks": { "preToolUse": [{ "type": "command", "command": command }] },
    });
    let body = serde_json::to_string_pretty(&root).map_err(std::io::Error::other)?;
    std::fs::write(path, body + "\n")?;
    let verb = if present { "refreshed" } else { "wired" };
    Ok(format!("+ {verb} hook -> {}", path.display()))
}

// ── Antigravity (named hook groups in hooks.json) ────────────────────────────

/// The top-level group key we own in agy's `hooks.json` - install writes
/// it whole, uninstall deletes it whole, nothing else is touched.
const AGY_GROUP: &str = "burn-agent";

/// Write the `burn-agent` group into `~/.gemini/config/hooks.json`:
/// `{"burn-agent":{"enabled":true,"PreToolUse":[{matcher,hooks:[…]}]}}`.
/// Same repair-in-place + leave-malformed-files-alone contract as the
/// other JSON hosts.
///
/// # Errors
/// Propagates directory-create / write I/O failure.
pub fn install_antigravity_hook(path: &Path, command: &str) -> std::io::Result<String> {
    if let Some(parent) = path.parent() {
        std::fs::create_dir_all(parent)?;
    }
    let text = std::fs::read_to_string(path).unwrap_or_default();
    let mut root: serde_json::Value = if text.trim().is_empty() {
        serde_json::json!({})
    } else {
        match serde_json::from_str(&text) {
            Ok(v) => v,
            Err(_) => {
                return Ok(format!(
                    "! {} is not valid JSON - left unchanged; add the hook manually",
                    path.display()
                ));
            }
        }
    };
    let Some(root_obj) = root.as_object_mut() else {
        return Ok(format!(
            "! {} is not a JSON object - left unchanged",
            path.display()
        ));
    };
    let present = root_obj.contains_key(AGY_GROUP);
    root_obj.insert(
        AGY_GROUP.to_string(),
        serde_json::json!({
            "enabled": true,
            "PreToolUse": [{
                "matcher": "run_command",
                "hooks": [{ "type": "command", "command": command }],
            }],
        }),
    );
    let body = serde_json::to_string_pretty(&root).map_err(std::io::Error::other)?;
    std::fs::write(
        path,
        body + "
",
    )?;
    let verb = if present { "refreshed" } else { "wired" };
    Ok(format!("+ {verb} hook -> {}", path.display()))
}

/// Delete the `burn-agent` group (the inverse of
/// [`install_antigravity_hook`]).
///
/// # Errors
/// Propagates write I/O failure.
pub fn uninstall_antigravity_hook(path: &Path) -> std::io::Result<String> {
    let Ok(text) = std::fs::read_to_string(path) else {
        return Ok(format!("= nothing to remove: {}", path.display()));
    };
    let Ok(mut root) = serde_json::from_str::<serde_json::Value>(&text) else {
        return Ok(format!(
            "! {} is not valid JSON - left unchanged",
            path.display()
        ));
    };
    let removed = root
        .as_object_mut()
        .is_some_and(|o| o.remove(AGY_GROUP).is_some());
    if !removed {
        return Ok(format!("= no {AGY_GROUP} group in {}", path.display()));
    }
    let body = serde_json::to_string_pretty(&root).map_err(std::io::Error::other)?;
    std::fs::write(
        path,
        body + "
",
    )?;
    Ok(format!("- removed hook <- {}", path.display()))
}

// ── orchestration ────────────────────────────────────────────────────────────

/// Wire the pre-tool hook for `key`. `user=true` writes the home-dir config
/// (the default); `false` writes the project's where the host supports it.
/// Always repairs in place (replaces an entry baked with a stale path).
///
/// # Errors
/// Propagates I/O failure, or `InvalidInput` for an unknown host.
pub fn wire_host(key: &str, user: bool) -> std::io::Result<String> {
    let cmd = hook_command(key);
    let path = config_path(key, user);
    match key {
        "claude-code" => install_json_hook(&path, "PreToolUse", "Bash", &cmd),
        "gemini" => install_json_hook(&path, "BeforeTool", "run_shell_command", &cmd),
        "codex" => install_codex_hook(&path, &cmd),
        "cursor" => install_cursor_hook(&path, &cmd),
        "copilot" => install_copilot_hook(&path, &cmd),
        "antigravity" => install_antigravity_hook(&path, &cmd),
        other => Err(std::io::Error::new(
            std::io::ErrorKind::InvalidInput,
            format!("unknown host: {other}"),
        )),
    }
}

/// Remove the pre-tool hook for `key` (the inverse of [`wire_host`]).
///
/// # Errors
/// Propagates I/O failure, or `InvalidInput` for an unknown host.
pub fn unwire_host(key: &str, user: bool) -> std::io::Result<String> {
    let path = config_path(key, user);
    match key {
        "claude-code" => uninstall_json_hook(&path, "PreToolUse"),
        "gemini" => uninstall_json_hook(&path, "BeforeTool"),
        "codex" => uninstall_codex_hook(&path),
        "cursor" => uninstall_json_hook(&path, "beforeShellExecution"),
        "copilot" => {
            if path.exists() {
                std::fs::remove_file(&path)?;
                Ok(format!("- removed hook <- {}", path.display()))
            } else {
                Ok(format!("= nothing to remove: {}", path.display()))
            }
        }
        "antigravity" => uninstall_antigravity_hook(&path),
        other => Err(std::io::Error::new(
            std::io::ErrorKind::InvalidInput,
            format!("unknown host: {other}"),
        )),
    }
}

/// Hook status for one host, as shown by `burn agent status`.
pub struct HostStatus {
    /// The assistant looks installed (binary on PATH or config dir exists).
    pub detected: bool,
    /// Our hook entry is present in the host's config.
    pub wired: bool,
    /// The hook entry exists but bakes a binary path that is not the
    /// currently running `burn` (e.g. after a reinstall elsewhere).
    pub stale: bool,
}

/// Inspect a host's wiring without touching anything.
#[must_use]
pub fn status_host(key: &str, user: bool) -> HostStatus {
    let detected = spec(key).is_some_and(is_installed);
    let path = config_path(key, user);
    let text = std::fs::read_to_string(&path).unwrap_or_default();
    let wired = text.contains(MARKER);
    let stale = wired && !text.contains(&hook_command(key));
    HostStatus {
        detected,
        wired,
        stale,
    }
}

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

    fn tmp(name: &str) -> PathBuf {
        let dir = std::env::temp_dir().join(format!("burn-agent-{name}-{}", std::process::id()));
        std::fs::create_dir_all(&dir).unwrap();
        dir
    }

    #[test]
    fn json_hook_merges_idempotently_and_preserves_existing() {
        let dir = tmp("json");
        let path = dir.join("settings.json");
        std::fs::write(&path, r#"{"theme":"dark"}"#).unwrap();

        let cmd = "/usr/local/bin/burn agent hook --host claude-code";
        let msg = install_json_hook(&path, "PreToolUse", "Bash", cmd).unwrap();
        assert!(msg.contains("wired"));
        let v: serde_json::Value =
            serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
        assert_eq!(v["theme"], "dark"); // unrelated settings preserved
        assert!(
            v["hooks"]["PreToolUse"].as_array().unwrap()[0]["hooks"][0]["command"]
                .as_str()
                .unwrap()
                .contains(MARKER)
        );

        // Second install -> refresh in place, never a duplicate.
        assert!(
            install_json_hook(&path, "PreToolUse", "Bash", cmd)
                .unwrap()
                .contains("refreshed")
        );
        let v: serde_json::Value =
            serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
        assert_eq!(v["hooks"]["PreToolUse"].as_array().unwrap().len(), 1);

        let _ = std::fs::remove_dir_all(&dir);
    }

    #[test]
    fn json_install_replaces_a_stale_path_without_duplicating() {
        let dir = tmp("jstale");
        let path = dir.join("settings.json");

        install_json_hook(
            &path,
            "PreToolUse",
            "Bash",
            "/old/burn agent hook --host claude-code",
        )
        .unwrap();
        install_json_hook(
            &path,
            "PreToolUse",
            "Bash",
            "/new/burn agent hook --host claude-code",
        )
        .unwrap();

        let v: serde_json::Value =
            serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
        let arr = v["hooks"]["PreToolUse"].as_array().unwrap();
        assert_eq!(arr.len(), 1, "no duplicate entry");
        let cmd = arr[0]["hooks"][0]["command"].as_str().unwrap();
        assert!(cmd.contains("/new/"), "uses the current path");
        assert!(!cmd.contains("/old/"), "stale path dropped");

        let _ = std::fs::remove_dir_all(&dir);
    }

    #[test]
    fn json_uninstall_round_trip_restores_the_original_shape() {
        let dir = tmp("jround");
        let path = dir.join("settings.json");
        std::fs::write(&path, "{\n  \"theme\": \"dark\"\n}\n").unwrap();

        install_json_hook(
            &path,
            "PreToolUse",
            "Bash",
            "/x/burn agent hook --host claude-code",
        )
        .unwrap();
        let msg = uninstall_json_hook(&path, "PreToolUse").unwrap();
        assert!(msg.contains("removed"));

        let v: serde_json::Value =
            serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
        assert_eq!(v["theme"], "dark");
        assert!(v.get("hooks").is_none(), "empty hooks containers pruned");

        // Second uninstall is a no-op, not an error.
        assert!(
            uninstall_json_hook(&path, "PreToolUse")
                .unwrap()
                .contains("no ")
        );
        let _ = std::fs::remove_dir_all(&dir);
    }

    #[test]
    fn json_uninstall_preserves_unrelated_hooks() {
        let dir = tmp("jpres");
        let path = dir.join("settings.json");
        std::fs::write(
            &path,
            r#"{"hooks":{"PreToolUse":[{"matcher":"Bash","hooks":[{"type":"command","command":"other-tool hook"}]}]}}"#,
        )
        .unwrap();

        install_json_hook(
            &path,
            "PreToolUse",
            "Bash",
            "/x/burn agent hook --host claude-code",
        )
        .unwrap();
        uninstall_json_hook(&path, "PreToolUse").unwrap();

        let v: serde_json::Value =
            serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
        let arr = v["hooks"]["PreToolUse"].as_array().unwrap();
        assert_eq!(arr.len(), 1, "the other tool's hook survives");
        assert!(arr[0].to_string().contains("other-tool"));

        let _ = std::fs::remove_dir_all(&dir);
    }

    #[test]
    fn malformed_json_is_reported_and_left_unchanged() {
        let dir = tmp("jbad");
        let path = dir.join("settings.json");
        std::fs::write(&path, "{not json").unwrap();

        let msg = install_json_hook(&path, "PreToolUse", "Bash", "x burn agent hook").unwrap();
        assert!(msg.starts_with('!'));
        assert_eq!(std::fs::read_to_string(&path).unwrap(), "{not json");

        let msg = uninstall_json_hook(&path, "PreToolUse").unwrap();
        assert!(msg.starts_with('!'));
        assert_eq!(std::fs::read_to_string(&path).unwrap(), "{not json");

        let _ = std::fs::remove_dir_all(&dir);
    }

    #[test]
    fn codex_install_uninstall_round_trip_preserves_user_config() {
        let dir = tmp("codex");
        let path = dir.join("config.toml");
        std::fs::write(&path, "model = \"o3\"\n").unwrap();

        install_codex_hook(&path, "/x/burn agent hook --host codex").unwrap();
        let text = std::fs::read_to_string(&path).unwrap();
        assert!(text.contains("model = \"o3\""));
        assert!(text.contains("[[hooks.PreToolUse]]"));
        assert_eq!(text.matches(CODEX_SENTINEL_START).count(), 1);

        // Re-install with a new path -> splice + rewrite, no dupes.
        install_codex_hook(&path, "/new/burn agent hook --host codex").unwrap();
        let text = std::fs::read_to_string(&path).unwrap();
        assert_eq!(text.matches(MARKER).count(), 1);
        assert!(text.contains("/new/"));
        assert!(!text.contains("/x/"));

        let msg = uninstall_codex_hook(&path).unwrap();
        assert!(msg.contains("removed"));
        let text = std::fs::read_to_string(&path).unwrap();
        assert!(text.contains("model = \"o3\""));
        assert!(!text.contains(MARKER));
        assert!(!text.contains(CODEX_SENTINEL_START));

        let _ = std::fs::remove_dir_all(&dir);
    }

    #[test]
    fn cursor_install_and_uninstall_preserve_unrelated_hooks() {
        let dir = tmp("cursor");
        let path = dir.join("hooks.json");
        std::fs::write(
            &path,
            r#"{"version":1,"hooks":{"beforeShellExecution":[{"command":"echo hello"}]}}"#,
        )
        .unwrap();

        install_cursor_hook(&path, "/x/burn agent hook --host cursor").unwrap();
        let v: serde_json::Value =
            serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
        let arr = v["hooks"]["beforeShellExecution"].as_array().unwrap();
        assert_eq!(arr.len(), 2, "original hook + ours");

        uninstall_json_hook(&path, "beforeShellExecution").unwrap();
        let v: serde_json::Value =
            serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
        let arr = v["hooks"]["beforeShellExecution"].as_array().unwrap();
        assert_eq!(arr.len(), 1);
        assert!(arr[0].to_string().contains("echo hello"));

        let _ = std::fs::remove_dir_all(&dir);
    }

    #[test]
    fn copilot_install_writes_and_uninstall_deletes_the_file() {
        let dir = tmp("copilot");
        let path = dir.join("hooks").join("burn.json");

        install_copilot_hook(&path, "/x/burn agent hook --host copilot").unwrap();
        let v: serde_json::Value =
            serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
        assert_eq!(v["hooks"]["preToolUse"][0]["type"], "command");
        assert!(
            v["hooks"]["preToolUse"][0]["command"]
                .as_str()
                .unwrap()
                .contains(MARKER)
        );

        // Uninstall = delete the dedicated file.
        std::fs::remove_file(&path).unwrap();
        assert!(!path.exists());

        let _ = std::fs::remove_dir_all(&dir);
    }

    #[test]
    fn antigravity_group_install_uninstall_round_trip() {
        let dir = tmp("agy");
        let path = dir.join("hooks.json");
        std::fs::write(&path, r#"{"user-group":{"enabled":true}}"#).unwrap();

        install_antigravity_hook(&path, "/x/burn agent hook --host antigravity").unwrap();
        let v: serde_json::Value =
            serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
        assert_eq!(v["burn-agent"]["enabled"], true);
        assert_eq!(v["burn-agent"]["PreToolUse"][0]["matcher"], "run_command");
        assert!(
            v["burn-agent"]["PreToolUse"][0]["hooks"][0]["command"]
                .as_str()
                .unwrap()
                .contains(MARKER)
        );
        assert_eq!(v["user-group"]["enabled"], true, "other groups preserved");

        // Refresh in place: still one group.
        install_antigravity_hook(&path, "/new/burn agent hook --host antigravity").unwrap();
        let v: serde_json::Value =
            serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
        assert!(
            v["burn-agent"]["PreToolUse"][0]["hooks"][0]["command"]
                .as_str()
                .unwrap()
                .contains("/new/")
        );

        let msg = uninstall_antigravity_hook(&path).unwrap();
        assert!(msg.contains("removed"));
        let v: serde_json::Value =
            serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
        assert!(v.get("burn-agent").is_none());
        assert_eq!(v["user-group"]["enabled"], true);

        let _ = std::fs::remove_dir_all(&dir);
    }

    #[test]
    fn toml_escape_handles_backslashes_and_quotes() {
        assert_eq!(toml_escape(r"C:\bin\burn"), r"C:\\bin\\burn");
        assert_eq!(toml_escape(r#"a"b"#), r#"a\"b"#);
    }

    #[test]
    fn unknown_host_errors() {
        assert!(wire_host("emacs", true).is_err());
        assert!(unwire_host("emacs", true).is_err());
    }
}