ast-bro 2.2.0

Fast, AST-based code-navigation: shape, public API, deps & call graphs, hybrid semantic search, structural rewrite. MCP server included.
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
use std::path::Path;

use serde_json::Value;

use super::io::{atomic_write, read_optional};
use super::json_hook;
use super::json_object;
use super::marker_block::{self, ApplyOutcome};
use super::toml_object;
use super::{Change, InstallOpts, Status};

use toml_edit::{DocumentMut, Table};

/// Legacy MCP server name used before the ast-outline → ast-bro rename.
/// Uninstall routines check for this in addition to the current name so
/// ghost entries from older installs are cleaned up.
pub const OLD_MCP_SERVER_NAME: &str = "ast-outline";

pub fn install_prompt_in(
    path: &Path,
    snippet: &str,
    opts: &InstallOpts,
) -> Result<Change, String> {
    let existing = read_optional(path)?.unwrap_or_default();

    let body = snippet.trim_end_matches('\n').to_string() + "\n";
    let (new_contents, outcome) = marker_block::apply(
        &existing,
        &body,
        &body,
        snippet,
        env!("CARGO_PKG_VERSION"),
        opts.force,
    );
    match outcome {
        ApplyOutcome::UserEditsBlocked(diff) => Err(format!(
            "{}: user edits inside marker block; pass --force to overwrite\n{}",
            path.display(),
            diff
        )),
        ApplyOutcome::Appended
            if !opts.force && marker_block::has_unmanaged_brand_content(&existing) =>
        {
            Err(format!(
                "{}: user-written ast-bro content outside marker block; pass --force to overwrite",
                path.display()
            ))
        }
        _ => {
            if existing == new_contents {
                return Ok(Change::Skipped {
                    path: path.to_path_buf(),
                    reason: "already up to date".into(),
                });
            }
            if !opts.dry_run {
                atomic_write(path, &new_contents)?;
            }
            Ok(if existing.is_empty() {
                Change::Created(path.to_path_buf())
            } else {
                Change::Updated(path.to_path_buf())
            })
        }
    }
}

/// Like `install_prompt_in` but seeds an empty file with `frontmatter` before
/// appending the marker block. This keeps valid YAML frontmatter at offset 0
/// for Claude Code sub-agent files, while still allowing users who already
/// have their own `.claude/agents/<Name>.md` to retain their customizations.
pub fn install_subagent_in(
    path: &Path,
    frontmatter: &str,
    snippet: &str,
    opts: &InstallOpts,
) -> Result<Change, String> {
    let on_disk = read_optional(path)?.unwrap_or_default();
    let is_new = on_disk.is_empty();
    // When the file doesn't exist yet, seed `existing` with the frontmatter so
    // marker_block::apply appends the block after it rather than at offset 0.
    let existing = if is_new { frontmatter.to_string() } else { on_disk.clone() };

    let body = snippet.trim_end_matches('\n').to_string() + "\n";
    let (new_contents, outcome) = marker_block::apply(
        &existing,
        &body,
        &body,
        snippet,
        env!("CARGO_PKG_VERSION"),
        opts.force,
    );
    match outcome {
        ApplyOutcome::UserEditsBlocked(diff) => Err(format!(
            "{}: user edits inside marker block; pass --force to overwrite\n{}",
            path.display(),
            diff
        )),
        // `!is_new` is defense-in-depth: when the file doesn't exist yet,
        // `on_disk` is empty and `has_unmanaged_brand_content` would return
        // false anyway, but skipping the call also makes the intent clear —
        // the snippet-shape guard only protects pre-existing user content.
        // The seeded `frontmatter` is content *we* control, so it would be
        // wrong to flag it as a "user-written conflict" even hypothetically.
        ApplyOutcome::Appended
            if !opts.force && !is_new && marker_block::has_unmanaged_brand_content(&on_disk) =>
        {
            Err(format!(
                "{}: user-written ast-bro content outside marker block; pass --force to overwrite",
                path.display()
            ))
        }
        _ => {
            if on_disk == new_contents {
                return Ok(Change::Skipped {
                    path: path.to_path_buf(),
                    reason: "already up to date".into(),
                });
            }
            if !opts.dry_run {
                atomic_write(path, &new_contents)?;
            }
            Ok(if is_new {
                Change::Created(path.to_path_buf())
            } else {
                Change::Updated(path.to_path_buf())
            })
        }
    }
}

pub fn install_json_hook_in<F>(
    path: &Path,
    hook_path: &[&str],
    entry: Value,
    matches: F,
    opts: &InstallOpts,
) -> Result<Change, String>
where
    F: Fn(&Value) -> bool,
{
    let existing = read_optional(path)?.unwrap_or_else(|| "{}".into());
    let mut root: Value = serde_json::from_str(&existing)
        .map_err(|e| format!("parse {}: {}", path.display(), e))?;
    let modified = json_hook::upsert(&mut root, hook_path, entry, matches);
    if !modified {
        return Ok(Change::Skipped {
            path: path.to_path_buf(),
            reason: "already up to date".into(),
        });
    }
    let new_contents = serde_json::to_string_pretty(&root).unwrap() + "\n";
    if !opts.dry_run {
        atomic_write(path, &new_contents)?;
    }
    Ok(if existing.trim() == "{}" || existing.is_empty() {
        Change::Created(path.to_path_buf())
    } else {
        Change::Updated(path.to_path_buf())
    })
}

pub fn uninstall_prompt_in(path: &Path, opts: &InstallOpts) -> Result<Option<Change>, String> {
    let Some(existing) = read_optional(path)? else {
        return Ok(None);
    };
    let (out, removed) = marker_block::remove(&existing);
    if !removed {
        return Ok(None);
    }
    if !opts.dry_run {
        atomic_write(path, &out)?;
    }
    Ok(Some(Change::Removed(path.to_path_buf())))
}

pub fn uninstall_json_hook_in<F>(
    path: &Path,
    hook_path: &[&str],
    matches: F,
    opts: &InstallOpts,
) -> Result<Option<Change>, String>
where
    F: Fn(&Value) -> bool,
{
    let Some(existing) = read_optional(path)? else {
        return Ok(None);
    };
    let mut root: Value = serde_json::from_str(&existing)
        .map_err(|e| format!("parse {}: {}", path.display(), e))?;
    if !json_hook::remove(&mut root, hook_path, matches) {
        return Ok(None);
    }
    let new_contents = serde_json::to_string_pretty(&root).unwrap() + "\n";
    if !opts.dry_run {
        atomic_write(path, &new_contents)?;
    }
    Ok(Some(Change::Removed(path.to_path_buf())))
}

pub fn install_mcp_in(
    path: &Path,
    key_path: &[&str],
    name: &str,
    old_name: &str,
    entry: Value,
    opts: &InstallOpts,
) -> Result<Change, String> {
    let existing = read_optional(path)?.unwrap_or_else(|| "{}".into());
    let mut root: Value = serde_json::from_str(&existing)
        .map_err(|e| format!("parse {}: {}", path.display(), e))?;

    let mut modified = false;
    // Remove old name if it exists (migration)
    if json_object::remove(&mut root, key_path, old_name) {
        modified = true;
    }
    // Upsert current name
    if json_object::upsert(&mut root, key_path, name, entry) {
        modified = true;
    }

    if !modified {
        return Ok(Change::Skipped {
            path: path.to_path_buf(),
            reason: "already up to date".into(),
        });
    }
    let new_contents = serde_json::to_string_pretty(&root).unwrap() + "\n";
    if !opts.dry_run {
        atomic_write(path, &new_contents)?;
    }
    Ok(if existing.trim() == "{}" || existing.is_empty() {
        Change::Created(path.to_path_buf())
    } else {
        Change::Updated(path.to_path_buf())
    })
}

pub fn uninstall_json_object_in(
    path: &Path,
    key_path: &[&str],
    key: &str,
    opts: &InstallOpts,
) -> Result<Option<Change>, String> {
    let Some(existing) = read_optional(path)? else {
        return Ok(None);
    };
    let mut root: Value = serde_json::from_str(&existing)
        .map_err(|e| format!("parse {}: {}", path.display(), e))?;
    if !json_object::remove(&mut root, key_path, key) {
        return Ok(None);
    }
    let new_contents = serde_json::to_string_pretty(&root).unwrap() + "\n";
    if !opts.dry_run {
        atomic_write(path, &new_contents)?;
    }
    Ok(Some(Change::Removed(path.to_path_buf())))
}

pub fn install_toml_mcp_in(
    path: &Path,
    parent: &str,
    name: &str,
    old_name: &str,
    entry: Table,
    opts: &InstallOpts,
) -> Result<Change, String> {
    let existing = read_optional(path)?.unwrap_or_default();
    let mut doc: DocumentMut = existing
        .parse()
        .map_err(|e| format!("parse {}: {}", path.display(), e))?;

    let mut modified = false;
    // Remove old name if it exists (migration)
    if toml_object::remove(&mut doc, parent, old_name) {
        modified = true;
    }
    // Upsert current name
    if toml_object::upsert(&mut doc, parent, name, entry) {
        modified = true;
    }

    if !modified {
        return Ok(Change::Skipped {
            path: path.to_path_buf(),
            reason: "already up to date".into(),
        });
    }
    let new_contents = doc.to_string();
    if !opts.dry_run {
        atomic_write(path, &new_contents)?;
    }
    Ok(if existing.is_empty() {
        Change::Created(path.to_path_buf())
    } else {
        Change::Updated(path.to_path_buf())
    })
}

pub fn uninstall_toml_object_in(
    path: &Path,
    parent: &str,
    key: &str,
    opts: &InstallOpts,
) -> Result<Option<Change>, String> {
    let Some(existing) = read_optional(path)? else {
        return Ok(None);
    };
    let mut doc: DocumentMut = existing
        .parse()
        .map_err(|e| format!("parse {}: {}", path.display(), e))?;
    if !toml_object::remove(&mut doc, parent, key) {
        return Ok(None);
    }
    if !opts.dry_run {
        atomic_write(path, &doc.to_string())?;
    }
    Ok(Some(Change::Removed(path.to_path_buf())))
}

/// Install a plain file (no marker block, no JSON merge). Idempotent by
/// byte-identical comparison. Used for files we own end-to-end (e.g. the
/// Claude Code `SKILL.md`, where YAML frontmatter forbids comment markers).
pub fn install_plain_file_in(
    path: &Path,
    contents: &str,
    opts: &InstallOpts,
) -> Result<Change, String> {
    let existing = read_optional(path)?;
    if existing.as_deref() == Some(contents) {
        return Ok(Change::Skipped {
            path: path.to_path_buf(),
            reason: "already up to date".into(),
        });
    }
    if !opts.dry_run {
        atomic_write(path, contents)?;
    }
    Ok(if existing.is_some() {
        Change::Updated(path.to_path_buf())
    } else {
        Change::Created(path.to_path_buf())
    })
}

/// Remove a plain file we wrote, but only if its first line still
/// contains `expected_marker` — guards against deleting a file the user
/// has fully replaced with their own content. Also tries to remove the
/// parent directory (succeeds only if empty), keeping `~/.claude/skills/`
/// itself intact when other skills are present.
pub fn uninstall_plain_file_in(
    path: &Path,
    expected_marker: &str,
    opts: &InstallOpts,
) -> Result<Option<Change>, String> {
    let Some(existing) = read_optional(path)? else {
        return Ok(None);
    };
    let first_line = existing.lines().next().unwrap_or("");
    let still_ours = existing.contains(expected_marker) || first_line.contains(expected_marker);
    if !still_ours {
        return Ok(None);
    }
    if !opts.dry_run {
        std::fs::remove_file(path)
            .map_err(|e| format!("remove {}: {}", path.display(), e))?;
        if let Some(parent) = path.parent() {
            let _ = std::fs::remove_dir(parent); // succeeds only if empty
        }
    }
    Ok(Some(Change::Removed(path.to_path_buf())))
}

pub fn status_for<F>(
    prompt_path: Option<&Path>,
    settings_path: Option<&Path>,
    hook_path: &[&str],
    matches: F,
) -> Status
where
    F: Fn(&Value) -> bool,
{
    let mut s = Status::default();
    if let Some(pp) = prompt_path {
        if let Ok(Some(contents)) = read_optional(pp) {
            s.prompt_version = marker_block::installed_version(&contents);
            s.prompt_installed = s.prompt_version.is_some();
        }
    }
    if let Some(sp) = settings_path {
        if let Ok(Some(contents)) = read_optional(sp) {
            if let Ok(root) = serde_json::from_str::<Value>(&contents) {
                s.hook_installed = json_hook::is_installed(&root, hook_path, matches);
            }
        }
    }
    s
}

pub fn status_for_prompt_only(prompt_path: Option<&Path>) -> Status {
    let mut s = Status::default();
    if let Some(pp) = prompt_path {
        if let Ok(Some(contents)) = read_optional(pp) {
            s.prompt_version = marker_block::installed_version(&contents);
            s.prompt_installed = s.prompt_version.is_some();
        }
    }
    s
}

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

    #[test]
    fn install_prompt_rejects_existing_snippet_like_content() {
        let dir = TempDir::new().unwrap();
        let path = dir.path().join("CLAUDE.md");
        // User pasted snippet-shaped content (backticked brand) without our markers.
        std::fs::write(&path, "Use `ast-bro` to explore the code.\n").unwrap();
        let err = install_prompt_in(&path, "## Snippet\n", &InstallOpts::default()).unwrap_err();
        assert!(err.contains("user-written ast-bro content outside marker block"));
        // File must not be touched on rejection.
        let after = std::fs::read_to_string(&path).unwrap();
        assert_eq!(after, "Use `ast-bro` to explore the code.\n");
    }

    #[test]
    fn install_prompt_force_overrides_snippet_check() {
        let dir = TempDir::new().unwrap();
        let path = dir.path().join("CLAUDE.md");
        std::fs::write(&path, "Use `ast-bro` to explore.\n").unwrap();
        let opts = InstallOpts { force: true, ..Default::default() };
        let change = install_prompt_in(&path, "## Snippet\n", &opts).unwrap();
        assert!(matches!(change, Change::Updated(_)));
        let after = std::fs::read_to_string(&path).unwrap();
        assert!(after.contains("<!-- ast-bro:begin"));
        // Original line is preserved above the new block.
        assert!(after.starts_with("Use `ast-bro` to explore.\n"));
    }

    #[test]
    fn install_prompt_allows_unrelated_existing_content() {
        let dir = TempDir::new().unwrap();
        let path = dir.path().join("CLAUDE.md");
        std::fs::write(&path, "# My project notes\nNothing branded here.\n").unwrap();
        let change = install_prompt_in(&path, "## Snippet\n", &InstallOpts::default()).unwrap();
        assert!(matches!(change, Change::Updated(_)));
        let after = std::fs::read_to_string(&path).unwrap();
        assert!(after.contains("<!-- ast-bro:begin"));
    }

    #[test]
    fn install_prompt_allows_casual_prose_mention() {
        // Plain prose mention isn't snippet-shaped — install should proceed.
        let dir = TempDir::new().unwrap();
        let path = dir.path().join("CLAUDE.md");
        std::fs::write(&path, "Our team uses ast-bro among other tools.\n").unwrap();
        let change = install_prompt_in(&path, "## Snippet\n", &InstallOpts::default()).unwrap();
        assert!(matches!(change, Change::Updated(_)));
        let after = std::fs::read_to_string(&path).unwrap();
        assert!(after.contains("<!-- ast-bro:begin"));
    }
}