patchloom 0.33.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
use super::*;

#[test]
fn test_mcp_setup_documents_search_files_modes() {
    let doc = fs::read_to_string(repo_root().join("docs/getting-started/mcp-setup.md")).unwrap();
    assert!(doc.contains("literal, case-insensitive, count, file-only (`files_with_matches` / `files_without_match`), multiline, invert-match, and assert-count modes"));
}

#[test]
fn test_mcp_setup_documents_surface_core_honesty() {
    let doc = fs::read_to_string(repo_root().join("docs/getting-started/mcp-setup.md")).unwrap();
    assert!(
        doc.contains("PATCHLOOM_MCP_SURFACE"),
        "mcp-setup must document surface env"
    );
    assert!(
        doc.contains("match the active surface"),
        "mcp-setup must document surface-aware instructions"
    );
    assert!(
        doc.contains("not a capability sandbox for the plan catalog"),
        "mcp-setup must clarify execute_plan is not sandboxed by core surface"
    );
}

/// MCP execute_plan must not be documented as running format/validate like CLI tx.
#[test]
fn test_mcp_setup_execute_plan_strips_lifecycle() {
    let doc = fs::read_to_string(repo_root().join("docs/getting-started/mcp-setup.md")).unwrap();
    assert!(
        doc.contains("MCP `execute_plan` strips `format`/`validate`")
            || doc.contains("strips `format` and `validate`"),
        "mcp-setup must state that MCP strips format/validate"
    );
    assert!(
        !doc.contains("format` steps, `validate` steps: same as CLI"),
        "mcp-setup must not say MCP plans run format/validate like CLI tx"
    );
}

/// doc_update filters via the selector string, not a predicate field.
#[test]
fn test_mcp_setup_doc_update_selector_not_predicate_field() {
    let doc = fs::read_to_string(repo_root().join("docs/getting-started/mcp-setup.md")).unwrap();
    assert!(
        !doc.contains("| `doc_update` | Update array elements matching a predicate |"),
        "doc_update must not be described as taking a predicate field"
    );
    let update_idx = doc
        .find("`doc_update`")
        .expect("mcp-setup must list doc_update");
    let row = doc[update_idx..].lines().next().unwrap_or("");
    assert!(
        row.contains("selector") && (row.contains("wildcard") || row.contains("wildcards")),
        "doc_update row must mention selector predicates/wildcards: {row}"
    );
    assert!(
        row.contains("doc_delete_where"),
        "doc_update row must point at doc_delete_where for the predicate tool: {row}"
    );
}

/// #2060: setup docs must list package version + protocol_version on server_info
/// (not only cwd/surface), matching the tool payload and agent-rules.
#[test]
fn test_mcp_setup_documents_server_info_version_fields() {
    let doc = fs::read_to_string(repo_root().join("docs/getting-started/mcp-setup.md")).unwrap();
    assert!(
        doc.contains("protocol_version"),
        "mcp-setup must document server_info protocol_version after #2060"
    );
    assert!(
        doc.contains("package `version`") || doc.contains("package \"version\""),
        "mcp-setup must document server_info package version after #2060"
    );
    assert!(
        doc.contains("tool_count"),
        "mcp-setup must keep documenting server_info tool_count"
    );
}

/// Invalid `PATCHLOOM_MCP_SURFACE` must fail closed at process start (#1994).
#[cfg(feature = "mcp")]
#[test]
fn test_mcp_surface_invalid_env_fails_closed() {
    if !has_mcp_support() {
        return;
    }
    Command::cargo_bin("patchloom")
        .unwrap()
        .args(["mcp-server"])
        .env("PATCHLOOM_MCP_SURFACE", "tiny")
        .assert()
        .failure()
        .stderr(predicates::str::contains("PATCHLOOM_MCP_SURFACE"));
}

/// Subprocess path: `from_env` + list_tools (unit tests inject surface without env).
#[cfg(feature = "mcp")]
#[tokio::test]
async fn test_mcp_surface_core_env_lists_eleven_tools() {
    if !has_mcp_support() {
        return;
    }
    let dir = TempDir::new().unwrap();
    let client = spawn_mcp_client_with_env(dir.path(), &[("PATCHLOOM_MCP_SURFACE", "core")]).await;
    let tools = client.peer().list_all_tools().await.unwrap();
    let names: std::collections::BTreeSet<_> =
        tools.iter().map(|t| t.name.as_ref().to_string()).collect();
    assert_eq!(names.len(), 11, "core pack is 11 tools, got {names:?}");
    for required in [
        "read_file",
        "search_files",
        "list_files",
        "replace_text",
        "batch_replace",
        "doc_get",
        "doc_set",
        "doc_query",
        "md_replace_section",
        "execute_plan",
        "server_info",
    ] {
        assert!(names.contains(required), "missing core tool {required}");
    }
    assert!(!names.contains("create_file"));
    assert!(!names.contains("ast_list"));

    let info = client.peer_info().expect("peer info");
    let instructions = info.instructions.as_deref().unwrap_or("");
    assert!(
        instructions.contains("PATCHLOOM_MCP_SURFACE=core"),
        "handshake instructions must name core surface"
    );
    assert!(
        !instructions.contains("create_file"),
        "core instructions must not advertise create_file"
    );

    let params = rmcp::model::CallToolRequestParams::new("server_info");
    let result = client.peer().call_tool(params).await.unwrap();
    let text = match result.content.first().unwrap() {
        rmcp::model::ContentBlock::Text(t) => &t.text,
        _ => panic!("expected text"),
    };
    let v: serde_json::Value = serde_json::from_str(text).unwrap();
    assert_eq!(v["surface"], "core");
    assert_eq!(v["tool_count"], 11);
    client.cancel().await.unwrap();
}

#[test]
fn test_mcp_setup_documents_text_file_skip_semantics() {
    let doc = fs::read_to_string(repo_root().join("docs/getting-started/mcp-setup.md")).unwrap();
    assert!(doc.contains("Binary and invalid UTF-8 files are skipped"));
}

#[cfg(feature = "mcp-http")]
#[test]
fn test_mcp_http_port_requires_http_flag() {
    if !has_mcp_http_support() {
        return;
    }
    Command::cargo_bin("patchloom")
        .unwrap()
        .args(["mcp-server", "--port", "3000"])
        .assert()
        .failure()
        .stderr(predicates::str::contains("--http"));
}

#[cfg(feature = "mcp-http")]
#[test]
fn test_mcp_http_host_requires_http_flag() {
    if !has_mcp_http_support() {
        return;
    }
    Command::cargo_bin("patchloom")
        .unwrap()
        .args(["mcp-server", "--host", "0.0.0.0"])
        .assert()
        .failure()
        .stderr(predicates::str::contains("--http"));
}

/// Unauthenticated `--host 0.0.0.0` must fail closed before bind.
#[cfg(feature = "mcp-http")]
#[test]
fn test_mcp_http_non_loopback_refused_without_allow_flag() {
    if !has_mcp_http_support() {
        return;
    }
    let output = Command::cargo_bin("patchloom")
        .unwrap()
        .args(["--json", "mcp-server", "--http", "--host", "0.0.0.0"])
        .output()
        .unwrap();
    assert!(
        !output.status.success(),
        "non-loopback HTTP must fail without --allow-unauthenticated"
    );
    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);
    let combined = format!("{stdout}{stderr}");
    assert!(
        !combined.contains("listening"),
        "must refuse before bind, got: {combined}"
    );
    let v: serde_json::Value = serde_json::from_str(stdout.trim()).unwrap_or_else(|e| {
        panic!("expected JSON invalid_input on stdout: {e}; stdout={stdout} stderr={stderr}")
    });
    assert_eq!(v["error_kind"], "invalid_input", "{v}");
    let msg = v["error"].as_str().unwrap_or_default();
    assert!(
        msg.contains("--allow-unauthenticated"),
        "error must name the opt-in flag: {msg}"
    );
    assert!(
        msg.contains("no authentication") || msg.contains("unauthenticated"),
        "error must say HTTP has no auth: {msg}"
    );
}

/// Loopback `--http` still starts without `--allow-unauthenticated`.
#[cfg(feature = "mcp-http")]
#[tokio::test]
async fn test_mcp_http_loopback_starts_without_allow_flag() {
    if !has_mcp_http_support() {
        return;
    }
    let dir = TempDir::new().unwrap();
    let bin = assert_cmd::cargo::cargo_bin("patchloom");
    let mut child = tokio::process::Command::new(&bin)
        .args(["mcp-server", "--http", "--host", "127.0.0.1", "--port", "0"])
        .current_dir(dir.path())
        .stderr(std::process::Stdio::piped())
        .stdout(std::process::Stdio::null())
        .stdin(std::process::Stdio::null())
        .spawn()
        .expect("failed to spawn loopback mcp-server --http");

    let stderr = child.stderr.take().unwrap();
    let mut reader = tokio::io::BufReader::new(stderr);
    let mut line = String::new();
    tokio::io::AsyncBufReadExt::read_line(&mut reader, &mut line)
        .await
        .expect("failed to read loopback HTTP banner");
    assert!(
        line.contains("MCP HTTP server listening"),
        "loopback --http must start without --allow-unauthenticated: {line}"
    );
    assert!(
        line.contains("127.0.0.1"),
        "banner should show loopback bind: {line}"
    );
    child.kill().await.ok();
}

/// `--quiet` and `--json` must not print the HTTP listening banner.
#[cfg(feature = "mcp-http")]
#[tokio::test]
async fn test_mcp_http_quiet_suppresses_banner() {
    if !has_mcp_http_support() {
        return;
    }
    let dir = TempDir::new().unwrap();
    let bin = assert_cmd::cargo::cargo_bin("patchloom");
    let mut child = tokio::process::Command::new(&bin)
        .args([
            "--quiet",
            "mcp-server",
            "--http",
            "--host",
            "127.0.0.1",
            "--port",
            "0",
        ])
        .current_dir(dir.path())
        .stderr(std::process::Stdio::piped())
        .stdout(std::process::Stdio::null())
        .stdin(std::process::Stdio::null())
        .spawn()
        .expect("failed to spawn quiet mcp-server --http");

    let stderr = child.stderr.take().unwrap();
    let collector = tokio::spawn(async move {
        let mut reader = tokio::io::BufReader::new(stderr);
        let mut all = String::new();
        let mut line = String::new();
        loop {
            line.clear();
            match tokio::io::AsyncBufReadExt::read_line(&mut reader, &mut line).await {
                Ok(0) => break,
                Ok(_) => all.push_str(&line),
                Err(_) => break,
            }
        }
        all
    });
    tokio::time::sleep(std::time::Duration::from_millis(1200)).await;
    assert!(
        child.try_wait().expect("try_wait").is_none(),
        "quiet --http server should still be running"
    );
    child.kill().await.ok();
    let stderr = collector.await.expect("stderr collector");
    assert!(
        !stderr.contains("listening"),
        "--quiet must not print the listening banner: {stderr}"
    );
}

/// Occupied bind is a typed `invalid_input` under `--json`, not a bare anyhow.
#[cfg(feature = "mcp-http")]
#[test]
fn test_mcp_http_bind_failure_is_invalid_input_json() {
    if !has_mcp_http_support() {
        return;
    }
    let holder = std::net::TcpListener::bind("127.0.0.1:0").expect("hold a port");
    let port = holder.local_addr().expect("local_addr").port();
    let output = Command::cargo_bin("patchloom")
        .unwrap()
        .args([
            "--json",
            "mcp-server",
            "--http",
            "--host",
            "127.0.0.1",
            "--port",
            &port.to_string(),
        ])
        .output()
        .unwrap();
    assert_eq!(output.status.code(), Some(1), "{output:?}");
    let stdout = String::from_utf8_lossy(&output.stdout);
    let v: serde_json::Value = serde_json::from_str(stdout.trim())
        .unwrap_or_else(|e| panic!("expected JSON invalid_input on stdout: {e}; stdout={stdout}"));
    assert_eq!(v["ok"], false, "{v}");
    assert_eq!(v["error_kind"], "invalid_input", "{v}");
    let msg = v["error"].as_str().unwrap_or_default();
    assert!(
        msg.contains("failed to bind"),
        "error must name the bind failure: {msg}"
    );
    drop(holder);
}

/// mcp-setup must not present `--host 0.0.0.0` as a one-line default.
#[test]
fn test_mcp_setup_does_not_advertise_bare_all_interfaces_http() {
    let doc = fs::read_to_string(repo_root().join("docs/getting-started/mcp-setup.md")).unwrap();
    assert!(
        doc.contains("--allow-unauthenticated"),
        "mcp-setup must document --allow-unauthenticated"
    );
    assert!(
        doc.contains("127.0.0.1"),
        "mcp-setup must show loopback first"
    );
    for line in doc.lines() {
        let trimmed = line.trim();
        // Command examples only. Prose may mention 0.0.0.0 to warn against it.
        if trimmed.starts_with("patchloom ") && trimmed.contains("--host 0.0.0.0") {
            assert!(
                trimmed.contains("--allow-unauthenticated") || trimmed.ends_with('\\'),
                "0.0.0.0 command must include --allow-unauthenticated (or continue to a line that does): {trimmed}"
            );
        }
    }
    let idx = doc
        .find("patchloom mcp-server --http --host 0.0.0.0")
        .expect("all-interfaces example should still exist with the flag");
    let window = &doc[idx..idx.saturating_add(200).min(doc.len())];
    assert!(
        window.contains("--allow-unauthenticated"),
        "0.0.0.0 command example must include --allow-unauthenticated nearby"
    );
}

#[cfg(feature = "mcp-http")]
#[test]
fn test_mcp_http_tls_cert_requires_tls_key() {
    if !has_mcp_http_support() {
        return;
    }
    Command::cargo_bin("patchloom")
        .unwrap()
        .args(["mcp-server", "--http", "--tls-cert", "cert.pem"])
        .assert()
        .failure()
        .stderr(predicates::str::contains("--tls-key"));
}

#[cfg(feature = "mcp-http")]
#[test]
fn test_mcp_http_tls_key_requires_tls_cert() {
    if !has_mcp_http_support() {
        return;
    }
    Command::cargo_bin("patchloom")
        .unwrap()
        .args(["mcp-server", "--http", "--tls-key", "key.pem"])
        .assert()
        .failure()
        .stderr(predicates::str::contains("--tls-cert"));
}

/// Verify that invalid TLS cert content produces a clear error.
#[cfg(feature = "mcp-http")]
#[test]
fn test_mcp_http_invalid_tls_cert_fails_with_error() {
    if !has_mcp_http_support() {
        return;
    }
    let dir = TempDir::new().unwrap();
    fs::write(dir.path().join("bad-cert.pem"), "not a certificate\n").unwrap();
    fs::write(dir.path().join("bad-key.pem"), "not a key\n").unwrap();
    Command::cargo_bin("patchloom")
        .unwrap()
        .args([
            "mcp-server",
            "--http",
            "--tls-cert",
            dir.path().join("bad-cert.pem").to_str().unwrap(),
            "--tls-key",
            dir.path().join("bad-key.pem").to_str().unwrap(),
        ])
        .assert()
        .failure()
        .stderr(predicates::str::contains("TLS"));
}

/// Verify that search_files works over HTTPS (TLS) transport with --port 0.
/// This exercises the TLS server setup (axum_server::bind_rustls) and the
/// ephemeral port banner fix (#867).
#[cfg(feature = "mcp-http")]
#[tokio::test]
async fn test_mcp_https_search_files_round_trip() {
    if !has_mcp_http_support() {
        return;
    }

    let dir = TempDir::new().unwrap();
    fs::write(dir.path().join("hello.txt"), "hello tls world\n").unwrap();

    // Generate a self-signed certificate with rcgen.
    let ca = rcgen::generate_simple_self_signed(vec!["127.0.0.1".to_string()])
        .expect("self-signed cert");

    let cert_pem = ca.cert.pem();
    let key_pem = ca.signing_key.serialize_pem();

    let cert_path = dir.path().join("cert.pem");
    let key_path = dir.path().join("key.pem");
    fs::write(&cert_path, &cert_pem).unwrap();
    fs::write(&key_path, &key_pem).unwrap();

    let bin = assert_cmd::cargo::cargo_bin("patchloom");
    let mut child = tokio::process::Command::new(&bin)
        .args([
            "mcp-server",
            "--http",
            "--port",
            "0",
            "--tls-cert",
            cert_path.to_str().unwrap(),
            "--tls-key",
            key_path.to_str().unwrap(),
        ])
        .current_dir(dir.path())
        .stderr(std::process::Stdio::piped())
        .stdout(std::process::Stdio::null())
        .stdin(std::process::Stdio::null())
        .spawn()
        .expect("failed to spawn mcp-server --http --tls-*");

    // Read stderr to discover the actual bound port.
    let stderr = child.stderr.take().unwrap();
    let mut reader = tokio::io::BufReader::new(stderr);
    let mut line = String::new();
    tokio::io::AsyncBufReadExt::read_line(&mut reader, &mut line)
        .await
        .expect("failed to read HTTPS server banner");

    // Parse "MCP HTTPS server listening on https://127.0.0.1:PORT/mcp"
    let url = line.trim().rsplit("on ").next().expect("no URL in banner");
    assert!(
        url.starts_with("https://"),
        "expected https:// URL in banner: {line}"
    );
    // Verify the banner shows a real port, not 0.
    assert!(
        !url.contains(":0/"),
        "banner should show real ephemeral port, not :0: {url}"
    );

    // Build a reqwest client that trusts the self-signed CA.
    let ca_cert = reqwest::tls::Certificate::from_pem(cert_pem.as_bytes()).expect("parse CA cert");
    let http_client = reqwest::Client::builder()
        .pool_max_idle_per_host(0)
        .add_root_certificate(ca_cert)
        .build()
        .expect("build reqwest client");

    // Connect an MCP client over Streamable HTTPS.
    // Retry the TLS connection: axum_server prints the banner when the TCP
    // listener binds, but the TLS acceptor may not be fully initialized yet.
    // On macOS this race is hit consistently; on Linux it rarely occurs.
    use rmcp::ServiceExt;
    let mut client_opt = None;
    for attempt in 0..10 {
        let config =
            rmcp::transport::streamable_http_client::StreamableHttpClientTransportConfig::with_uri(
                url,
            );
        let transport = rmcp::transport::StreamableHttpClientTransport::with_client(
            http_client.clone(),
            config,
        );
        match ().serve(transport).await {
            Ok(c) => {
                client_opt = Some(c);
                break;
            }
            Err(_) if attempt < 9 => {
                tokio::time::sleep(std::time::Duration::from_millis(200)).await;
            }
            Err(e) => panic!("HTTPS client connect failed after 10 attempts: {e}"),
        }
    }
    let client: rmcp::service::RunningService<rmcp::RoleClient, ()> = client_opt.unwrap();

    // List tools.
    let tools = client.peer().list_all_tools().await.unwrap();
    let names: Vec<&str> = tools.iter().map(|t| t.name.as_ref()).collect();
    assert!(
        names.contains(&"search_files"),
        "search_files tool should be listed over HTTPS"
    );

    // Call search_files.
    let params = rmcp::model::CallToolRequestParams::new("search_files".to_string())
        .with_arguments(
            serde_json::from_value(serde_json::json!({"pattern": "tls", "paths": ["."]})).unwrap(),
        );
    let result = client.peer().call_tool(params).await.unwrap();
    assert!(
        !result.is_error.unwrap_or(false),
        "search_files should succeed over HTTPS"
    );
    let text = result
        .content
        .first()
        .and_then(|c| match c {
            rmcp::model::ContentBlock::Text(t) => Some(t.text.clone()),
            _ => None,
        })
        .unwrap_or_default();
    assert!(
        text.contains("hello tls world"),
        "search result should contain match: {text}"
    );

    client.cancel().await.unwrap();
    child.kill().await.ok();
}