agent-config 0.2.0

Install hooks/integrations into AI coding harnesses (Claude Code, Cursor, Gemini CLI, OpenCode, Codex CLI, Cline, Windsurf, ...) without learning each one's filesystem layout.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
//! Cline integration.
//!
//! Surfaces:
//!
//! 1. **Rules** — project-local markdown files at `.clinerules/<tag>.md`.
//!    Same model as Roo / Kilo (one file per consumer, owned outright).
//!
//! 2. **Hooks (v3.36+)** — executable scripts at
//!    `.clinerules/hooks/<event>` (Local) or `~/Documents/Cline/Hooks/<event>`
//!    (Global, macOS/Linux only). Cline reads JSON event payloads on stdin
//!    and inspects the script's exit code / JSON stdout. Filenames are event
//!    names, so concurrent consumers wanting the same event must coordinate;
//!    we record ownership in a sibling `.agent-config-hooks.json` ledger and
//!    refuse to overwrite a hook owned by a different consumer.
//!
//! 3. **MCP servers** — global VS Code extension config at
//!    `Code/User/globalStorage/saoudrizwan.claude-dev/settings/cline_mcp_settings.json`,
//!    keyed by server name under `mcpServers`. Lives in `mcp.rs`.
//!
//! 4. **Skills** — directory-scoped skills at `.cline/skills/<name>/`
//!    (Local) or `~/.cline/skills/<name>/` (Global). Lives in `skills.rs`.
//!
//! 5. **Instructions** — standalone files in `.clinerules/`. Lives in
//!    `instructions.rs`.

use std::path::PathBuf;

use crate::error::AgentConfigError;
use crate::integration::{InstallReport, Integration, UninstallReport};
use crate::plan::{
    has_refusal, InstallPlan, PlanTarget as DryPlanTarget, PlannedChange, RefusalReason,
    UninstallPlan,
};
use crate::scope::{Scope, ScopeKind};
use crate::spec::HookSpec;
#[cfg(not(windows))]
use crate::spec::{Event, ScriptTemplate};
use crate::status::{InstallStatus, PathStatus, PlanTarget, StatusReport, StatusWarning};
#[cfg(not(windows))]
use crate::util::fs_atomic;
use crate::util::{file_lock, ownership, planning, rules_dir, safe_fs};

mod instructions;
mod mcp;
mod skills;

pub(super) const RULES_DIR: &str = ".clinerules";
const HOOKS_SUBDIR: &str = "hooks";
#[cfg(not(windows))]
const KIND: &str = "cline hook";

/// Cline integration.
#[derive(Debug, Clone, Copy, Default)]
pub struct ClineAgent {
    _private: (),
}

impl ClineAgent {
    /// Construct an instance. Stateless.
    pub const fn new() -> Self {
        Self { _private: () }
    }

    pub(super) fn project_root<'a>(
        &self,
        scope: &'a Scope,
    ) -> Result<&'a std::path::Path, AgentConfigError> {
        match scope {
            Scope::Local(p) => Ok(p),
            Scope::Global => Err(AgentConfigError::UnsupportedScope {
                id: "cline",
                scope: ScopeKind::Global,
            }),
        }
    }

    /// `.clinerules/hooks/` (Local). Global is unsupported (Cline's
    /// `~/Documents/Cline/Hooks/` is macOS/Linux-only and the path
    /// convention is unstable enough that we leave it out of v0.1).
    fn hooks_dir(&self, scope: &Scope) -> Result<PathBuf, AgentConfigError> {
        Ok(self.project_root(scope)?.join(RULES_DIR).join(HOOKS_SUBDIR))
    }

    fn ledger_path(&self, scope: &Scope) -> Result<PathBuf, AgentConfigError> {
        Ok(self.hooks_dir(scope)?.join(".agent-config-hooks.json"))
    }
}

impl Integration for ClineAgent {
    fn id(&self) -> &'static str {
        "cline"
    }

    fn display_name(&self) -> &'static str {
        "Cline"
    }

    fn supported_scopes(&self) -> &'static [ScopeKind] {
        &[ScopeKind::Local]
    }

    /// Reports installed if either the rules file *or* a hook script for the
    /// caller exists. (Tag is the consumer ID; hooks are keyed by event name
    /// and recorded by tag in the ledger.)
    fn status(&self, scope: &Scope, tag: &str) -> Result<StatusReport, AgentConfigError> {
        HookSpec::validate_tag(tag)?;
        let root = self.project_root(scope)?;
        let rules_file = rules_dir::target_path(root, RULES_DIR, tag);
        let rules_exists = rules_file.exists();

        let ledger = self.ledger_path(scope)?;
        let owned_hook_count = if ledger.exists() {
            let v = crate::util::json_patch::read_or_empty(&ledger)?;
            v.get("entries")
                .and_then(|e| e.as_object())
                .map(|m| {
                    m.values()
                        .filter(|entry| entry.get("owner").and_then(|o| o.as_str()) == Some(tag))
                        .count()
                })
                .unwrap_or(0)
        } else {
            0
        };

        let mut files = vec![if rules_exists {
            PathStatus::Exists {
                path: rules_file.clone(),
            }
        } else {
            PathStatus::Missing {
                path: rules_file.clone(),
            }
        }];
        if ledger.exists() {
            files.push(PathStatus::Exists {
                path: ledger.clone(),
            });
        }

        let mut warnings = Vec::new();
        let status = if rules_exists || owned_hook_count > 0 {
            InstallStatus::InstalledOwned {
                owner: tag.to_string(),
            }
        } else {
            // Surface a backup file if it exists for the rules markdown.
            let mut bak = rules_file.clone();
            if let Some(name) = bak.file_name().map(|n| n.to_os_string()) {
                if let Ok(mut s) = name.into_string() {
                    s.push_str(".bak");
                    bak.set_file_name(s);
                    if bak.exists() {
                        warnings.push(StatusWarning::BackupExists { path: bak });
                    }
                }
            }
            InstallStatus::Absent
        };

        Ok(StatusReport {
            target: PlanTarget::Hook {
                tag: tag.to_string(),
            },
            status,
            config_path: Some(rules_file),
            ledger_path: Some(ledger),
            files,
            warnings,
        })
    }

    fn plan_install(
        &self,
        scope: &Scope,
        spec: &HookSpec,
    ) -> Result<InstallPlan, AgentConfigError> {
        HookSpec::validate_tag(&spec.tag)?;
        let target = DryPlanTarget::Hook {
            integration_id: Integration::id(self),
            scope: scope.clone(),
            tag: spec.tag.clone(),
        };
        let root = match self.project_root(scope) {
            Ok(root) => root,
            Err(AgentConfigError::UnsupportedScope { .. }) => {
                return Ok(InstallPlan::refused(
                    target,
                    None,
                    RefusalReason::UnsupportedScope,
                ));
            }
            Err(e) => return Err(e),
        };
        let mut changes = Vec::new();
        if let Some(rules) = &spec.rules {
            changes.extend(rules_dir::plan_install(
                root,
                RULES_DIR,
                &spec.tag,
                &rules.content,
            )?);
        }
        if has_refusal(&changes) {
            return Ok(InstallPlan::from_changes(target, changes));
        }

        if spec.script.is_some() || spec.rules.is_none() {
            // Cline's hook contract is a `bash`-shebanged script chmod'd
            // executable. On native Windows the chmod is a no-op and the
            // shebang is meaningless, so the install would silently
            // produce a non-runnable hook. Refuse before any mutation.
            #[cfg(windows)]
            {
                return Ok(InstallPlan::refused(
                    target,
                    None,
                    RefusalReason::UnsupportedPlatform,
                ));
            }
            #[cfg(not(windows))]
            {
                let body = match &spec.script {
                    Some(ScriptTemplate::Shell(s)) => {
                        fs_atomic::ensure_trailing_newline(&prefix_shebang(s))
                    }
                    Some(ScriptTemplate::TypeScript(_)) => {
                        return Ok(InstallPlan::refused(
                            target,
                            None,
                            RefusalReason::MissingRequiredSpecField,
                        ));
                    }
                    None => default_hook_body(&spec.command.render_shell()),
                };
                let event_filename = event_to_filename(&spec.event)?;
                let path = self.hooks_dir(scope)?.join(&event_filename);
                let ledger = self.ledger_path(scope)?;
                let actual_owner = ownership::owner_of(&ledger, &event_filename)?;
                match (actual_owner.as_deref(), path.exists()) {
                    (Some(owner), _) if owner != spec.tag => {
                        changes.push(PlannedChange::Refuse {
                            path: Some(ledger),
                            reason: RefusalReason::OwnerMismatch,
                        });
                        return Ok(InstallPlan::from_changes(target, changes));
                    }
                    (None, true) => {
                        changes.push(PlannedChange::Refuse {
                            path: Some(path),
                            reason: RefusalReason::UserInstalledEntry,
                        });
                        return Ok(InstallPlan::from_changes(target, changes));
                    }
                    _ => {}
                }
                planning::plan_write_file(&mut changes, &path, body.as_bytes(), false)?;
                planning::plan_set_permissions(&mut changes, &path, 0o755);
                let owner_changed = actual_owner.as_deref() != Some(spec.tag.as_str());
                let file_changed = changes.iter().any(|change| {
                    matches!(
                        change,
                        PlannedChange::CreateFile { .. } | PlannedChange::PatchFile { .. }
                    )
                });
                if owner_changed || file_changed {
                    planning::plan_write_ledger(&mut changes, &ledger, &event_filename, &spec.tag);
                }
            }
        }

        Ok(InstallPlan::from_changes(target, changes))
    }

    fn plan_uninstall(&self, scope: &Scope, tag: &str) -> Result<UninstallPlan, AgentConfigError> {
        HookSpec::validate_tag(tag)?;
        let target = DryPlanTarget::Hook {
            integration_id: Integration::id(self),
            scope: scope.clone(),
            tag: tag.to_string(),
        };
        let root = match self.project_root(scope) {
            Ok(root) => root,
            Err(AgentConfigError::UnsupportedScope { .. }) => {
                return Ok(UninstallPlan::refused(
                    target,
                    None,
                    RefusalReason::UnsupportedScope,
                ));
            }
            Err(e) => return Err(e),
        };
        let mut changes = rules_dir::plan_uninstall(root, RULES_DIR, tag)?;
        let ledger = self.ledger_path(scope)?;
        if ledger.exists() {
            let v = match crate::util::json_patch::read_or_empty(&ledger) {
                Ok(v) => v,
                Err(AgentConfigError::JsonInvalid { .. }) => {
                    changes.push(PlannedChange::Refuse {
                        path: Some(ledger),
                        reason: RefusalReason::InvalidConfig,
                    });
                    return Ok(UninstallPlan::from_changes(target, changes));
                }
                Err(e) => return Err(e),
            };
            let owned: Vec<String> = v
                .get("entries")
                .and_then(|e| e.as_object())
                .map(|m| {
                    m.iter()
                        .filter(|(_, entry)| {
                            entry.get("owner").and_then(|o| o.as_str()) == Some(tag)
                        })
                        .map(|(k, _)| k.clone())
                        .collect()
                })
                .unwrap_or_default();
            for filename in owned {
                if validate_custom_event_filename(&filename).is_err() {
                    changes.push(PlannedChange::Refuse {
                        path: Some(ledger),
                        reason: RefusalReason::InvalidConfig,
                    });
                    return Ok(UninstallPlan::from_changes(target, changes));
                }
                let path = self.hooks_dir(scope)?.join(&filename);
                if path.exists() {
                    changes.push(PlannedChange::RemoveFile { path });
                }
                planning::plan_remove_ledger_entry(&mut changes, &ledger, &filename);
            }
        }
        Ok(UninstallPlan::from_changes(target, changes))
    }

    fn install(&self, scope: &Scope, spec: &HookSpec) -> Result<InstallReport, AgentConfigError> {
        HookSpec::validate_tag(&spec.tag)?;
        let root = self.project_root(scope)?;
        let mut report = InstallReport::default();

        if let Some(rules) = &spec.rules {
            scope.ensure_contained(&rules_dir::target_path(root, RULES_DIR, &spec.tag))?;
            let r = rules_dir::install(scope, RULES_DIR, &spec.tag, &rules.content)?;
            report.merge(r);
        }

        if spec.script.is_some() || spec.rules.is_none() {
            // Cline's hook script body is bash-shebanged and chmod'd 0o755;
            // both are no-ops on native Windows. Refuse before any
            // mutation so the install does not silently produce a
            // non-runnable hook. See `plan_install` for the matching
            // refusal in dry-run mode.
            #[cfg(windows)]
            {
                return Err(AgentConfigError::UnsupportedPlatform {
                    id: "cline",
                    reason: "Cline hooks require a POSIX shell environment; native Windows is not supported",
                });
            }
            #[cfg(not(windows))]
            let hooks_dir = self.hooks_dir(scope)?;
            #[cfg(not(windows))]
            scope.ensure_contained(&hooks_dir)?;
            #[cfg(not(windows))]
            file_lock::with_lock(&hooks_dir, || {
                let body = match &spec.script {
                    Some(ScriptTemplate::Shell(s)) => {
                        fs_atomic::ensure_trailing_newline(&prefix_shebang(s))
                    }
                    Some(ScriptTemplate::TypeScript(_)) => {
                        return Err(AgentConfigError::MissingSpecField {
                            id: "cline",
                            field: "script (Shell — TypeScript not supported)",
                        });
                    }
                    None => default_hook_body(&spec.command.render_shell()),
                };

                let event_filename = event_to_filename(&spec.event)?;
                let path = hooks_dir.join(&event_filename);
                let ledger = hooks_dir.join(".agent-config-hooks.json");

                // Refuse to overwrite a hook owned by a different consumer.
                ownership::require_owner(&ledger, &event_filename, &spec.tag, KIND, path.exists())?;

                let outcome = safe_fs::write(scope, &path, body.as_bytes(), false)?;
                #[cfg(unix)]
                safe_fs::chmod(scope, &path, 0o755)?;
                if !outcome.no_change {
                    if outcome.existed {
                        report.patched.push(outcome.path.clone());
                    } else {
                        report.created.push(outcome.path.clone());
                    }
                    let hash = ownership::content_hash(body.as_bytes());
                    ownership::record_install(&ledger, &event_filename, &spec.tag, Some(&hash))?;
                    report.already_installed = false;
                } else {
                    let prior = ownership::owner_of(&ledger, &event_filename)?;
                    if prior.as_deref() != Some(spec.tag.as_str()) {
                        let hash = ownership::content_hash(body.as_bytes());
                        ownership::record_install(
                            &ledger,
                            &event_filename,
                            &spec.tag,
                            Some(&hash),
                        )?;
                        report.already_installed = false;
                    } else if report.created.is_empty() && report.patched.is_empty() {
                        report.already_installed = true;
                    }
                }
                Ok::<(), AgentConfigError>(())
            })?;
        }
        Ok(report)
    }

    fn uninstall(&self, scope: &Scope, tag: &str) -> Result<UninstallReport, AgentConfigError> {
        HookSpec::validate_tag(tag)?;
        let root = self.project_root(scope)?;
        let mut report = UninstallReport::default();

        scope.ensure_contained(&rules_dir::target_path(root, RULES_DIR, tag))?;
        let r = rules_dir::uninstall(scope, RULES_DIR, tag)?;
        report.merge(r);

        // Any hook scripts owned by this tag.
        let hooks_dir = self.hooks_dir(scope)?;
        scope.ensure_contained(&hooks_dir)?;
        file_lock::with_lock(&hooks_dir, || {
            let ledger = hooks_dir.join(".agent-config-hooks.json");
            if ledger.exists() {
                let v = crate::util::json_patch::read_or_empty(&ledger)?;
                let owned: Vec<String> = v
                    .get("entries")
                    .and_then(|e| e.as_object())
                    .map(|m| {
                        m.iter()
                            .filter(|(_, entry)| {
                                entry.get("owner").and_then(|o| o.as_str()) == Some(tag)
                            })
                            .map(|(k, _)| k.clone())
                            .collect()
                    })
                    .unwrap_or_default();

                for filename in owned {
                    validate_custom_event_filename(&filename)?;
                    let path = hooks_dir.join(&filename);
                    if path.exists() {
                        safe_fs::remove_file(scope, &path)?;
                        report.removed.push(path);
                    }
                    ownership::record_uninstall(&ledger, &filename)?;
                }
            }
            Ok::<(), AgentConfigError>(())
        })?;

        // Tidy: prune empty hooks/ then .clinerules/ in case the rules path
        // already pruned them.
        for empty_dir in [self.hooks_dir(scope)?, root.join(RULES_DIR)] {
            if let Ok(mut entries) = std::fs::read_dir(&empty_dir) {
                if entries.next().is_none() {
                    let _ = safe_fs::remove_empty_dir(scope, &empty_dir);
                }
            }
        }

        if report.removed.is_empty() && report.patched.is_empty() && report.restored.is_empty() {
            report.not_installed = true;
        }
        Ok(report)
    }
}

/// Map [`Event`] to Cline's filename convention. Custom names become
/// file names, so they must be path-safe single components.
#[cfg(not(windows))]
fn event_to_filename(event: &Event) -> Result<String, AgentConfigError> {
    match event {
        Event::PreToolUse => Ok("PreToolUse".into()),
        Event::PostToolUse => Ok("PostToolUse".into()),
        Event::Custom(s) => validate_custom_event_filename(s).map(|()| s.clone()),
    }
}

fn validate_custom_event_filename(name: &str) -> Result<(), AgentConfigError> {
    if name.is_empty() {
        return Err(AgentConfigError::InvalidTag {
            tag: name.into(),
            reason: "Cline custom event must not be empty",
        });
    }
    if !name
        .chars()
        .all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '-')
    {
        return Err(AgentConfigError::InvalidTag {
            tag: name.into(),
            reason: "Cline custom event may only contain ASCII letters, digits, '_' and '-'",
        });
    }
    Ok(())
}

#[cfg(not(windows))]
fn prefix_shebang(s: &str) -> String {
    if s.starts_with("#!") {
        s.to_string()
    } else {
        format!("#!/usr/bin/env bash\n{s}")
    }
}

/// Minimal default hook body: pipe stdin to the rendered command and forward
/// exit code. Safe program commands are shell-quoted before they reach here;
/// unchecked shell commands intentionally pass through as shell syntax.
#[cfg(not(windows))]
fn default_hook_body(command: &str) -> String {
    format!(
        "#!/usr/bin/env bash\n# Generated by agent-config.\n# Forwards Cline's JSON event payload to the consumer command.\n{command}\n"
    )
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::integration::McpSurface;
    use crate::spec::McpSpec;
    // `Event` is imported in the parent module behind `cfg(not(windows))` so
    // the lib build on Windows stays warning-clean. The test mod still needs
    // it because `#[cfg(windows)]` tests in this file construct `HookSpec`s
    // with `Event::PreToolUse`. Re-importing here keeps the symbol in scope
    // on every platform without producing a duplicate-import warning.
    use crate::spec::Event;
    use std::fs;
    use tempfile::tempdir;

    fn rules_spec(tag: &str, body: &str) -> HookSpec {
        HookSpec::builder(tag)
            .command_program("noop", [] as [&str; 0])
            .rules(body)
            .build()
    }

    fn hook_spec(tag: &str, event: Event, command: &str) -> HookSpec {
        HookSpec::builder(tag)
            .command_shell_unchecked(command)
            .event(event)
            .build()
    }

    #[test]
    fn install_rules_writes_dot_clinerules_file() {
        let dir = tempdir().unwrap();
        let agent = ClineAgent::new();
        let scope = Scope::Local(dir.path().to_path_buf());
        agent
            .install(&scope, &rules_spec("alpha", "rule body"))
            .unwrap();
        let p = dir.path().join(".clinerules/alpha.md");
        assert!(p.exists());
        assert_eq!(fs::read_to_string(&p).unwrap(), "rule body\n");
    }

    #[cfg(not(windows))]
    #[test]
    fn install_hook_default_writes_executable_script() {
        let dir = tempdir().unwrap();
        let agent = ClineAgent::new();
        let scope = Scope::Local(dir.path().to_path_buf());
        agent
            .install(
                &scope,
                &hook_spec("alpha", Event::PreToolUse, "myapp hook cline"),
            )
            .unwrap();
        let p = dir.path().join(".clinerules/hooks/PreToolUse");
        assert!(p.exists());
        let body = fs::read_to_string(&p).unwrap();
        assert!(body.starts_with("#!/usr/bin/env bash"));
        assert!(body.contains("myapp hook cline"));
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            let mode = fs::metadata(&p).unwrap().permissions().mode() & 0o777;
            assert_eq!(mode, 0o755);
        }
    }

    #[cfg(windows)]
    #[test]
    fn plan_install_hook_refuses_on_windows() {
        let dir = tempdir().unwrap();
        let agent = ClineAgent::new();
        let scope = Scope::Local(dir.path().to_path_buf());
        let spec = hook_spec("alpha", Event::PreToolUse, "noop");
        let plan = agent.plan_install(&scope, &spec).unwrap();
        assert_eq!(plan.status, crate::plan::PlanStatus::Refused);
        let refusal = plan
            .changes
            .iter()
            .find_map(|c| match c {
                PlannedChange::Refuse { reason, .. } => Some(*reason),
                _ => None,
            })
            .expect("plan should contain a refusal");
        assert!(matches!(refusal, RefusalReason::UnsupportedPlatform));
    }

    #[cfg(windows)]
    #[test]
    fn install_hook_returns_unsupported_platform_on_windows() {
        let dir = tempdir().unwrap();
        let agent = ClineAgent::new();
        let scope = Scope::Local(dir.path().to_path_buf());
        let spec = hook_spec("alpha", Event::PreToolUse, "noop");
        let err = agent.install(&scope, &spec).unwrap_err();
        assert!(matches!(
            err,
            AgentConfigError::UnsupportedPlatform { id: "cline", .. }
        ));
        assert!(!dir.path().join(".clinerules/hooks/PreToolUse").exists());
    }

    #[cfg(windows)]
    #[test]
    fn install_rules_only_still_works_on_windows() {
        let dir = tempdir().unwrap();
        let agent = ClineAgent::new();
        let scope = Scope::Local(dir.path().to_path_buf());
        // Rules-only spec does not enter the script-writing branch, so it
        // must still install on Windows.
        agent
            .install(&scope, &rules_spec("alpha", "rule body"))
            .unwrap();
        assert!(dir.path().join(".clinerules/alpha.md").exists());
    }

    #[cfg(not(windows))]
    #[test]
    fn install_hook_default_quotes_program_arguments() {
        let dir = tempdir().unwrap();
        let agent = ClineAgent::new();
        let scope = Scope::Local(dir.path().to_path_buf());
        let spec = HookSpec::builder("alpha")
            .command_program(
                "my hook",
                ["repo path", "semi;$(not run)", "`tick`", "quote's"],
            )
            .build();

        agent.install(&scope, &spec).unwrap();

        let body = fs::read_to_string(dir.path().join(".clinerules/hooks/PreToolUse")).unwrap();
        assert!(body.contains("\n'my hook' 'repo path' 'semi;$(not run)' '`tick`' 'quote'\\''s'\n"));
    }

    #[cfg(not(windows))]
    #[test]
    fn install_hook_with_custom_script_body() {
        let dir = tempdir().unwrap();
        let agent = ClineAgent::new();
        let scope = Scope::Local(dir.path().to_path_buf());
        let s = HookSpec::builder("alpha")
            .command_program("noop", [] as [&str; 0])
            .event(Event::Custom("TaskStart".into()))
            .script(ScriptTemplate::Shell("echo started".into()))
            .build();
        agent.install(&scope, &s).unwrap();
        let p = dir.path().join(".clinerules/hooks/TaskStart");
        assert!(p.exists());
        let body = fs::read_to_string(&p).unwrap();
        assert!(body.contains("echo started"));
    }

    #[cfg(not(windows))]
    #[test]
    fn install_hook_rejects_unsafe_custom_event_filename() {
        let dir = tempdir().unwrap();
        let agent = ClineAgent::new();
        let scope = Scope::Local(dir.path().to_path_buf());

        for bad in [
            "../TaskStart",
            "/tmp/TaskStart",
            "C:\\TaskStart",
            "Task.Start",
        ] {
            let spec = HookSpec::builder("alpha")
                .command_program("noop", [] as [&str; 0])
                .event(Event::Custom(bad.into()))
                .build();
            let err = agent.install(&scope, &spec).unwrap_err();
            assert!(
                matches!(err, AgentConfigError::InvalidTag { .. }),
                "expected invalid custom event for {bad:?}"
            );
        }

        assert!(!dir.path().join(".clinerules/hooks").exists());
    }

    #[cfg(not(windows))]
    #[test]
    fn plan_hook_rejects_unsafe_custom_event_filename() {
        let dir = tempdir().unwrap();
        let agent = ClineAgent::new();
        let scope = Scope::Local(dir.path().to_path_buf());
        let spec = HookSpec::builder("alpha")
            .command_program("noop", [] as [&str; 0])
            .event(Event::Custom("../TaskStart".into()))
            .build();

        let err = agent.plan_install(&scope, &spec).unwrap_err();
        assert!(matches!(err, AgentConfigError::InvalidTag { .. }));
    }

    #[cfg(not(windows))]
    #[test]
    fn install_hook_records_ownership() {
        let dir = tempdir().unwrap();
        let agent = ClineAgent::new();
        let scope = Scope::Local(dir.path().to_path_buf());
        agent
            .install(&scope, &hook_spec("myapp", Event::PreToolUse, "noop"))
            .unwrap();
        let ledger = dir
            .path()
            .join(".clinerules/hooks/.agent-config-hooks.json");
        assert!(ledger.exists());
        let v: serde_json::Value = serde_json::from_slice(&fs::read(&ledger).unwrap()).unwrap();
        assert_eq!(
            v["entries"]["PreToolUse"]["owner"],
            serde_json::json!("myapp")
        );
    }

    #[cfg(not(windows))]
    #[test]
    fn install_hook_collision_with_other_owner_refused() {
        let dir = tempdir().unwrap();
        let agent = ClineAgent::new();
        let scope = Scope::Local(dir.path().to_path_buf());
        agent
            .install(&scope, &hook_spec("appA", Event::PreToolUse, "a"))
            .unwrap();
        let err = agent
            .install(&scope, &hook_spec("appB", Event::PreToolUse, "b"))
            .unwrap_err();
        assert!(matches!(err, AgentConfigError::NotOwnedByCaller { .. }));
        // appA's hook untouched.
        let body = fs::read_to_string(dir.path().join(".clinerules/hooks/PreToolUse")).unwrap();
        assert!(body.contains("a\n"));
    }

    #[cfg(not(windows))]
    #[test]
    fn install_idempotent_for_hook() {
        let dir = tempdir().unwrap();
        let agent = ClineAgent::new();
        let scope = Scope::Local(dir.path().to_path_buf());
        let s = hook_spec("alpha", Event::PreToolUse, "noop");
        agent.install(&scope, &s).unwrap();
        let r2 = agent.install(&scope, &s).unwrap();
        assert!(r2.already_installed);
    }

    #[cfg(not(windows))]
    #[test]
    fn install_typescript_script_rejected() {
        let dir = tempdir().unwrap();
        let agent = ClineAgent::new();
        let scope = Scope::Local(dir.path().to_path_buf());
        let s = HookSpec::builder("alpha")
            .command_program("noop", [] as [&str; 0])
            .script(ScriptTemplate::TypeScript("export {}".into()))
            .build();
        let err = agent.install(&scope, &s).unwrap_err();
        assert!(matches!(err, AgentConfigError::MissingSpecField { .. }));
    }

    #[test]
    fn install_with_only_rules_does_not_create_hook() {
        let dir = tempdir().unwrap();
        let agent = ClineAgent::new();
        let scope = Scope::Local(dir.path().to_path_buf());
        agent.install(&scope, &rules_spec("alpha", "body")).unwrap();
        assert!(dir.path().join(".clinerules/alpha.md").exists());
        assert!(!dir.path().join(".clinerules/hooks").exists());
    }

    #[cfg(not(windows))]
    #[test]
    fn install_with_rules_and_script_creates_both() {
        let dir = tempdir().unwrap();
        let agent = ClineAgent::new();
        let scope = Scope::Local(dir.path().to_path_buf());
        let s = HookSpec::builder("alpha")
            .command_program("noop", [] as [&str; 0])
            .event(Event::PreToolUse)
            .rules("rules body")
            .script(ScriptTemplate::Shell("echo hi".into()))
            .build();
        agent.install(&scope, &s).unwrap();
        assert!(dir.path().join(".clinerules/alpha.md").exists());
        assert!(dir.path().join(".clinerules/hooks/PreToolUse").exists());
    }

    #[cfg(not(windows))]
    #[test]
    fn uninstall_removes_rules_and_owned_hooks() {
        let dir = tempdir().unwrap();
        let agent = ClineAgent::new();
        let scope = Scope::Local(dir.path().to_path_buf());
        agent
            .install(
                &scope,
                &HookSpec::builder("alpha")
                    .command_program("noop", [] as [&str; 0])
                    .event(Event::PreToolUse)
                    .rules("body")
                    .build(),
            )
            .unwrap();
        agent.uninstall(&scope, "alpha").unwrap();
        assert!(!dir.path().join(".clinerules").exists());
    }

    #[cfg(not(windows))]
    #[test]
    fn uninstall_keeps_other_consumers_hooks() {
        let dir = tempdir().unwrap();
        let agent = ClineAgent::new();
        let scope = Scope::Local(dir.path().to_path_buf());
        agent
            .install(&scope, &hook_spec("appA", Event::PreToolUse, "a"))
            .unwrap();
        agent
            .install(&scope, &hook_spec("appB", Event::PostToolUse, "b"))
            .unwrap();
        agent.uninstall(&scope, "appA").unwrap();
        assert!(!dir.path().join(".clinerules/hooks/PreToolUse").exists());
        assert!(dir.path().join(".clinerules/hooks/PostToolUse").exists());
    }

    #[test]
    fn uninstall_rejects_unsafe_ledger_filename() {
        let dir = tempdir().unwrap();
        let agent = ClineAgent::new();
        let scope = Scope::Local(dir.path().to_path_buf());
        let hooks_dir = dir.path().join(".clinerules/hooks");
        fs::create_dir_all(&hooks_dir).unwrap();
        fs::write(
            hooks_dir.join(".agent-config-hooks.json"),
            r#"{"entries":{"../escape":{"owner":"alpha"}}}"#,
        )
        .unwrap();
        let escaped = dir.path().join(".clinerules/escape");
        fs::write(&escaped, "do not remove").unwrap();

        let err = agent.uninstall(&scope, "alpha").unwrap_err();
        assert!(matches!(err, AgentConfigError::InvalidTag { .. }));
        assert!(escaped.exists());
    }

    #[test]
    fn uninstall_unknown_tag_is_noop() {
        let dir = tempdir().unwrap();
        let agent = ClineAgent::new();
        let scope = Scope::Local(dir.path().to_path_buf());
        let r = agent.uninstall(&scope, "ghost").unwrap();
        assert!(r.not_installed);
    }

    #[test]
    fn rejects_global_scope() {
        let agent = ClineAgent::new();
        let err = agent.is_installed(&Scope::Global, "x").unwrap_err();
        assert!(matches!(err, AgentConfigError::UnsupportedScope { .. }));
    }

    #[test]
    fn is_installed_detects_either_surface() {
        let dir = tempdir().unwrap();
        let agent = ClineAgent::new();
        let scope = Scope::Local(dir.path().to_path_buf());
        assert!(!agent.is_installed(&scope, "alpha").unwrap());
        agent.install(&scope, &rules_spec("alpha", "body")).unwrap();
        assert!(agent.is_installed(&scope, "alpha").unwrap());
        agent.uninstall(&scope, "alpha").unwrap();

        // Hook surface install is POSIX-only (Cline writes a bash-shebanged
        // script and refuses on native Windows with `UnsupportedPlatform`).
        // Skip the hook portion of this dual-surface test on Windows; the
        // rules-surface assertion above already exercises `is_installed`.
        #[cfg(not(windows))]
        {
            agent
                .install(&scope, &hook_spec("alpha", Event::PreToolUse, "x"))
                .unwrap();
            assert!(agent.is_installed(&scope, "alpha").unwrap());
        }
    }

    #[test]
    fn mcp_supports_global_only() {
        let agent = ClineAgent::new();
        assert_eq!(agent.supported_mcp_scopes(), &[ScopeKind::Global]);

        let dir = tempdir().unwrap();
        let scope = Scope::Local(dir.path().to_path_buf());
        let spec = McpSpec::builder("github")
            .owner("myapp")
            .stdio("npx", ["@example/server"])
            .build();
        let err = agent.install_mcp(&scope, &spec).unwrap_err();
        assert!(matches!(
            err,
            AgentConfigError::UnsupportedScope {
                scope: ScopeKind::Local,
                ..
            }
        ));
    }
}