patchloom 0.8.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
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
use assert_cmd::Command;
use predicates::prelude::*;
use std::fs;
use std::path::{Path, PathBuf};
use tempfile::TempDir;

/// Convert a path to a string safe for embedding in YAML/TOML values.
/// On Windows, backslashes in paths like `C:\Users\...` are interpreted
/// as escape sequences (`\U` = unicode escape). Forward slashes work
/// fine on Windows and avoid the problem.
fn portable_path_str(p: &Path) -> String {
    p.to_str().unwrap().replace('\\', "/")
}

fn nonexistent_path(name: &str) -> String {
    #[cfg(windows)]
    {
        format!("C:\\patchloom-nonexistent-{name}")
    }
    #[cfg(not(windows))]
    {
        format!("/tmp/patchloom-nonexistent-{name}")
    }
}

fn shell_false() -> &'static str {
    #[cfg(windows)]
    {
        "exit /b 1"
    }
    #[cfg(not(windows))]
    {
        "false"
    }
}

fn shell_exit_1() -> &'static str {
    #[cfg(windows)]
    {
        "exit /b 1"
    }
    #[cfg(not(windows))]
    {
        "exit 1"
    }
}

fn shell_sleep_300() -> &'static str {
    #[cfg(windows)]
    {
        // ping localhost 301 times (1 second apart) to sleep ~300 seconds.
        // Redirect to nul to suppress output.
        "ping -n 301 127.0.0.1 > nul"
    }
    #[cfg(not(windows))]
    {
        "sleep 300"
    }
}

fn shell_touch(path: &Path) -> String {
    #[cfg(windows)]
    {
        let path = path.to_str().unwrap();
        // type nul produces empty output; > redirects it to create the file.
        format!("type nul > \"{path}\"")
    }
    #[cfg(not(windows))]
    {
        let path = path.display();
        format!("touch '{path}'")
    }
}

fn shell_fail_with_secret(secret: &str) -> String {
    #[cfg(windows)]
    {
        format!("cmd /C \"set PATCHLOOM_SECRET={secret}&& exit /b 1\"")
    }
    #[cfg(not(windows))]
    {
        format!("PATCHLOOM_SECRET='{secret}' false")
    }
}

fn shell_test_exists(path: &Path) -> String {
    #[cfg(windows)]
    {
        let path = path.to_str().unwrap();
        format!("if exist \"{path}\" (exit /b 0) else (exit /b 1)")
    }
    #[cfg(not(windows))]
    {
        let path = path.display();
        format!("test -f '{path}'")
    }
}

#[cfg(unix)]
fn run_patchloom_confirm_in_pty_with_env(
    args: &[&str],
    input: &str,
    env: &[(&str, &str)],
) -> std::process::Output {
    let python = r#"
import json, os, pty, subprocess, sys

args = json.loads(os.environ["PATCHLOOM_PTY_ARGS"])
cwd = os.environ["PATCHLOOM_PTY_CWD"]
input_data = os.environ["PATCHLOOM_PTY_INPUT"].encode()
child_env = os.environ.copy()
child_env.update(json.loads(os.environ["PATCHLOOM_PTY_ENV"]))

master_fd, slave_fd = pty.openpty()
proc = subprocess.Popen(
    args,
    cwd=cwd,
    stdin=slave_fd,
    stdout=slave_fd,
    stderr=slave_fd,
    env=child_env,
)
os.close(slave_fd)
if input_data:
    os.write(master_fd, input_data)

output = bytearray()
while True:
    try:
        chunk = os.read(master_fd, 4096)
        if not chunk:
            break
        output.extend(chunk)
    except OSError:
        break

status = proc.wait()
os.close(master_fd)
sys.stdout.buffer.write(output)
sys.exit(status)
"#;
    let full_args = std::iter::once(env!("CARGO_BIN_EXE_patchloom").to_string())
        .chain(args.iter().map(|arg| (*arg).to_string()))
        .collect::<Vec<_>>();
    let env_json = env
        .iter()
        .map(|(key, value)| ((*key).to_string(), (*value).to_string()))
        .collect::<std::collections::BTreeMap<_, _>>();

    std::process::Command::new("python3")
        .arg("-c")
        .arg(python)
        .env(
            "PATCHLOOM_PTY_ARGS",
            serde_json::to_string(&full_args).unwrap(),
        )
        .env("PATCHLOOM_PTY_CWD", repo_root())
        .env("PATCHLOOM_PTY_INPUT", input)
        .env(
            "PATCHLOOM_PTY_ENV",
            serde_json::to_string(&env_json).unwrap(),
        )
        .output()
        .unwrap()
}

#[cfg(unix)]
fn run_patchloom_confirm_in_pty(args: &[&str], input: &str) -> std::process::Output {
    run_patchloom_confirm_in_pty_with_env(args, input, &[])
}

fn assert_patch_apply_error_object(
    output: &std::process::Output,
    expected_exit_code: i32,
    expected_error_substring: &str,
) {
    assert_eq!(output.status.code(), Some(expected_exit_code));
    assert!(String::from_utf8_lossy(&output.stderr).trim().is_empty());

    let json: serde_json::Value = serde_json::from_slice(&output.stdout).unwrap();
    assert_eq!(json["ok"], false);
    assert!(
        json["error"]
            .as_str()
            .unwrap()
            .contains(expected_error_substring)
    );
}

fn git_ok(dir: &Path, args: &[&str]) {
    let output = std::process::Command::new("git")
        .args(args)
        .current_dir(dir)
        .output()
        .unwrap();
    assert!(
        output.status.success(),
        "git {:?} failed\nstdout:{}\nstderr:{}",
        args,
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    );
}

fn init_git_repo_with_committed_file(dir: &Path, file: &str, content: &str) {
    git_ok(dir, &["init"]);
    git_ok(dir, &["config", "user.email", "test@test.com"]);
    git_ok(dir, &["config", "user.name", "Test"]);
    fs::write(dir.join(file), content).unwrap();
    git_ok(dir, &["add", file]);
    git_ok(dir, &["commit", "-m", "init"]);
}

// ---------------------------------------------------------------------------
// search
// ---------------------------------------------------------------------------

// ---------------------------------------------------------------------------
// Shared test helpers (used across multiple test modules)
// ---------------------------------------------------------------------------

fn repo_root() -> PathBuf {
    PathBuf::from(env!("CARGO_MANIFEST_DIR"))
}

fn example_plan_path(name: &str) -> PathBuf {
    repo_root().join("examples").join(name)
}

fn quickstart_path() -> PathBuf {
    repo_root()
        .join("docs")
        .join("getting-started")
        .join("quickstart.md")
}

fn installation_path() -> PathBuf {
    repo_root()
        .join("docs")
        .join("getting-started")
        .join("installation.md")
}

fn agent_test_readme_path() -> PathBuf {
    repo_root().join("tests").join("agent").join("README.md")
}

fn concepts_path() -> PathBuf {
    repo_root()
        .join("docs")
        .join("getting-started")
        .join("concepts.md")
}

fn ci_workflow_path() -> PathBuf {
    repo_root().join(".github").join("workflows").join("ci.yml")
}

fn readme_path() -> PathBuf {
    repo_root().join("README.md")
}

fn launch_announcement_path() -> PathBuf {
    repo_root()
        .join("docs")
        .join("blog")
        .join("launch-announcement.md")
}

fn patchloom_in(cwd: &Path) -> Command {
    let mut cmd = Command::cargo_bin("patchloom").unwrap();
    cmd.arg("--cwd").arg(cwd);
    cmd
}

fn write_fixture_file(root: &Path, relative: &str, content: &str) {
    let path = root.join(relative);
    if let Some(parent) = path.parent() {
        fs::create_dir_all(parent).unwrap();
    }
    fs::write(path, content).unwrap();
}

fn seed_docs_smoke_fixture(root: &Path) {
    write_fixture_file(
        root,
        "Cargo.toml",
        "[package]\nname = \"smoke-fixture\"\nversion = \"0.1.0\"\nedition = \"2021\"\n",
    );
    write_fixture_file(
        root,
        "src/lib.rs",
        "pub mod write;\n\n// TODO: rename old_function\n// TODO: update docs\npub fn old_function() -> &'static str {\n    \"old\"\n}\n",
    );
    write_fixture_file(
        root,
        "src/write.rs",
        "pub fn existing_write() -> &'static str {\n    \"write\"\n}\n",
    );
    write_fixture_file(
        root,
        "src/rename.rs",
        "pub fn old_name() -> &'static str {\n    \"old_name\"\n}\n",
    );
    write_fixture_file(
        root,
        "README.md",
        "# Smoke Fixture\n\nCurrent release: v1.0.0\nDocs version: v0.1.0\n\n## Commands\n\n| Command | Description |\n|---|---|\n| `search` | Search repo |\n",
    );
    write_fixture_file(
        root,
        "CHANGELOG.md",
        "# Changelog\n\n## Unreleased\n\n- Existing entry\n\n## 0.1.0\n\n- Initial release\n",
    );
    write_fixture_file(
        root,
        "AGENTS.md",
        "# AGENTS.md\n\n## Safety rules\n\n- existing rule\n\n## Safety rules\n\n- duplicate rule\n\n## FAQ\n\nQ: How?\nA: Like this.\n\n## License\n\nMIT\n",
    );
    write_fixture_file(
        root,
        "package.json",
        "{\n  \"name\": \"smoke-fixture\",\n  \"version\": \"1.0.0\"\n}\n",
    );
    write_fixture_file(
        root,
        "config.json",
        "{\n  \"database\": {\n    \"host\": \"localhost\",\n    \"port\": 3306\n  },\n  \"allowed_origins\": [\"http://localhost:3000\"],\n  \"deprecated_field\": true\n}\n",
    );
    write_fixture_file(
        root,
        "config.yaml",
        "users:\n  - name: alice\n    active: true\n  - name: bob\n    active: false\n",
    );
}

fn extract_markdown_code_block_after(markdown: &str, marker: &str, language: &str) -> String {
    let (_, after_marker) = markdown
        .split_once(marker)
        .expect("marker should exist in markdown");
    let fence = format!("```{language}\n");
    let (_, after_fence) = after_marker
        .split_once(&fence)
        .expect("fenced code block should exist after marker");
    let (block, _) = after_fence
        .split_once("\n```")
        .expect("fenced code block should terminate");
    block.to_string()
}

fn reference_path() -> PathBuf {
    repo_root().join("docs").join("reference").join("README.md")
}

fn extract_braced_block(source: &str, anchor: &str) -> String {
    let anchor_start = source
        .find(anchor)
        .unwrap_or_else(|| panic!("anchor `{anchor}` should exist"));
    let body_start = source[anchor_start..]
        .find('{')
        .map(|offset| anchor_start + offset)
        .expect("anchor should be followed by `{`");

    let mut depth = 0usize;
    for (offset, ch) in source[body_start..].char_indices() {
        match ch {
            '{' => depth += 1,
            '}' => {
                depth -= 1;
                if depth == 0 {
                    return source[body_start + 1..body_start + offset].to_string();
                }
            }
            _ => {}
        }
    }

    panic!("anchor `{anchor}` should have a matching closing brace");
}

fn read_anchored_block(path: &Path, anchor: &str) -> String {
    let source = fs::read_to_string(path).unwrap();
    extract_braced_block(&source, anchor)
}

fn camel_to_kebab(name: &str) -> String {
    let mut out = String::new();
    for (idx, ch) in name.chars().enumerate() {
        if ch.is_uppercase() {
            if idx > 0 {
                out.push('-');
            }
            for lower in ch.to_lowercase() {
                out.push(lower);
            }
        } else {
            out.push(ch);
        }
    }
    out
}

fn snake_to_kebab(name: &str) -> String {
    name.replace('_', "-")
}

fn collect_enum_variant_cli_names(path: &Path, anchor: &str) -> Vec<String> {
    let block = read_anchored_block(path, anchor);
    let re = regex::Regex::new(r"(?m)^\s*([A-Z][A-Za-z0-9]*)(?:\s*\(|\s*\{|,).*$").unwrap();
    re.captures_iter(&block)
        .map(|caps| camel_to_kebab(&caps[1]))
        .collect::<std::collections::BTreeSet<_>>()
        .into_iter()
        .collect()
}

fn collect_struct_field_names(path: &Path, anchor: &str) -> Vec<String> {
    let block = read_anchored_block(path, anchor);
    let re = regex::Regex::new(r"(?m)^\s*pub\s+([a-z_]+)\s*:").unwrap();
    re.captures_iter(&block)
        .map(|caps| caps[1].to_string())
        .collect::<std::collections::BTreeSet<_>>()
        .into_iter()
        .collect()
}

fn collect_serde_rename_values(path: &Path, anchor: &str) -> Vec<String> {
    let block = read_anchored_block(path, anchor);
    // Match only variant-level renames (which always have an alias), not field-level renames.
    let re = regex::Regex::new(r#"#\[serde\(rename = "([^"]+)",\s*alias"#).unwrap();
    re.captures_iter(&block)
        .map(|caps| caps[1].to_string())
        .collect::<std::collections::BTreeSet<_>>()
        .into_iter()
        .collect()
}

fn collect_batch_operation_names(path: &Path) -> Vec<String> {
    let block = read_anchored_block(path, "match op");
    let re = regex::Regex::new(r#"(?m)^\s*"([^"]+)"\s*=>"#).unwrap();
    re.captures_iter(&block)
        .map(|caps| caps[1].to_string())
        .collect::<std::collections::BTreeSet<_>>()
        .into_iter()
        .collect()
}

fn collect_source_ref_markers(path: &Path) -> Vec<String> {
    let source = fs::read_to_string(path).unwrap();
    let re = regex::Regex::new(r"(?m)^\s*//\s*ref:([a-z0-9._:-]+)\s*$").unwrap();
    re.captures_iter(&source)
        .map(|caps| caps[1].to_string())
        .collect::<std::collections::BTreeSet<_>>()
        .into_iter()
        .collect()
}

fn expected_reference_markers() -> Vec<String> {
    let root = repo_root();
    let cmd_dir = root.join("src").join("cmd");
    let global_path = root.join("src").join("cli").join("global.rs");
    let plan_path = root.join("src").join("plan.rs");

    let mut markers = std::collections::BTreeSet::new();

    for name in collect_enum_variant_cli_names(&cmd_dir.join("mod.rs"), "pub enum Command") {
        markers.insert(format!("command:{name}"));
    }
    for name in collect_enum_variant_cli_names(&cmd_dir.join("doc.rs"), "pub enum DocAction") {
        markers.insert(format!("doc-action:{name}"));
    }
    for name in collect_enum_variant_cli_names(&cmd_dir.join("md.rs"), "pub enum MdAction") {
        markers.insert(format!("md-action:{name}"));
    }
    for name in collect_enum_variant_cli_names(&cmd_dir.join("patch.rs"), "pub enum PatchAction") {
        markers.insert(format!("patch-action:{name}"));
    }
    for name in collect_enum_variant_cli_names(&cmd_dir.join("tidy.rs"), "pub enum TidyAction") {
        markers.insert(format!("tidy-action:{name}"));
    }
    for name in collect_struct_field_names(&plan_path, "pub struct Plan") {
        markers.insert(format!("tx-field:{name}"));
    }
    for name in collect_serde_rename_values(&plan_path, "pub enum Operation") {
        markers.insert(format!("tx-op:{name}"));
    }

    let write_flags: std::collections::BTreeSet<String> =
        collect_struct_field_names(&global_path, "pub struct WriteFlags")
            .into_iter()
            .collect();
    for name in &write_flags {
        markers.insert(format!("write-flag:{}", snake_to_kebab(name)));
    }
    for name in collect_struct_field_names(&global_path, "pub struct GlobalFlags") {
        if !write_flags.contains(&name) {
            markers.insert(format!("global-flag:{}", snake_to_kebab(&name)));
        }
    }

    for entry in fs::read_dir(&cmd_dir).unwrap() {
        let path = entry.unwrap().path();
        if path.extension().and_then(|ext| ext.to_str()) != Some("rs") {
            continue;
        }

        for marker in collect_source_ref_markers(&path) {
            markers.insert(marker);
        }
    }

    markers.into_iter().collect()
}

fn reference_markers(reference: &str) -> Vec<(String, usize)> {
    let re = regex::Regex::new(r#"<!--\s*ref:([a-z0-9._:-]+)\s*-->"#).unwrap();
    re.captures_iter(reference)
        .map(|caps| (caps[1].to_string(), caps.get(0).unwrap().start()))
        .collect()
}

fn reference_section<'a>(reference: &'a str, markers: &[(String, usize)], marker: &str) -> &'a str {
    let idx = markers
        .iter()
        .position(|(name, _)| name == marker)
        .unwrap_or_else(|| panic!("marker `{marker}` should exist"));
    let start = markers[idx].1;
    let end = markers
        .get(idx + 1)
        .map(|(_, pos)| *pos)
        .unwrap_or(reference.len());
    &reference[start..end]
}

fn reference_doc_validation_errors(reference: &str) -> Vec<String> {
    let markers = reference_markers(reference);
    let actual_marker_names: std::collections::BTreeSet<String> =
        markers.iter().map(|(name, _)| name.clone()).collect();
    let expected_markers = expected_reference_markers();
    let missing_markers: Vec<String> = expected_markers
        .iter()
        .filter(|marker| !actual_marker_names.contains(*marker))
        .cloned()
        .collect();
    let mut errors = Vec::new();

    if !missing_markers.is_empty() {
        errors.push(format!(
            "reference doc is missing markers for:\n{}",
            missing_markers.join("\n")
        ));
    }

    for marker in expected_markers {
        if !actual_marker_names.contains(&marker) {
            continue;
        }

        let section = reference_section(reference, &markers, &marker);
        if !section.contains("**Use when:**") {
            errors.push(format!(
                "reference section `{marker}` must include a `Use when` stanza"
            ));
        }
    }

    errors
}

fn reference_without_use_when(reference: &str, marker: &str) -> String {
    let markers = reference_markers(reference);
    let section = reference_section(reference, &markers, marker);
    let use_when_start = section
        .find("- **Use when:**")
        .unwrap_or_else(|| panic!("reference section `{marker}` should include `Use when`"));
    let use_when_end = section[use_when_start..]
        .find('\n')
        .map(|offset| use_when_start + offset + 1)
        .unwrap_or(section.len());
    let broken_section = format!("{}{}", &section[..use_when_start], &section[use_when_end..]);

    reference.replacen(section, &broken_section, 1)
}

fn has_mcp_support() -> bool {
    Command::cargo_bin("patchloom")
        .unwrap()
        .arg("mcp-server")
        .arg("--help")
        .ok()
        .is_ok()
}

#[cfg(feature = "mcp-http")]
fn has_mcp_http_support() -> bool {
    let output = Command::cargo_bin("patchloom")
        .unwrap()
        .arg("mcp-server")
        .arg("--help")
        .ok()
        .expect("mcp-server --help failed");
    let stdout = String::from_utf8_lossy(&output.stdout);
    stdout.contains("--http")
}

/// Spawn `patchloom mcp-server` in a tempdir and return a connected MCP client.
async fn spawn_mcp_client(cwd: &Path) -> rmcp::service::RunningService<rmcp::RoleClient, ()> {
    use rmcp::ServiceExt;
    use rmcp::transport::TokioChildProcess;

    let bin = assert_cmd::cargo::cargo_bin("patchloom");
    let mut cmd = tokio::process::Command::new(bin);
    cmd.arg("mcp-server").current_dir(cwd);

    let transport = TokioChildProcess::new(cmd).expect("failed to spawn patchloom mcp-server");
    ().serve(transport)
        .await
        .expect("failed to connect MCP client")
}

/// Helper: call a tool and return the text content from the first Content item.
async fn call_tool_text(
    client: &rmcp::service::RunningService<rmcp::RoleClient, ()>,
    tool: impl Into<String>,
    args: serde_json::Value,
) -> (bool, String) {
    let params = rmcp::model::CallToolRequestParams::new(tool.into())
        .with_arguments(serde_json::from_value(args).unwrap());
    let result = match client.peer().call_tool(params).await {
        Ok(r) => r,
        Err(e) => {
            return (true, format!("MCP call error: {e}"));
        }
    };
    let is_error = result.is_error.unwrap_or(false);
    let text = result
        .content
        .first()
        .and_then(|c| match c {
            rmcp::model::ContentBlock::Text(t) => Some(t.text.clone()),
            _ => None,
        })
        .unwrap_or_default();
    (is_error, text)
}

/// Helper to call an MCP tool and parse the response text as JSON Value.
/// This enables honest structured assertions for tools like git_status
/// (instead of fragile substring `contains` on the whole pretty-printed output).
async fn call_tool_value(
    client: &rmcp::service::RunningService<rmcp::RoleClient, ()>,
    tool: impl Into<String>,
    args: serde_json::Value,
) -> (bool, serde_json::Value) {
    let (is_error, text) = call_tool_text(client, tool, args).await;
    let val =
        serde_json::from_str(&text).unwrap_or_else(|_| serde_json::json!({ "raw_text": text }));
    (is_error, val)
}

// ---------------------------------------------------------------------------
// Backup pruning integration tests (#371)
// ---------------------------------------------------------------------------

#[cfg(unix)]
fn readonly_dir_blocks_writes(dir: &std::path::Path) -> bool {
    use std::os::unix::fs::PermissionsExt;

    fs::set_permissions(dir, fs::Permissions::from_mode(0o555)).unwrap();
    let probe = dir.join(".write_probe");
    match fs::write(&probe, "x") {
        Ok(()) => {
            let _ = fs::remove_file(&probe);
            fs::set_permissions(dir, fs::Permissions::from_mode(0o755)).unwrap();
            false
        }
        Err(_) => true,
    }
}

mod agent_rules_tests;
mod ast_tests;
mod batch_tests;
mod cli_flags_tests;
mod create_tests;
mod delete_tests;
mod doc_tests;
mod explain_tests;
mod file_ops_tests;
mod init_tests;
mod mcp_tests;
mod mcp_tool_tests;
mod md_tests;
mod misc_tests;
mod parse_tests;
mod patch_tests;
mod read_tests;
mod reference_docs_tests;
mod rename_tests;
mod replace_tests;
mod schema_tests;
mod search_tests;
mod smoke_tests;
mod status_tests;
mod tidy_tests;
mod tx_tests;
mod undo_tests;