patchloom 0.11.0

Structured file editing library and CLI for AI agents: parser-backed JSON/YAML/TOML edits, AST-aware code operations, multi-file batching, markdown operations, and MCP server
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
use crate::cli::global::GlobalFlags;
use crate::cmd::agent_rules::{
    AGENT_RULES_GENERATED_MARKER, AgentMode, AgentPlatform, AgentRulesArgs, generate_agent_rules,
};
use crate::exit;
use anyhow::Context;
use clap::Args;
use std::path::{Path, PathBuf};

#[derive(Debug, Args)]
#[command(after_help = "\
EXAMPLES:
  patchloom init
  patchloom init --yes")]
pub struct InitArgs {
    /// Skip confirmation prompts.
    #[arg(long, short = 'y')]
    pub yes: bool,
}

/// Candidate files where agent instructions may already exist.
const AGENT_FILES: &[&str] = &[
    "AGENTS.md",
    "Agents.md",
    "AGENT.md",
    "Claude.md",
    "CLAUDE.md",
    "PATCHLOOM.md",
];

pub fn run(args: InitArgs, global: &GlobalFlags) -> anyhow::Result<u8> {
    crate::verbose!("init: yes={}", args.yes);
    let cwd = global.resolve_cwd()?;
    let auto_yes = args.yes;
    let quiet = global.quiet;

    // Helper: print to stderr unless --quiet.
    macro_rules! status {
        ($($arg:tt)*) => {
            if !quiet { eprintln!($($arg)*); }
        };
    }

    // 1. Generate and write agent rules.
    let rules = generate_agent_rules(&AgentRulesArgs {
        mode: AgentMode::All,
        platform: AgentPlatform::All,
    });

    let target = find_agent_file(&cwd);
    let (target_path, action) = match target {
        Some(existing) => (existing, "append"),
        None => (cwd.join("AGENTS.md"), "create"),
    };
    let rel_target = target_path
        .strip_prefix(&cwd)
        .unwrap_or(&target_path)
        .display();

    if action == "append" {
        // Check if patchloom rules are already present.
        let content = std::fs::read_to_string(&target_path)
            .with_context(|| format!("reading existing {}", target_path.display()))?;
        if content.contains(AGENT_RULES_GENERATED_MARKER) {
            status!("{rel_target} already contains patchloom rules, skipping.");
        } else if auto_yes || confirm(&format!("Append patchloom rules to {rel_target}?")) {
            let mut content = content;
            if !content.ends_with('\n') {
                content.push('\n');
            }
            content.push('\n');
            content.push_str(&rules);
            std::fs::write(&target_path, content)
                .with_context(|| format!("writing {}", target_path.display()))?;
            status!("appended patchloom rules to {rel_target}");
        } else {
            status!("skipped {rel_target}");
        }
    } else if auto_yes || confirm(&format!("Create {rel_target}?")) {
        std::fs::write(&target_path, &rules)
            .with_context(|| format!("writing {}", target_path.display()))?;
        status!("created {rel_target}");
    } else {
        status!("skipped {rel_target}");
    }

    // 2. Shell completions: auto-install or hint.
    if let Some(shell) = detect_shell() {
        if let Some(target) = completion_install_path(&shell) {
            let parent_ready = target.parent().map_or(Ok(()), |parent| {
                if parent.exists() {
                    Ok(())
                } else {
                    std::fs::create_dir_all(parent)
                        .with_context(|| format!("creating {}", parent.display()))
                }
            });
            if let Err(e) = parent_ready {
                status!("failed to prepare completion directory: {e}");
                let cmd = completion_command(&shell);
                status!("\nshell completions ({shell}):");
                status!("  {cmd}");
            } else if auto_yes
                || confirm(&format!(
                    "Install {} completions to {}?",
                    shell,
                    target.display()
                ))
            {
                match generate_completions(&shell, &target) {
                    Ok(()) => status!("installed {shell} completions to {}", target.display()),
                    Err(e) => status!("failed to install completions: {e}"),
                }
            } else {
                let cmd = completion_command(&shell);
                status!("\nshell completions ({shell}):");
                status!("  {cmd}");
            }
        } else {
            let cmd = completion_command(&shell);
            status!("\nshell completions ({shell}):");
            status!("  {cmd}");
        }
    } else if !quiet {
        eprintln!("\nshell completions:");
        eprintln!("  patchloom completions <bash|zsh|fish|elvish>");
    }

    // 3. Keep undo backups out of git noise (pairs with `status` filtering).
    match ensure_gitignore_patchloom(&cwd) {
        Ok(GitignorePatchloom::Created) => status!("created .gitignore with .patchloom/"),
        Ok(GitignorePatchloom::Appended) => status!("appended .patchloom/ to .gitignore"),
        Ok(GitignorePatchloom::AlreadyPresent) => {
            status!(".gitignore already ignores .patchloom/");
        }
        Err(e) => status!("could not update .gitignore: {e}"),
    }

    if !quiet {
        // 4. MCP setup hint.
        eprintln!();
        if cfg!(feature = "mcp") {
            let mcp_json_hint = r#"    { "servers": { "patchloom": { "command": "patchloom", "args": ["mcp-server"] } } }"#;
            eprintln!("MCP server is available. Add to your agent's config:");
            if cwd.join(".grok").is_dir() || home_file_exists(".grok/config.toml") {
                eprintln!("  Grok: add to ~/.grok/config.toml:");
                eprintln!("    [mcp_servers.patchloom]");
                eprintln!("    command = \"patchloom\"");
                eprintln!("    args = [\"mcp-server\"]");
            }
            if cwd.join(".vscode").is_dir() {
                eprintln!("  VS Code: create .vscode/mcp.json:");
                eprintln!("{mcp_json_hint}");
            }
            if cwd.join(".cursor").is_dir() {
                eprintln!("  Cursor: create .cursor/mcp.json:");
                eprintln!("{mcp_json_hint}");
            }
        } else {
            eprintln!(
                "MCP server not available (this binary was built with --no-default-features)."
            );
        }

        eprintln!();
        eprintln!("setup complete.");
    }
    Ok(exit::SUCCESS)
}

#[derive(Debug, PartialEq, Eq)]
enum GitignorePatchloom {
    Created,
    Appended,
    AlreadyPresent,
}

const GITIGNORE_PATCHLOOM_LINE: &str = ".patchloom/";

/// Ensure `.gitignore` ignores Patchloom backup sessions so `git status`
/// stays clean after `--apply` (CLI `status` already filters these paths).
fn ensure_gitignore_patchloom(cwd: &Path) -> anyhow::Result<GitignorePatchloom> {
    let path = cwd.join(".gitignore");
    if path.exists() {
        let content = std::fs::read_to_string(&path)
            .with_context(|| format!("reading {}", path.display()))?;
        if gitignore_already_covers_patchloom(&content) {
            return Ok(GitignorePatchloom::AlreadyPresent);
        }
        let mut next = content;
        if !next.ends_with('\n') && !next.is_empty() {
            next.push('\n');
        }
        next.push_str(GITIGNORE_PATCHLOOM_LINE);
        next.push('\n');
        std::fs::write(&path, next).with_context(|| format!("writing {}", path.display()))?;
        return Ok(GitignorePatchloom::Appended);
    }
    let body =
        format!("# Patchloom undo sessions (created by --apply)\n{GITIGNORE_PATCHLOOM_LINE}\n");
    std::fs::write(&path, body).with_context(|| format!("writing {}", path.display()))?;
    Ok(GitignorePatchloom::Created)
}

fn gitignore_already_covers_patchloom(content: &str) -> bool {
    content.lines().any(|line| {
        let t = line.trim();
        t == ".patchloom/"
            || t == ".patchloom"
            || t == "**/.patchloom/"
            || t == "**/.patchloom"
            || t.starts_with(".patchloom/")
    })
}

fn find_agent_file(cwd: &Path) -> Option<std::path::PathBuf> {
    let entries: Vec<_> = std::fs::read_dir(cwd)
        .ok()?
        .filter_map(Result::ok)
        .collect();
    for name in AGENT_FILES {
        for entry in &entries {
            if entry.file_name() == std::ffi::OsStr::new(name) {
                return Some(entry.path());
            }
        }
    }
    None
}

fn detect_shell() -> Option<String> {
    std::env::var("SHELL").ok().and_then(|s| {
        let name = Path::new(&s).file_name()?.to_str()?.to_string();
        match name.as_str() {
            "bash" | "zsh" | "fish" | "elvish" => Some(name),
            _ => None,
        }
    })
}

fn completion_command(shell: &str) -> String {
    match shell {
        "bash" => "patchloom completions bash > /etc/bash_completion.d/patchloom".into(),
        "zsh" => "patchloom completions zsh > ~/.zfunc/_patchloom".into(),
        "fish" => "patchloom completions fish > ~/.config/fish/completions/patchloom.fish".into(),
        "elvish" => "patchloom completions elvish >> ~/.config/elvish/rc.elv".into(),
        _ => format!("patchloom completions {shell}"),
    }
}

fn completion_install_path(shell: &str) -> Option<PathBuf> {
    let home = std::env::var("HOME").ok()?;
    let home = Path::new(&home);
    match shell {
        "bash" => {
            let xdg = home.join(".local/share/bash-completion/completions");
            if xdg.is_dir() {
                return Some(xdg.join("patchloom"));
            }
            // System path as fallback (may need root).
            let sys = Path::new("/etc/bash_completion.d/patchloom");
            Some(sys.to_path_buf())
        }
        "zsh" => Some(home.join(".zfunc/_patchloom")),
        "fish" => Some(home.join(".config/fish/completions/patchloom.fish")),
        _ => None,
    }
}

fn generate_completions(shell: &str, target: &Path) -> anyhow::Result<()> {
    use crate::cli::Cli;
    let clap_shell = match shell {
        "bash" => clap_complete::Shell::Bash,
        "zsh" => clap_complete::Shell::Zsh,
        "fish" => clap_complete::Shell::Fish,
        "elvish" => clap_complete::Shell::Elvish,
        _ => anyhow::bail!("unsupported shell: {shell}"),
    };
    let mut cmd = <Cli as clap::CommandFactory>::command();
    let mut buf = Vec::new();
    clap_complete::generate(clap_shell, &mut cmd, "patchloom", &mut buf);
    if let Some(parent) = target.parent() {
        std::fs::create_dir_all(parent)
            .with_context(|| format!("creating directory {}", parent.display()))?;
    }
    std::fs::write(target, buf)
        .with_context(|| format!("writing completions to {}", target.display()))?;
    Ok(())
}

fn home_file_exists(rel: &str) -> bool {
    if let Some(home) = std::env::var_os("HOME") {
        return Path::new(&home).join(rel).exists();
    }
    false
}

fn confirm(prompt: &str) -> bool {
    crate::cli::global::confirm_prompt(prompt)
}

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

    #[test]
    fn detect_shell_from_env() {
        // Just ensure the function doesn't panic.
        let _ = detect_shell();
    }

    #[test]
    fn completion_command_known_shells() {
        assert!(completion_command("bash").contains("bash_completion"));
        assert!(completion_command("zsh").contains("_patchloom"));
        assert!(completion_command("fish").contains("completions/patchloom.fish"));
    }

    #[test]
    fn completion_command_elvish_uses_append() {
        // #1177: Elvish completions must use >> (append) not > (overwrite)
        // to avoid destroying the user's existing rc.elv configuration.
        let cmd = completion_command("elvish");
        assert!(
            cmd.contains(">>"),
            "elvish command should use >> (append), got: {cmd}"
        );
        assert!(
            !cmd.contains(" > "),
            "elvish command must not use > (overwrite), got: {cmd}"
        );
    }

    #[test]
    fn find_agent_file_none_in_empty_dir() {
        let dir = tempfile::TempDir::new().unwrap();
        assert!(find_agent_file(dir.path()).is_none());
    }

    #[test]
    fn ensure_gitignore_creates_when_missing() {
        let dir = tempfile::TempDir::new().unwrap();
        assert_eq!(
            ensure_gitignore_patchloom(dir.path()).unwrap(),
            GitignorePatchloom::Created
        );
        let content = std::fs::read_to_string(dir.path().join(".gitignore")).unwrap();
        assert!(content.contains(GITIGNORE_PATCHLOOM_LINE));
        assert_eq!(
            ensure_gitignore_patchloom(dir.path()).unwrap(),
            GitignorePatchloom::AlreadyPresent
        );
    }

    #[test]
    fn ensure_gitignore_appends_when_present_without_entry() {
        let dir = tempfile::TempDir::new().unwrap();
        std::fs::write(dir.path().join(".gitignore"), "target/\n").unwrap();
        assert_eq!(
            ensure_gitignore_patchloom(dir.path()).unwrap(),
            GitignorePatchloom::Appended
        );
        let content = std::fs::read_to_string(dir.path().join(".gitignore")).unwrap();
        assert!(content.contains("target/"));
        assert!(content.contains(GITIGNORE_PATCHLOOM_LINE));
    }

    #[test]
    fn find_agent_file_finds_agents_md() {
        let dir = tempfile::TempDir::new().unwrap();
        std::fs::write(dir.path().join("AGENTS.md"), "# Rules\n").unwrap();
        let found = find_agent_file(dir.path()).expect("should find AGENTS.md");
        assert!(found.ends_with("AGENTS.md"));
    }

    #[test]
    fn find_agent_file_preserves_claude_md_case() {
        let dir = tempfile::TempDir::new().unwrap();
        std::fs::write(dir.path().join("Claude.md"), "# Rules\n").unwrap();
        let found = find_agent_file(dir.path()).expect("should find Claude.md");
        assert!(found.ends_with("Claude.md"));
    }

    #[test]
    fn find_agent_file_supports_agents_md_variant() {
        let dir = tempfile::TempDir::new().unwrap();
        std::fs::write(dir.path().join("Agents.md"), "# Rules\n").unwrap();
        let found = find_agent_file(dir.path()).expect("should find Agents.md");
        assert!(found.ends_with("Agents.md"));
    }

    #[test]
    fn generate_completions_writes_file() {
        let dir = tempfile::TempDir::new().unwrap();
        let target = dir.path().join("completions/patchloom");
        generate_completions("bash", &target).unwrap();
        assert!(target.exists());
        let content = std::fs::read_to_string(&target).unwrap();
        assert!(content.contains("patchloom"));
    }

    #[test]
    fn completion_install_path_returns_some_for_known_shells() {
        // Requires HOME to be set (normal in test environments).
        if std::env::var("HOME").is_ok() {
            // strengthened from bare is_some per Test Auditor (gives message on fail)
            let _ = completion_install_path("bash").expect("bash shell path");
            let _ = completion_install_path("zsh").expect("zsh shell path");
            let _ = completion_install_path("fish").expect("fish shell path");
            assert!(completion_install_path("elvish").is_none());
        }
    }
}