openmoat 0.12.1

Policy checks for AI coding agents: decides allow, defer, ask 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
//! `moat init`: create the state directory, default policy, audit log and host hooks.

use std::io::Write as _;
use std::path::PathBuf;

use anyhow::{Result, bail};
use openmoat_audit::Store;
use openmoat_core::{DEFAULT_POLICY, Policy};
use openmoat_hosts::Host;

use crate::cli::InitArgs;
use crate::environment::Snapshot;
use crate::exit::Code;
use crate::home::Home;
use crate::install::{HostConfig, Outcome, Recorded, dir_variable, env_config_dir};
use crate::integrity;
use crate::render::Deferred;
use crate::sandbox::{self, Plan};

mod mcp;
mod preview;
#[cfg(test)]
mod tests;
mod unattended;

pub fn run(args: &InitArgs) -> Result<Code> {
    let dry_run = args.dry_run;
    let prefix = if dry_run { "would" } else { "✔" };
    let home = Home::locate()?;
    let binary = crate::install::hook_binary()?;
    refuse_writable_binary(&home, &binary)?;
    let mut out = Deferred::default();
    // Whether a lock existed before this run; `repin` below always leaves one.
    let lock_existed = !dry_run && refuse_repin(&home, &mut out)?;
    // No person over an existing lock: what the lock trusts stays (`unattended.rs`).
    // A person may run a binary the lock does not record yet: moving the hooks
    // to it is what `init` is for.
    let unattended =
        unattended::applies(lock_existed, crate::terminal::person_for("openmoat init"));
    if unattended {
        let recorded = crate::install::self_paths::recorded_binary(&home)?;
        unattended::refuse_other_binary(&recorded, &binary)?;
    }
    // Asked before anything is written, so declining everything changes nothing
    // but OpenMoat's own state directory.
    let hosts = choose_hosts(&home, args, &binary, &mut out)?;

    state(&home, dry_run, unattended, &mut out)?;
    if !dry_run {
        home.ensure_approval_files()?;
        // Recorded before the files are written: every command from here on,
        // this one included, finds each agent where this shell says it is.
        record(&home, &hosts, unattended)?;
    }
    for host in hosts.iter().copied() {
        let config = unattended::config(&home, host, unattended)?;
        let outcome = config.install(&home, &binary, dry_run)?;
        let verb = match outcome {
            Outcome::Installed => "installed",
            Outcome::Updated => "updated",
            Outcome::Unchanged => "unchanged",
        };
        let events: Vec<&str> = config.hooks.iter().map(|spec| spec.event).collect();
        writeln!(
            out,
            "{prefix} {:<16} {} ({verb}: {} → {} guard --host {})",
            host.display_name(),
            config.settings_path.display(),
            events.join(", "),
            binary.display(),
            host.id()
        )?;
        if let Some(note) = crate::install::trust_note::after_install(host, outcome) {
            writeln!(out, "{prefix} {:<16} next: {note}", host.display_name())?;
        }
    }

    sandboxes(&home, &binary, &hosts, dry_run, &mut out)?;

    if dry_run {
        writeln!(out, "would lock             {}", home.lock_path().display())?;
        if args.mcp_proxy {
            super::mcp::wrap_servers(&home, &binary, &hosts, &[], false, true, &mut out)?;
        }
        writeln!(out, "dry run: nothing was written")?;
    } else {
        let lock = integrity::repin(&home, &binary, integrity::HookPins::Adopt)?;
        writeln!(
            out,
            "✔ lock             {} ({} files pinned)",
            home.lock_path().display(),
            lock.entries.len()
        )?;
        mcp::servers(&home, &binary, args.mcp_proxy, &hosts, unattended, &mut out)?;
        if !hosts.is_empty() {
            backups(&home, &hosts, &mut out)?;
        }
        writeln!(out, "done. run `openmoat status` any time to verify.")?;
    }
    out.finish()?;
    Ok(Code::Ok)
}

/// Which checks guard `init`'s re-pin of an existing lock.
#[derive(Debug, PartialEq, Eq)]
struct RepinChecks {
    /// Refuse when an agent is among the parent processes.
    agent: bool,
    /// Refuse when pinned files drifted.
    drift: bool,
}

/// The checks for an `init` that writes. A first install has no lock, so
/// nothing to re-pin, and stays open to scripts and the agent setting it up.
/// Over an existing lock an agent is always refused; without a terminal (a
/// script, CI, or a process detached from the agent that started it) drift
/// is refused too, while a person at a terminal re-pins over it as
/// documented, shown what drifted.
fn repin_checks(lock_exists: bool, interactive: bool) -> RepinChecks {
    RepinChecks {
        agent: lock_exists,
        drift: lock_exists && !interactive,
    }
}

/// Refuse, before anything is written, a re-pin [`repin_checks`] does not
/// allow; returns whether a lock exists. An unreadable lock counts as drift:
/// `init` at a terminal is how a person recovers from one, and without a
/// terminal it is refused like any other drift.
fn refuse_repin(home: &Home, out: &mut Deferred) -> Result<bool> {
    let lock_path = home.lock_path();
    let lock_exists = lock_path.exists();
    let checks = repin_checks(lock_exists, crate::terminal::interactive());
    let drift: Vec<String> = if lock_exists {
        integrity::Lock::load(&lock_path).map_or_else(
            |error| vec![format!("{} cannot be read: {error:#}", lock_path.display())],
            |lock| lock.verify().iter().map(ToString::to_string).collect(),
        )
    } else {
        Vec::new()
    };
    if checks.drift && !drift.is_empty() {
        bail!(
            "refusing to re-pin while the policy lock shows drift ({}) and no person is at \
             a terminal:\n  {}\nrun `openmoat doctor` to see what changed, then `openmoat doctor \
             --accept` in a terminal of your own if the changes are yours",
            integrity::INTEGRITY_RULE,
            drift.join("\n  ")
        );
    }
    if checks.agent {
        crate::terminal::refuse_agent("openmoat init")?;
    }
    if !drift.is_empty() {
        writeln!(
            out,
            "! lock drift       re-pinning over: {}",
            drift.join("; ")
        )?;
    }
    Ok(lock_exists)
}

/// Refuse, before anything is written, a binary an agent could replace; the
/// sandboxes' writable directories come from the policy `init` will apply.
fn refuse_writable_binary(home: &Home, binary: &std::path::Path) -> Result<()> {
    let policy = if home.policy_path().exists() {
        home.load_policy()
    } else {
        Ok(Policy::parse(DEFAULT_POLICY)?)
    };
    // A policy that does not lint is reported by `sandboxes`, which then
    // writes no sandbox, so there are no sandbox grants to check.
    let writable = policy
        .and_then(|p| Plan::for_binary(home, &p, binary))
        .map(|plan| plan.writable_dirs())
        .unwrap_or_default();
    crate::install::self_paths::refuse_writable(binary, &writable)
}

/// OpenMoat's own state: the directory, the default policy (kept when there is
/// one), the audit log and the search-path snapshot (kept when `unattended`).
fn state(home: &Home, dry_run: bool, unattended: bool, out: &mut Deferred) -> Result<()> {
    let prefix = if dry_run { "would" } else { "✔" };
    if !dry_run {
        home.ensure()?;
    }
    writeln!(out, "{prefix} state directory  {}", home.root().display())?;

    let policy_path = home.policy_path();
    let wrote_policy = if dry_run {
        !policy_path.exists()
    } else {
        home.ensure_policy()?
    };
    if wrote_policy {
        writeln!(
            out,
            "{prefix} policy           {} (defaults v1)",
            policy_path.display()
        )?;
    } else {
        writeln!(out, "✔ policy           {} (kept)", policy_path.display())?;
        if let Some(update) = crate::upgrade::available(home) {
            writeln!(
                out,
                "! policy update    {update}: `openmoat policy upgrade` shows them and asks"
            )?;
        }
    }

    if !dry_run {
        Store::open(&home.audit_path())?;
    }
    writeln!(
        out,
        "{prefix} audit log        {}",
        home.audit_path().display()
    )?;

    if dry_run {
        writeln!(
            out,
            "would environment      {}",
            home.environment_path().display()
        )?;
    } else if unattended && home.environment_path().is_file() {
        writeln!(
            out,
            "· environment      {} kept: no person at a terminal to record a new search path",
            home.environment_path().display()
        )?;
    } else {
        let snapshot = Snapshot::capture();
        snapshot.save(&home.environment_path())?;
        writeln!(
            out,
            "✔ environment      {} ({} dirs, {} programs pinned)",
            home.environment_path().display(),
            snapshot.path.len(),
            snapshot.programs.len()
        )?;
    }
    Ok(())
}

/// The backups `init` made of `hosts`' files, and how to undo.
fn backups(home: &Home, hosts: &[Host], out: &mut Deferred) -> Result<()> {
    let recorded = Recorded::load(home)?;
    let mut backups = Vec::new();
    for host in hosts {
        for file in host_files(&HostConfig::for_host(home, *host)?) {
            let made = crate::install::backups(&file);
            backups.extend(made.into_iter().filter(|b| recorded.owns(b)));
        }
    }
    let backups: Vec<String> = backups.iter().map(|p| p.display().to_string()).collect();
    if backups.is_empty() {
        writeln!(
            out,
            "· backups          none: the agents had no files to back up"
        )?;
    } else {
        writeln!(out, "✔ backups          {}", backups.join(", "))?;
    }
    writeln!(out, "Undo anytime: openmoat uninstall")?;
    Ok(())
}

/// The hosts to set up: `--hosts` as given; else every host found, each confirmed
/// at the terminal (all of them with `--yes` or `--dry-run`, none without a
/// terminal, so a script never changes an agent's files unasked).
fn choose_hosts(
    home: &Home,
    args: &InitArgs,
    binary: &std::path::Path,
    out: &mut Deferred,
) -> Result<Vec<Host>> {
    if let Some(explicit) = &args.hosts {
        return supported(explicit.iter().copied(), out);
    }
    let mut found = Vec::new();
    for config in Host::ALL
        .into_iter()
        .filter_map(|h| HostConfig::for_init(home, h).ok())
    {
        if config.host_present() {
            found.push(config);
        } else if env_config_dir(config.host)?.is_some()
            && crate::install::location::custom(config.host)
                .is_none_or(openmoat_hosts::Locate::variables_are_the_hosts)
        {
            // The agent creates its directory on first start; say so instead of
            // leaving it out silently. The directory is not created here.
            let name = config.host.display_name();
            writeln!(
                out,
                "{name}: {} is {}, which does not exist yet; start {name} once to create \
                 it, then run openmoat init again",
                dir_variable(config.host)?.0,
                config.dir().display()
            )?;
        }
    }
    let takes_hook = supported(found.iter().map(|c| c.host), out)?;
    found.retain(|c| takes_hook.contains(&c.host));
    let names: Vec<&str> = found.iter().map(|c| c.host.display_name()).collect();
    if found.is_empty() {
        writeln!(
            out,
            "· agents           none found; pass --hosts to set one up"
        )?;
        return Ok(Vec::new());
    }
    if args.yes || args.dry_run {
        return Ok(found.iter().map(|c| c.host).collect());
    }
    if !crate::terminal::interactive() {
        writeln!(
            out,
            "· agents           found {}; none changed without a terminal: run `openmoat init` \
             in a terminal to choose, or `openmoat init --yes` to set up all of them",
            names.join(", ")
        )?;
        return Ok(Vec::new());
    }
    writeln!(out, "Found these agents:")?;
    for config in &found {
        let files: Vec<String> = host_files(config)
            .iter()
            .filter_map(|p| p.file_name().map(|n| n.to_string_lossy().into_owned()))
            .collect();
        writeln!(
            out,
            "  {:<12} {} (changes {})",
            config.host.display_name(),
            config.dir().display(),
            files.join(", ")
        )?;
    }
    writeln!(
        out,
        "OpenMoat adds its hook and sandbox settings to each agent you accept, backing \
         up every file first; `openmoat uninstall` undoes it. Each agent's changes are \
         summed up before you are asked; answer d to see the full diff."
    )?;
    // The sandbox settings the policy `init` keeps or installs gives each agent.
    let plan = sandbox_plan(home, binary).ok();
    let mut chosen = Vec::new();
    for config in &found {
        let changes = preview::changes(home, config, binary, plan.as_ref())?;
        preview::summary(config, &changes, out)?;
        loop {
            write!(
                out,
                "Protect {} ({})? [Y/n/d] ",
                config.host.display_name(),
                config.dir().display()
            )?;
            out.flush()?;
            // End of input is a no: nothing is changed that a person did not accept.
            let answer = crate::terminal::read_answer()?;
            if answer.is_empty() {
                writeln!(out)?;
                break;
            }
            match answer.trim().to_ascii_lowercase().as_str() {
                "d" | "diff" => preview::diff(&changes, out)?,
                "" | "y" | "yes" => {
                    chosen.push(config.host);
                    break;
                }
                _ => break,
            }
        }
    }
    if chosen.is_empty() {
        writeln!(
            out,
            "· agents           none accepted; no agent's files were changed"
        )?;
    }
    Ok(chosen)
}

/// `hosts` but those whose version on the search path does not take OpenMoat's
/// hook (OpenCode before 1.3.4), each named with why it is not set up: a hook it does not
/// load would be pinned and reported as protecting nothing.
fn supported(hosts: impl Iterator<Item = Host>, out: &mut Deferred) -> Result<Vec<Host>> {
    let mut kept = Vec::new();
    for host in hosts {
        match crate::install::host_version::unsupported(host) {
            Some(why) => writeln!(out, "· {:<16} not set up: {why}", host.display_name())?,
            None => kept.push(host),
        }
    }
    Ok(kept)
}

/// Every file `init` may change for `config`'s host: its hook file, its
/// sandbox file beside it when that is another file (Codex's `config.toml`,
/// Cursor's `sandbox.json`), and its other hook files (OpenClaw's plugin).
fn host_files(config: &HostConfig) -> Vec<PathBuf> {
    let mut files = vec![config.settings_path.clone()];
    let spec = config.host.spec();
    if let (Some(sandbox), Some(install)) = (&spec.sandbox, &spec.install)
        && sandbox.file != install.config_file
    {
        files.push(config.settings_path.with_file_name(sandbox.file));
    }
    files.extend(config.extra_files());
    files
}

/// Record where each of `hosts` keeps its configuration, so later commands
/// find it without this shell's environment (`hosts.json`, pinned by the lock);
/// `unattended`, a recorded host keeps its record.
fn record(home: &Home, hosts: &[Host], unattended: bool) -> Result<()> {
    let mut recorded = Recorded::load(home)?;
    for host in hosts {
        let config = HostConfig::for_init(home, *host)?;
        unattended::record(&mut recorded, &config, unattended);
    }
    recorded.save(home)
}

/// The sandbox plan for the policy in place, or the default one `init` would
/// install.
fn sandbox_plan(home: &Home, binary: &std::path::Path) -> Result<Plan> {
    let policy = if home.policy_path().exists() {
        home.load_policy()?
    } else {
        Policy::parse(DEFAULT_POLICY)?
    };
    Plan::for_binary(home, &policy, binary)
}

/// Standard tier (ADR-018): each selected host's own sandbox, configured from
/// the policy. A policy that does not lint is reported, not fatal, so `init`
/// can still repair hooks; `moat sandbox sync` writes the sandboxes later.
fn sandboxes(
    home: &Home,
    binary: &std::path::Path,
    hosts: &[Host],
    dry_run: bool,
    out: &mut Deferred,
) -> Result<()> {
    let prefix = if dry_run { "would" } else { "✔" };
    let selected: Vec<Host> = sandbox::install::hosts()
        .filter(|h| hosts.contains(h))
        .collect();
    if selected.is_empty() {
        return Ok(());
    }
    let plan = match sandbox_plan(home, binary) {
        Ok(plan) => plan,
        Err(e) => {
            writeln!(out, "✗ sandbox          not written: {e:#}")?;
            return Ok(());
        }
    };
    for host in selected {
        let changed = sandbox::install::write(home, host, &plan, dry_run)?;
        if let Some(note) = sandbox::install::unavailable(host) {
            if changed {
                writeln!(
                    out,
                    "{prefix} {:<16} {}",
                    host.display_name(),
                    sandbox::install::OLD_SANDBOX_REMOVED
                )?;
            }
            writeln!(out, "· {:<16} {note}", host.display_name())?;
            continue;
        }
        let verb = if changed { "updated" } else { "unchanged" };
        let report = sandbox::install::report(host, &plan);
        writeln!(
            out,
            "{prefix} {:<16} {} (sandbox {verb}; {} stricter, {} wider than the policy: `openmoat sandbox show`)",
            host.display_name(),
            sandbox::install::settings_path(home, host)?.display(),
            report.losses.len(),
            report.allowances.len()
        )?;
    }
    Ok(())
}