openmoat 0.15.0

Policy checks for AI coding agents: decides allow, defer, review or deny for every tool call from Claude Code, Codex, Cursor, OpenClaw, GitHub Copilot CLI, pi and Gemini CLI, configures Claude Code's, Codex's and Cursor's own OS sandboxes from the same policy, and keeps a local audit log.
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
use super::*;

fn warnings(cwd: &Path, user_home: &Path) -> Vec<String> {
    super::warnings(
        cwd,
        user_home,
        Host::ClaudeCode,
        &user_home.join(".claude/settings.json"),
    )
}

/// pi's extension, as `moat init` installs it under `user_home`.
fn pi_extension(user_home: &Path) -> PathBuf {
    user_home.join(".pi/agent/extensions/openmoat.js")
}

/// A trusted Gemini CLI folder's settings that turn hooks off or add one, and
/// a pi project's settings that turn OpenMoat's extension off and its own
/// extensions, are named with the agent that loads them.
#[test]
fn gemini_and_pi_project_settings_and_extensions_are_named() {
    let dir = tempfile::tempdir().unwrap();
    let user_home = dir.path().join("home");
    let project = user_home.join("proj");
    fs::create_dir_all(project.join(".git")).unwrap();
    fs::create_dir_all(project.join(".gemini")).unwrap();
    fs::create_dir_all(project.join(".pi/extensions/helper")).unwrap();
    let gemini = || {
        let ours = user_home.join(".gemini/settings.json");
        super::warnings(&project, &user_home, Host::GeminiCli, &ours)
    };
    let pi = || super::warnings(&project, &user_home, Host::Pi, &pi_extension(&user_home));
    fs::write(
        project.join(".gemini/settings.json"),
        r#"{"ui": {"theme": "x"}}"#,
    )
    .unwrap();
    fs::write(project.join(".pi/settings.json"), r#"{"theme": "dark"}"#).unwrap();
    fs::write(project.join(".pi/extensions/notes.md"), "not code").unwrap();
    assert_eq!(gemini(), Vec::<String>::new());
    assert_eq!(pi(), Vec::<String>::new());

    fs::write(
        project.join(".gemini/settings.json"),
        r#"{"hooksConfig": {"disabled": ["openmoat"]}, "hooks": {"BeforeTool": [{}]}}"#,
    )
    .unwrap();
    let rows = gemini();
    assert_eq!(rows.len(), 1, "{rows:?}");
    assert!(rows[0].contains("`hooksConfig.disabled`"), "{rows:?}");
    assert!(rows[0].contains("`hooks`"), "{rows:?}");
    assert!(rows[0].contains("A Gemini CLI session"), "{rows:?}");

    fs::write(
        project.join(".pi/settings.json"),
        r#"{"extensions": ["!openmoat.js"]}"#,
    )
    .unwrap();
    fs::write(
        project.join(".pi/extensions/evil.ts"),
        "export default () => {}",
    )
    .unwrap();
    fs::write(project.join(".pi/extensions/helper/index.js"), "").unwrap();
    let rows = pi();
    assert_eq!(rows.len(), 3, "{rows:?}");
    assert!(
        rows[0].contains("`!openmoat.js` turns OpenMoat's extension off"),
        "{rows:?}"
    );
    assert!(
        rows[1].contains("evil.ts: is an extension pi loads"),
        "{rows:?}"
    );
    assert!(rows[2].contains("helper: is an extension"), "{rows:?}");
}

/// The person's own settings file turns the hook off in each host's own way.
#[test]
fn a_user_settings_file_that_turns_the_hook_off_is_named() {
    let dir = tempfile::tempdir().unwrap();
    let cfg = dir.path().join("cfg");
    fs::create_dir_all(&cfg).unwrap();
    let hook = cfg.join("extensions/openmoat.js");
    let off = |host: Host, text: &str| {
        fs::write(cfg.join("settings.json"), text).unwrap();
        switched_off(host, &cfg, &hook)
    };
    for (host, text) in [
        (Host::ClaudeCode, r#"{"disableAllHooks": true}"#),
        (Host::CopilotCli, r#"{"disableAllHooks": true}"#),
        (Host::GeminiCli, r#"{"hooksConfig": {"enabled": false}}"#),
        (Host::Pi, r#"{"extensions": ["-extensions/openmoat.js"]}"#),
        (Host::Pi, "{ not json"),
    ] {
        let found = off(host, text).unwrap_or_else(|| panic!("{host}: {text}"));
        assert!(
            found.starts_with(&cfg.join("settings.json").display().to_string()),
            "{found}"
        );
    }
    for (host, text) in [
        // A project's own concerns are not the person's: hooks and env are theirs.
        (
            Host::ClaudeCode,
            r#"{"hooks": {"Stop": []}, "env": {"A": "b"}, "disableAllHooks": false}"#,
        ),
        (Host::CopilotCli, r#"{"model": "gpt-5"}"#),
        (
            Host::GeminiCli,
            r#"{"hooksConfig": {"disabled": ["lint"]}}"#,
        ),
        (Host::Pi, r#"{"extensions": ["-extensions/other.js"]}"#),
        // Hosts without such a file.
        (Host::Codex, r#"{"disableAllHooks": true}"#),
        (Host::Cursor, r#"{"disableAllHooks": true}"#),
    ] {
        assert_eq!(off(host, text), None, "{host}: {text}");
    }
    fs::remove_file(cfg.join("settings.json")).unwrap();
    assert_eq!(switched_off(Host::Pi, &cfg, &hook), None, "no file");
    // Codex reads `config.toml`, where OpenMoat's own profile sits too.
    let codex = cfg.join("config.toml");
    fs::write(
        &codex,
        "default_permissions = \"moat\"\n[features]\nhooks = false\n",
    )
    .unwrap();
    let found = switched_off(Host::Codex, &cfg, &cfg.join("hooks.json")).unwrap();
    assert!(found.contains("`features.hooks`"), "{found}");
    fs::write(
        &codex,
        "default_permissions = \"moat\"\n[features]\nnetwork_proxy = true\n",
    )
    .unwrap();
    assert_eq!(
        switched_off(Host::Codex, &cfg, &cfg.join("hooks.json")),
        None
    );
}

/// A trusted Codex project's `.codex/` layer and a Cursor project's
/// `.cursor/hooks.json` add hooks beside OpenMoat's.
#[test]
fn codex_and_cursor_project_hooks_are_named() {
    let dir = tempfile::tempdir().unwrap();
    let user_home = dir.path().join("home");
    let project = user_home.join("proj");
    fs::create_dir_all(project.join(".git")).unwrap();
    fs::create_dir_all(project.join(".codex")).unwrap();
    fs::create_dir_all(project.join(".cursor")).unwrap();
    let rows = |host: Host| super::warnings(&project, &user_home, host, Path::new("/x"));
    fs::write(project.join(".codex/config.toml"), "model = \"gpt-5\"\n").unwrap();
    fs::write(
        project.join(".cursor/hooks.json"),
        r#"{"version": 1, "hooks": {}}"#,
    )
    .unwrap();
    assert_eq!(rows(Host::Codex), Vec::<String>::new());
    assert_eq!(rows(Host::Cursor), Vec::<String>::new());
    fs::write(
        project.join(".codex/config.toml"),
        "[features]\nhooks = false\n",
    )
    .unwrap();
    fs::write(
        project.join(".codex/hooks.json"),
        r#"{"hooks": {"PreToolUse": [{}]}}"#,
    )
    .unwrap();
    let hooks = r#"{"version": 1, "hooks": {"beforeShellExecution": [{"command": "./x"}]}}"#;
    fs::write(project.join(".cursor/hooks.json"), hooks).unwrap();
    let codex = rows(Host::Codex);
    assert_eq!(codex.len(), 2, "{codex:?}");
    assert!(codex[0].contains("`features.hooks`"), "{codex:?}");
    assert!(
        codex[1].contains("hooks.json: `hooks` adds hooks"),
        "{codex:?}"
    );
    let cursor = rows(Host::Cursor);
    assert_eq!(cursor.len(), 1, "{cursor:?}");
    assert!(cursor[0].contains("A Cursor session"), "{cursor:?}");
    fs::write(project.join(".codex/config.toml"), "[features\n").unwrap();
    assert!(rows(Host::Codex)[0].contains("cannot be checked"));
}

/// A repository's Copilot settings that turn hooks off, and each hook file in
/// its `.github/hooks`, are named with the agent that loads them.
#[test]
fn copilot_repository_settings_and_hook_files_are_named() {
    let dir = tempfile::tempdir().unwrap();
    let user_home = dir.path().join("home");
    let project = user_home.join("proj");
    fs::create_dir_all(project.join(".git")).unwrap();
    fs::create_dir_all(project.join(".github/copilot")).unwrap();
    fs::create_dir_all(project.join(".github/hooks")).unwrap();
    let ours = user_home.join(".copilot/hooks/openmoat.json");
    let copilot = || super::warnings(&project, &user_home, Host::CopilotCli, &ours);
    assert_eq!(copilot(), Vec::<String>::new());
    fs::write(
        project.join(".github/copilot/settings.json"),
        r#"{"model": "gpt-5"}"#,
    )
    .unwrap();
    fs::write(project.join(".github/hooks/notes.txt"), "not a hook").unwrap();
    assert!(
        copilot().is_empty(),
        "a model choice and other files are left alone"
    );
    fs::write(
        project.join(".github/copilot/settings.local.json"),
        r#"{"disableAllHooks": true}"#,
    )
    .unwrap();
    fs::write(project.join(".github/hooks/audit.json"), "{}").unwrap();
    let rows = copilot();
    assert_eq!(rows.len(), 2, "{rows:?}");
    assert!(rows[0].contains("`disableAllHooks`"), "{rows:?}");
    assert!(rows[1].contains("audit.json: adds hooks"), "{rows:?}");
    assert!(
        rows.iter()
            .all(|r| r.contains("A GitHub Copilot CLI session")),
        "{rows:?}"
    );
    assert!(
        warnings(&project, &user_home).is_empty(),
        "not Claude Code's"
    );
}

/// A file that passes the check but is rewritten before the decision is
/// reported, whatever it holds by then; one left as it was passes.
#[test]
fn a_file_rewritten_during_the_check_is_reported() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("settings.json");
    fs::write(&path, r#"{"model": "opus"}"#).unwrap();
    assert_eq!(check_unchanged(&path), None);
    let swapped = check_between(&path, || {
        fs::write(&path, r#"{"disableAllHooks": true}"#).unwrap();
    });
    assert!(swapped.unwrap().contains("changed while"));
    fs::write(&path, "{}").unwrap();
    let touched = check_between(&path, || fs::write(&path, "{ }").unwrap());
    assert!(
        touched.unwrap().contains("changed while"),
        "bytes, not meaning"
    );
    let gone = dir.path().join("gone.json");
    let created = check_between(&gone, || fs::write(&gone, "{}").unwrap());
    assert!(created.unwrap().contains("changed while"), "appeared");
    fs::remove_file(&gone).unwrap();
    assert_eq!(check_unchanged(&gone), None);
    fs::write(&path, r#"{"hooks": {"Stop": [{}]}}"#).unwrap();
    assert!(
        check_between(&path, || panic!("not read again"))
            .unwrap()
            .contains("`hooks`")
    );
}

#[test]
fn reading_tells_absent_from_broken() {
    let dir = tempfile::tempdir().unwrap();
    assert!(read_bytes(&dir.path().join("none.json")).unwrap().is_none());
    assert_eq!(check(&dir.path().join("none.json")), None);
    let path = dir.path().join("settings.json");
    fs::write(&path, "// comment\n{}").unwrap();
    assert!(check(&path).unwrap().starts_with("cannot be checked"));
    fs::write(&path, " ".repeat(usize::try_from(MAX_BYTES).unwrap() + 1)).unwrap();
    assert!(check(&path).unwrap().contains("larger than"));
    fs::write(&path, r#"{"env": {"HTTPS_PROXY": "http://x"}}"#).unwrap();
    assert!(check(&path).unwrap().contains("`env`"));
}

#[test]
fn warns_for_the_session_directory_and_its_project_root_not_the_home() {
    let dir = tempfile::tempdir().unwrap();
    let user_home = dir.path().join("home");
    let project = user_home.join("proj");
    let sub = project.join("sub");
    fs::create_dir_all(project.join(".git")).unwrap();
    fs::create_dir_all(project.join(".claude")).unwrap();
    fs::create_dir_all(sub.join(".claude")).unwrap();
    fs::create_dir_all(user_home.join(".claude")).unwrap();
    fs::write(
        user_home.join(".claude/settings.json"),
        r#"{"hooks": {"Stop": [{}]}}"#,
    )
    .unwrap();
    assert_eq!(warnings(&sub, &user_home), Vec::<String>::new());
    assert!(warnings(&user_home, &user_home).is_empty(), "user settings");

    fs::write(
        project.join(".claude/settings.json"),
        r#"{"disableAllHooks": true}"#,
    )
    .unwrap();
    fs::write(sub.join(".claude/settings.local.json"), "{").unwrap();
    let rows = warnings(&sub, &user_home);
    assert_eq!(rows.len(), 2, "{rows:?}");
    assert!(
        rows.iter().any(|r| r.contains("`disableAllHooks`")),
        "{rows:?}"
    );
    assert!(
        rows.iter().any(|r| r.contains("cannot be checked")),
        "{rows:?}"
    );
    assert!(rows[0].contains("review the file"), "{rows:?}");
}

/// An OpenCode project's `opencode.json`, read with its comments as OpenCode
/// reads it, and its `.opencode/plugins/` are named; a `plugin` list that only
/// adds turns nothing off, so the person's own file with one names nothing.
#[test]
fn opencode_project_configuration_and_plugins_are_named() {
    let dir = tempfile::tempdir().unwrap();
    let user_home = dir.path().join("home");
    let project = user_home.join("proj");
    fs::create_dir_all(project.join(".git")).unwrap();
    fs::create_dir_all(project.join(".opencode/plugins")).unwrap();
    let plugin = user_home.join(".config/opencode/plugins/openmoat.js");
    let opencode = || super::warnings(&project, &user_home, Host::OpenCode, &plugin);
    fs::write(
        project.join("opencode.json"),
        "{\n  // the model\n  \"model\": \"x/y\",\n}\n",
    )
    .unwrap();
    assert!(opencode().is_empty(), "{:?}", opencode());

    fs::write(
        project.join("opencode.json"),
        "{\n  // added\n  \"plugin\": [\"opencode-helper\"],\n  \"mcp\": {\"db\": {\"type\": \"local\", \"command\": [\"db\"]}},\n}\n",
    )
    .unwrap();
    fs::write(
        project.join(".opencode/plugins/helper.ts"),
        "export default async () => ({})",
    )
    .unwrap();
    let rows = opencode();
    assert_eq!(rows.len(), 2, "{rows:?}");
    assert!(rows[0].contains("`plugin` loads plugins"), "{rows:?}");
    assert!(rows[0].contains("`mcp` adds MCP servers"), "{rows:?}");
    assert!(
        rows[1].contains("helper.ts: is a plugin OpenCode loads"),
        "{rows:?}"
    );

    let config = user_home.join(".config/opencode");
    fs::create_dir_all(&config).unwrap();
    fs::write(config.join("opencode.json"), "{\"plugin\": [\"x\"]}").unwrap();
    assert_eq!(switched_off(Host::OpenCode, &config, &plugin), None);
}

/// OpenCode reads `opencode.jsonc` and `.opencode/opencode.jsonc` as it reads
/// the `.json` files, and loads `.opencode/plugin/` as it loads `plugins/`.
#[test]
fn opencode_jsonc_files_and_the_singular_plugin_folder_are_named() {
    let dir = tempfile::tempdir().unwrap();
    let user_home = dir.path().join("home");
    let project = user_home.join("proj");
    fs::create_dir_all(project.join(".git")).unwrap();
    fs::create_dir_all(project.join(".opencode/plugin")).unwrap();
    let plugin = user_home.join(".config/opencode/plugins/openmoat.js");
    let loads = "{\n  // added\n  \"plugin\": [\"opencode-helper\"],\n}\n";
    fs::write(project.join("opencode.jsonc"), loads).unwrap();
    fs::write(project.join(".opencode/opencode.jsonc"), loads).unwrap();
    fs::write(
        project.join(".opencode/plugin/helper.js"),
        "export default async () => ({})",
    )
    .unwrap();
    // Compared with `/` whatever the platform writes between the names.
    let rows: Vec<String> = super::warnings(&project, &user_home, Host::OpenCode, &plugin)
        .iter()
        .map(|row| row.replace('\\', "/"))
        .collect();
    assert_eq!(rows.len(), 3, "{rows:?}");
    assert!(
        rows[0].contains("opencode.jsonc: `plugin` loads plugins"),
        "{rows:?}"
    );
    assert!(
        rows[1].contains(".opencode/opencode.jsonc: `plugin`"),
        "{rows:?}"
    );
    assert!(
        rows[2].contains("plugin/helper.js: is a plugin OpenCode loads"),
        "{rows:?}"
    );
}

/// OpenCode 2 removes OpenMoat's plugin for `-openmoat` in `plugins`: in the
/// person's `opencode.json` or `opencode.jsonc` OpenCode is not protected, in a
/// project's the warning says its sessions are not.
#[test]
fn a_removal_of_openmoats_opencode_plugin_is_named() {
    let dir = tempfile::tempdir().unwrap();
    let user_home = dir.path().join("home");
    let config = user_home.join(".config/opencode");
    let plugin = config.join("plugins/openmoat.js");
    fs::create_dir_all(&config).unwrap();
    let removes = "{\n  // drops it\n  \"plugins\": [\"-openmoat\"],\n}\n";
    for name in ["opencode.json", "opencode.jsonc"] {
        fs::write(config.join(name), removes).unwrap();
        let found = switched_off(Host::OpenCode, &config, &plugin).unwrap();
        assert!(
            found.contains(name) && found.contains("`-openmoat`"),
            "{found}"
        );
        fs::remove_file(config.join(name)).unwrap();
    }
    assert_eq!(switched_off(Host::OpenCode, &config, &plugin), None);

    let project = user_home.join("proj");
    fs::create_dir_all(project.join(".git")).unwrap();
    fs::write(project.join("opencode.json"), removes).unwrap();
    let rows = super::warnings(&project, &user_home, Host::OpenCode, &plugin);
    assert_eq!(rows.len(), 1, "{rows:?}");
    assert!(
        rows[0].contains("does not protect OpenCode 2 sessions started here"),
        "{rows:?}"
    );
}

/// goose loads a project's `.agents/plugins/openmoat` in place of OpenMoat's
/// plugin: unless its hook file is OpenMoat's, goose is not protected in a
/// session started there. Its other plugins' hooks are warned about, and the
/// project root's replacement is named from a subdirectory.
#[test]
fn a_goose_project_plugin_that_replaces_openmoats_is_named() {
    let dir = tempfile::tempdir().unwrap();
    let user_home = dir.path().join("home");
    let ours = user_home.join(".agents/plugins/openmoat/hooks/hooks.json");
    fs::create_dir_all(ours.parent().unwrap()).unwrap();
    let hooks = r#"{"hooks":{"PreToolUse":[{"hooks":[{"type":"command","command":"openmoat guard --host goose","on_failure":"block"}]}]}}"#;
    fs::write(&ours, hooks).unwrap();
    let project = user_home.join("proj");
    let sub = project.join("sub");
    fs::create_dir_all(project.join(".git")).unwrap();
    fs::create_dir_all(&sub).unwrap();
    let replaced = |cwd: &Path| super::plugins::replaced(cwd, &user_home, Host::Goose, &ours);
    let rows = |cwd: &Path| super::warnings(cwd, &user_home, Host::Goose, &ours);
    assert_eq!(replaced(&project), None);
    assert_eq!(rows(&project), Vec::<String>::new());

    // A copy of OpenMoat's own hook file replaces nothing.
    let theirs = project.join(".agents/plugins/openmoat/hooks/hooks.json");
    fs::create_dir_all(theirs.parent().unwrap()).unwrap();
    fs::write(&theirs, hooks).unwrap();
    assert_eq!(replaced(&project), None);

    // Any other hook file, or none at all, leaves goose unprotected there.
    fs::write(&theirs, r#"{"hooks":{}}"#).unwrap();
    let why = replaced(&project).unwrap();
    assert!(
        why.starts_with(
            &project
                .join(".agents/plugins")
                .join("openmoat")
                .display()
                .to_string()
        ),
        "{why}"
    );
    assert!(
        why.contains("without OpenMoat; remove that directory"),
        "{why}"
    );
    fs::remove_file(&theirs).unwrap();
    assert!(replaced(&project).is_some());
    // The protection level's reason, not a second warning.
    assert_eq!(rows(&project), Vec::<String>::new());

    // From a subdirectory, the project root's plugin is a warning only.
    assert_eq!(replaced(&sub), None);
    let found = rows(&sub);
    assert_eq!(found.len(), 1, "{found:?}");
    assert!(
        found[0].contains("for a goose session started in"),
        "{found:?}"
    );

    // Another plugin's hooks are warned about; one without hooks is not.
    let other = project.join(".agents/plugins/helper/hooks/hooks.json");
    fs::create_dir_all(other.parent().unwrap()).unwrap();
    fs::create_dir_all(project.join(".agents/plugins/skills-only")).unwrap();
    fs::write(&other, r#"{"hooks":{"PreToolUse":[]}}"#).unwrap();
    let found = rows(&project);
    assert_eq!(found.len(), 1, "{found:?}");
    assert!(found[0].contains("helper"), "{found:?}");
    assert!(
        found[0].contains("adds hooks that run commands"),
        "{found:?}"
    );

    // goose started in the home directory loads the person's own plugin.
    assert_eq!(replaced(&user_home), None);
    // A host without project plugins has nothing to replace.
    assert_eq!(
        super::plugins::replaced(&project, &user_home, Host::Pi, &ours),
        None
    );
}