patchloom 0.23.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
708
709
710
711
712
713
714
715
716
717
718
use super::*;
use crate::cli::global::GlobalFlags;
use std::collections::{HashMap, HashSet};
use std::path::PathBuf;
use tempfile::TempDir;

// ---- read_file_content ----

#[test]
fn read_file_content_from_disk() {
    let dir = TempDir::new().unwrap();
    let file = dir.path().join("test.txt");
    std::fs::write(&file, "hello world").unwrap();

    let mut pending = HashMap::new();
    let mut existed = HashSet::new();

    let content = read_file_content(&mut pending, &mut existed, &file).unwrap();
    assert_eq!(content, "hello world");
    assert!(existed.contains(&file));
    assert!(pending.contains_key(&file));
    // Original and current should both be "hello world".
    let (orig, cur) = &pending[&file];
    assert_eq!(orig, "hello world");
    assert_eq!(cur, "hello world");
}

#[test]
fn read_file_content_from_pending() {
    let path = PathBuf::from("/fake/already_loaded.txt");
    let mut pending = HashMap::new();
    pending.insert(
        path.clone(),
        ("original".to_string(), "modified".to_string()),
    );
    let mut existed = HashSet::new();

    let content = read_file_content(&mut pending, &mut existed, &path).unwrap();
    assert_eq!(content, "modified");
    // Should not add to existed_before since it was already in pending.
    assert!(!existed.contains(&path));
}

#[test]
fn read_file_content_missing_file_errors() {
    let mut pending = HashMap::new();
    let mut existed = HashSet::new();
    let path = PathBuf::from("/nonexistent/file.txt");

    let result = read_file_content(&mut pending, &mut existed, &path);
    result.expect_err("expected error");
}

// ---- read_and_probe ----

#[test]
fn read_and_probe_text_file() {
    let dir = TempDir::new().unwrap();
    let file = dir.path().join("text.txt");
    std::fs::write(&file, "text content").unwrap();

    let mut pending = HashMap::new();
    let mut existed = HashSet::new();

    assert!(read_and_probe(&mut pending, &mut existed, &file).unwrap());
    assert!(pending.contains_key(&file));
}

#[test]
fn read_and_probe_binary_file() {
    let dir = TempDir::new().unwrap();
    let file = dir.path().join("binary.bin");
    // Write bytes with NUL to trigger binary detection.
    std::fs::write(&file, b"\x00\x01\x02\x03").unwrap();

    let mut pending = HashMap::new();
    let mut existed = HashSet::new();

    assert!(!read_and_probe(&mut pending, &mut existed, &file).unwrap());
    assert!(!pending.contains_key(&file));
}

#[test]
fn read_and_probe_invalid_utf8_soft_skips() {
    let dir = TempDir::new().unwrap();
    let file = dir.path().join("bad.txt");
    std::fs::write(&file, b"hello \xff world").unwrap();
    let mut pending = HashMap::new();
    let mut existed = HashSet::new();
    assert!(!read_and_probe(&mut pending, &mut existed, &file).unwrap());
    assert!(pending.is_empty());
}

#[test]
fn read_and_probe_missing_is_hard_err() {
    let dir = TempDir::new().unwrap();
    let file = dir.path().join("missing.txt");
    let mut pending = HashMap::new();
    let mut existed = HashSet::new();
    let err = read_and_probe(&mut pending, &mut existed, &file).unwrap_err();
    assert!(
        err.to_string().contains("failed to read") || err.to_string().contains("missing"),
        "{err:#}"
    );
    // NotFound must remain classifiable through the chain (not a bare string).
    assert!(
        crate::exit::is_io_not_found(&err),
        "expected is_io_not_found, got: {err:#}"
    );
}

/// Strict sole-path load must refuse binary (not rewrite as text) (#1894).
#[test]
fn read_file_content_rejects_binary() {
    let dir = TempDir::new().unwrap();
    let file = dir.path().join("binary.bin");
    std::fs::write(&file, b"hello\x00world").unwrap();

    let mut pending = HashMap::new();
    let mut existed = HashSet::new();

    let err = read_file_content(&mut pending, &mut existed, &file).unwrap_err();
    assert!(crate::exit::is_binary(&err), "{err:#}");
    assert!(err.to_string().contains("binary"), "{err}");
    assert!(pending.is_empty());
}

#[test]
fn read_file_content_rejects_invalid_utf8() {
    let dir = TempDir::new().unwrap();
    let file = dir.path().join("bad.txt");
    std::fs::write(&file, b"hello \xff world").unwrap();

    let mut pending = HashMap::new();
    let mut existed = HashSet::new();

    let err = read_file_content(&mut pending, &mut existed, &file).unwrap_err();
    assert!(crate::exit::is_invalid_encoding(&err), "{err:#}");
    assert!(err.to_string().contains("UTF-8"), "{err}");
    assert!(pending.is_empty());
}

#[test]
fn read_and_probe_already_loaded_skips() {
    let path = PathBuf::from("/fake/loaded.txt");
    let mut pending = HashMap::new();
    pending.insert(path.clone(), ("orig".to_string(), "cur".to_string()));
    let mut existed = HashSet::new();

    assert!(read_and_probe(&mut pending, &mut existed, &path).unwrap());
}

// ---- update_file_content ----

#[test]
fn update_file_content_existing_entry() {
    let path = PathBuf::from("/fake/file.txt");
    let mut pending = HashMap::new();
    let mut deletions = HashSet::new();
    let mut write_targets = HashSet::new();
    pending.insert(path.clone(), ("original".to_string(), "old".to_string()));

    update_file_content(
        &mut pending,
        &mut deletions,
        &mut write_targets,
        &path,
        "new content".into(),
    );

    let (orig, cur) = &pending[&path];
    assert_eq!(orig, "original"); // original preserved
    assert_eq!(cur, "new content");
    assert!(write_targets.contains(&path));
}

#[test]
fn update_file_content_new_entry() {
    let path = PathBuf::from("/fake/new_file.txt");
    let mut pending = HashMap::new();
    let mut deletions = HashSet::new();
    let mut write_targets = HashSet::new();

    update_file_content(
        &mut pending,
        &mut deletions,
        &mut write_targets,
        &path,
        "content".into(),
    );

    let (orig, cur) = &pending[&path];
    assert!(orig.is_empty()); // no original for new files
    assert_eq!(cur, "content");
    assert!(write_targets.contains(&path));
}

#[test]
fn update_file_content_clears_deletion() {
    let path = PathBuf::from("/fake/file.txt");
    let mut pending = HashMap::new();
    let mut deletions = HashSet::new();
    let mut write_targets = HashSet::new();
    deletions.insert(path.clone());

    update_file_content(
        &mut pending,
        &mut deletions,
        &mut write_targets,
        &path,
        "revived".into(),
    );

    assert!(!deletions.contains(&path));
    assert!(write_targets.contains(&path));
}

// ---- path_err ----

#[test]
fn path_err_wraps_message() {
    let wrapper = path_err("config.yaml");
    let err = wrapper(anyhow::anyhow!("invalid key"));
    assert_eq!(err.to_string(), "config.yaml: invalid key");
}

#[test]
fn path_err_preserves_io_not_found() {
    let io_err: anyhow::Error = std::io::Error::new(std::io::ErrorKind::NotFound, "nope").into();
    let err = path_err("missing.json")(io_err.context("failed to read"));
    assert!(
        crate::exit::is_io_not_found(&err),
        "path_err must keep NotFound: {err:#}"
    );
}

// ---- op_needs_doc_flush ----

#[test]
fn op_needs_doc_flush_for_replace() {
    let op = Operation::Replace {
        path: Some("f.txt".into()),
        glob: None,
        regex: false,
        old: "a".into(),
        new_text: Some("b".into()),
        nth: None,
        insert_before: None,
        insert_after: None,
        case_insensitive: false,
        multiline: false,
        whole_line: false,
        word_boundary: false,
        range: None,
        before_context: None,
        after_context: None,
        if_exists: false,
        unique: false,
        require_change: false,
        command_position: false,
        fuzzy: false,
        min_fuzzy_score: None,
        allow_absent_old: false,
    };
    assert!(op_needs_doc_flush(&op));
}

#[test]
fn op_needs_doc_flush_false_for_doc_set() {
    let op = Operation::DocSet {
        path: "f.json".into(),
        selector: "key".into(),
        value: serde_json::json!("val"),
    };
    assert!(!op_needs_doc_flush(&op));
}

#[test]
fn op_needs_doc_flush_for_read() {
    let op = Operation::Read {
        path: "f.txt".into(),
        lines: None,
    };
    assert!(op_needs_doc_flush(&op));
}

#[test]
fn op_needs_doc_flush_for_search() {
    let op = Operation::Search {
        path: "src".into(),
        pattern: "TODO".into(),
        regex: false,
        case_insensitive: false,
        multiline: false,
        invert_match: false,
        context: None,
        before_context: None,
        after_context: None,
        assert_count: None,
        literal: false,
        globs: Vec::new(),
        max_results: 0,
        exclude_patterns: Vec::new(),
        custom_ignore_filenames: Vec::new(),
    };
    assert!(op_needs_doc_flush(&op));
}

/// Regression: appending to a file deleted earlier in the same tx must
/// fail, not silently resurrect the file.
#[test]
fn file_append_to_deleted_file_errors() {
    let dir = TempDir::new().unwrap();
    let file = dir.path().join("victim.txt");
    std::fs::write(&file, "original").unwrap();

    let mut f = TxStateFixture::new();
    // Simulate: file was loaded and then deleted in this tx.
    let _ = read_file_content(&mut f.pending, &mut f.existed_before, &file).unwrap();
    f.deletions.insert(file.clone());

    let mut tx = f.state(dir.path());

    let op = Operation::FileAppend {
        path: "victim.txt".into(),
        content: "new stuff".into(),
    };
    let result = execute_file_op(&op, &mut tx);
    assert!(
        result.is_err(),
        "append to deleted file should error, not resurrect"
    );
    let msg = result.unwrap_err().to_string();
    assert!(
        msg.contains("deleted earlier"),
        "error message should mention deletion: {msg}"
    );
}

/// Same regression for prepend.
#[test]
fn file_prepend_to_deleted_file_errors() {
    let dir = TempDir::new().unwrap();
    let file = dir.path().join("victim.txt");
    std::fs::write(&file, "original").unwrap();

    let mut f = TxStateFixture::new();
    let _ = read_file_content(&mut f.pending, &mut f.existed_before, &file).unwrap();
    f.deletions.insert(file.clone());

    let mut tx = f.state(dir.path());

    let op = Operation::FilePrepend {
        path: "victim.txt".into(),
        content: "prefix".into(),
    };
    let result = execute_file_op(&op, &mut tx);
    assert!(
        result.is_err(),
        "prepend to deleted file should error, not resurrect"
    );
}

/// append/prepend must not rewrite binary (NUL) files as text.
#[test]
fn file_append_rejects_binary_file() {
    let dir = TempDir::new().unwrap();
    let file = dir.path().join("data.bin");
    std::fs::write(&file, b"hello\x00world").unwrap();

    let mut f = TxStateFixture::new();
    let mut tx = f.state(dir.path());

    let op = Operation::FileAppend {
        path: "data.bin".into(),
        content: "evil\n".into(),
    };
    let err = execute_file_op(&op, &mut tx).unwrap_err();
    assert!(
        crate::exit::is_binary(&err),
        "expected BinaryError, got: {err:#}"
    );
    assert!(
        err.to_string().contains("binary file"),
        "message should name binary: {err}"
    );
    assert!(
        f.pending.is_empty(),
        "must not stage a write for a binary append"
    );
    assert_eq!(std::fs::read(&file).unwrap(), b"hello\x00world");
}

#[test]
fn file_prepend_rejects_binary_file() {
    let dir = TempDir::new().unwrap();
    let file = dir.path().join("data.bin");
    std::fs::write(&file, b"hello\x00world").unwrap();

    let mut f = TxStateFixture::new();
    let mut tx = f.state(dir.path());

    let op = Operation::FilePrepend {
        path: "data.bin".into(),
        content: "evil\n".into(),
    };
    let err = execute_file_op(&op, &mut tx).unwrap_err();
    assert!(crate::exit::is_binary(&err), "got: {err:#}");
    assert_eq!(std::fs::read(&file).unwrap(), b"hello\x00world");
}

/// Sole-path replace must refuse binary (NUL) files; MCP/tx used to rewrite them.
#[test]
fn replace_rejects_sole_binary_file() {
    let dir = TempDir::new().unwrap();
    let file = dir.path().join("data.bin");
    std::fs::write(&file, b"hello\x00world").unwrap();

    let mut f = TxStateFixture::new();
    let mut tx = f.state(dir.path());

    let op = Operation::Replace {
        glob: None,
        path: Some("data.bin".into()),
        regex: false,
        old: "hello".into(),
        new_text: Some("HELLO".into()),
        nth: None,
        insert_before: None,
        insert_after: None,
        case_insensitive: false,
        multiline: false,
        if_exists: false,
        whole_line: false,
        range: None,
        word_boundary: false,
        before_context: None,
        after_context: None,
        unique: false,
        require_change: false,
        command_position: false,
        fuzzy: false,
        min_fuzzy_score: None,
        allow_absent_old: false,
    };
    let err = crate::tx::replace_op::execute_replace_op(&op, &mut tx).unwrap_err();
    assert!(
        crate::exit::is_binary(&err),
        "expected BinaryError, got: {err:#}"
    );
    assert!(
        err.to_string().contains("binary file"),
        "message should name binary: {err}"
    );
    assert!(
        f.pending.is_empty() && f.write_targets.is_empty(),
        "must not stage a write for binary replace"
    );
    assert_eq!(std::fs::read(&file).unwrap(), b"hello\x00world");
}

/// file.create through a path component that is a file must fail with
/// InvalidInputError before staging (no bare tempfile / false backup).
#[test]
fn file_create_rejects_parent_that_is_a_file() {
    let dir = TempDir::new().unwrap();
    let blocking = dir.path().join("notdir");
    std::fs::write(&blocking, "file\n").unwrap();

    let mut f = TxStateFixture::new();
    let mut tx = f.state(dir.path());

    let op = Operation::FileCreate {
        path: "notdir/child.txt".into(),
        content: "x\n".into(),
        force: None,
    };
    let err = execute_file_op(&op, &mut tx).unwrap_err();
    assert!(
        crate::exit::is_invalid_input(&err),
        "expected InvalidInputError, got: {err:#}"
    );
    assert!(
        err.to_string().contains("not a directory"),
        "message should name the problem: {err}"
    );
    assert!(
        f.pending.is_empty(),
        "must not stage a write when parent is not a directory"
    );
}

#[test]
fn md_move_section_same_file_by_path_equality() {
    // Regression: MdMoveSection with to=Some(same_path) must detect
    // same-file via path equality, not just canonicalize (which fails
    // for files created in-tx that don't exist on disk).
    let dir = TempDir::new().unwrap();
    let file = dir.path().join("doc.md");
    std::fs::write(&file, "# A\ntext a\n# B\ntext b\n").unwrap();

    let mut f = TxStateFixture::new();
    let mut tx = f.state(dir.path());

    let op = Operation::MdMoveSection {
        path: "doc.md".into(),
        heading: "# A".into(),
        to: Some("doc.md".into()),
        before: None,
        after: Some("# B".into()),
    };
    execute_operation(&op, &mut tx).unwrap();
    drop(tx);
    // Section A should appear after B, not be duplicated
    let content = &f.pending[&file].1;
    let a_pos = content.find("# A").unwrap();
    let b_pos = content.find("# B").unwrap();
    assert!(a_pos > b_pos, "section A should be after B: {content}");
    // Section A should appear exactly once
    assert_eq!(
        content.matches("# A").count(),
        1,
        "section A should not be duplicated: {content}"
    );
}

#[test]
fn rename_deleted_source_is_rejected() {
    // Regression: FileRename of a source file deleted earlier in the
    // same transaction should error, not silently create an empty file.
    let dir = TempDir::new().unwrap();
    let file = dir.path().join("victim.txt");
    std::fs::write(&file, "content").unwrap();

    let mut f = TxStateFixture::new();
    // Simulate deletion
    f.pending
        .insert(file.clone(), ("content".to_string(), String::new()));
    f.deletions.insert(file);
    f.existed_before.insert(dir.path().join("victim.txt"));

    let mut tx = f.state(dir.path());

    let op = Operation::FileRename {
        from: "victim.txt".into(),
        to: "dest.txt".into(),
        force: false,
    };
    let result = execute_file_op(&op, &mut tx);
    assert!(result.is_err(), "rename of deleted file should error");
    let msg = result.unwrap_err().to_string();
    assert!(
        msg.contains("deleted earlier"),
        "error should mention deletion: {msg}"
    );
}

/// Case-only rename (readme.md → README.md) must stage `tx.renames` so commit
/// uses `fs::rename`. Without that, write-dest + delete-src removes the only
/// inode on case-insensitive filesystems (macOS APFS); agents hit this via
/// plan/MCP while CLI bypasses the engine (#1167).
#[test]
fn case_only_rename_records_tx_renames() {
    let dir = TempDir::new().unwrap();
    let file = dir.path().join("readme.md");
    std::fs::write(&file, "hello content\n").unwrap();

    let mut f = TxStateFixture::new();
    let mut tx = f.state(dir.path());
    let op = Operation::FileRename {
        from: "readme.md".into(),
        to: "README.md".into(),
        force: false,
    };
    execute_file_op(&op, &mut tx).expect("case-only rename should stage");
    drop(tx);
    assert!(
        !f.renames.is_empty(),
        "case-only rename must record tx.renames for fs::rename; got empty"
    );
    assert_eq!(
        f.renames[0].0.file_name().and_then(|n| n.to_str()),
        Some("readme.md")
    );
    assert_eq!(
        f.renames[0].1.file_name().and_then(|n| n.to_str()),
        Some("README.md")
    );
}

/// End-to-end: plan/tx case-only rename must leave content on disk (not delete).
#[test]
fn case_only_rename_plan_apply_preserves_content() {
    let dir = TempDir::new().unwrap();
    let src = dir.path().join("readme.md");
    std::fs::write(&src, "hello content\n").unwrap();

    let plan = crate::plan::Plan {
        version: crate::plan::SCHEMA_VERSION,
        cwd: None,
        operations: vec![Operation::FileRename {
            from: "readme.md".into(),
            to: "README.md".into(),
            force: false,
        }],
        write_policy: None,
        strict: None,
        format: None,
        validate: None,
        verify: None,
        for_each: None,
    };
    let report = crate::tx::execute_plan_direct(plan, dir.path(), None).expect("plan ok");
    assert!(
        report.ok,
        "case-only rename plan should succeed: {report:?}"
    );

    // Content must still exist under either spelling (case-insensitive FS).
    let content = std::fs::read_to_string(dir.path().join("README.md"))
        .or_else(|_| std::fs::read_to_string(&src))
        .expect("file must still exist after case-only rename");
    assert_eq!(content, "hello content\n");
}

/// Regression: files loaded for Read/Search operations should not be
/// modified by write policy (e.g. ensure_final_newline) (#1108).
#[test]
fn read_only_files_skip_write_policy() {
    let dir = TempDir::new().unwrap();
    // Create a file WITHOUT a trailing newline.
    let file = dir.path().join("readonly.txt");
    std::fs::write(&file, "no trailing newline").unwrap();

    // Build a plan that only reads the file, with ensure_final_newline active.
    let plan = Plan {
        version: crate::plan::SCHEMA_VERSION,
        operations: vec![Operation::Read {
            path: "readonly.txt".into(),
            lines: None,
        }],
        write_policy: Some(crate::write::WritePolicyOverride {
            ensure_final_newline: Some(true),
            ..Default::default()
        }),
        strict: None,
        format: None,
        validate: None,
        verify: None,
        cwd: None,
        for_each: None,
    };

    let global = GlobalFlags {
        ensure_final_newline: true,
        ..GlobalFlags::default()
    };
    let ctx = crate::tx::context::EngineContext::from_global(&global, dir.path().to_path_buf());
    let result = execute_and_collect(&plan, &ctx, true, false, None).unwrap();

    // The file should NOT appear in changes since it was only read.
    assert!(
        result.changes.is_empty(),
        "read-only file should not be modified by write policy, got {} changes",
        result.changes.len()
    );
    // Verify the file on disk is unchanged.
    let on_disk = std::fs::read_to_string(&file).unwrap();
    assert_eq!(
        on_disk, "no trailing newline",
        "file on disk should be unchanged"
    );
}

#[test]
fn execute_and_collect_preserves_no_match_error_kind() {
    // Missing doc.update target must remain NoMatchError (exit 3 path), not
    // a plain anyhow operation_failed, even after op-label wrapping.
    let dir = TempDir::new().unwrap();
    let file = dir.path().join("data.json");
    std::fs::write(&file, r#"{"a":1}"#).unwrap();

    let plan = Plan {
        version: crate::plan::SCHEMA_VERSION,
        operations: vec![Operation::DocUpdate {
            path: "data.json".into(),
            selector: "missing.key".into(),
            value: serde_json::json!(2),
        }],
        write_policy: None,
        strict: None,
        format: None,
        validate: None,
        verify: None,
        cwd: None,
        for_each: None,
    };
    let ctx = crate::tx::context::EngineContext::from_global(
        &GlobalFlags::default(),
        dir.path().to_path_buf(),
    );
    match execute_and_collect(&plan, &ctx, true, false, None) {
        Ok(_) => panic!("expected NoMatch for missing selector"),
        Err(err) => {
            assert!(
                crate::exit::is_no_match(&err),
                "NoMatch must survive execute wrap: {err:#}"
            );
            let msg = err.to_string();
            assert!(
                msg.contains("doc.update")
                    || msg.contains("matched nothing")
                    || msg.contains("missing"),
                "detail should remain: {msg}"
            );
        }
    }
}