patchloom 0.15.2

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
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
use crate::cli::global::GlobalFlags;
use crate::exit;
use crate::plan::{Operation, Plan};
use clap::Args;

/// Maximum number of operations in a single batch. Prevents unbounded
/// memory allocation from accidentally or maliciously large inputs.
pub const MAX_BATCH_OPERATIONS: usize = 10_000;

/// Execute multiple operations from a simple line-oriented format.
///
/// Each line is one operation with positional arguments:
///
/// ```text
/// doc.set <path> <selector> <value>
/// doc.delete <path> <selector>
/// doc.merge <path> <json-value>
/// doc.ensure <path> <selector> <value>
/// doc.append <path> <selector> <value>
/// doc.prepend <path> <selector> <value>
/// doc.update <path> <selector> <value>
/// doc.move <path> <from> <to>
/// doc.delete_where <path> <selector> <predicate>
/// replace <path> <from> <to> [--fuzzy] [--min-fuzzy-score N] [-i] …
///   Optional flags (phase 1, #1724): --fuzzy, --min-fuzzy-score, --word-boundary/-w,
///   --command-position, --require-change, -i/--case-insensitive, --if-exists
/// file.create <path> <content>
/// file.delete <path>
/// file.rename <from> <to>
/// file.append <path> <content>
/// file.prepend <path> <content>
/// md.upsert_bullet <path> <heading> <bullet>
/// md.table_append <path> <heading> <row>
/// md.replace_section <path> <heading> <content>
/// md.insert_after_heading <path> <heading> <content>
/// md.insert_after_section <path> <heading> <content>
/// md.insert_before_heading <path> <heading> <content>
/// md.move_section <path> <heading> before|after <target_heading>
/// md.move_section <path> <heading> <to> before|after <target_heading>
/// md.dedupe_headings <path>
/// md.lint_agents <path>
/// tidy.fix <path>
/// ast.rename <path> <old> <new>
/// ast.replace <path> <symbol> <old> <new>
/// ast.rewrite_signature <path> <old> <parameters> [return_type]
/// ```
///
/// Lines starting with `#` are comments. Empty lines are ignored.
/// Values containing spaces must be quoted with double quotes.
#[derive(Debug, Args)]
#[command(after_help = r#"OPERATIONS:
  doc.set, doc.delete, doc.merge, doc.ensure, doc.append, doc.prepend,
  doc.update, doc.move, doc.delete_where, replace (optional flags: --fuzzy, …), file.create,
  file.delete, file.rename, file.append, file.prepend, md.upsert_bullet,
  md.table_append, md.replace_section, md.insert_after_heading,
  md.insert_after_section, md.insert_before_heading, md.move_section, md.dedupe_headings,
  md.lint_agents, tidy.fix, ast.rename, ast.replace, ast.rewrite_signature

EXAMPLES:
  printf 'doc.set config.json version "2.0"\nreplace README.md v1 v2\n' | patchloom batch
  patchloom batch --apply <<'EOF'
  doc.set package.json version "3.0.0"
  replace README.md "v1.0" "v3.0"
  replace src/main.rs "proccess" "process" --fuzzy --min-fuzzy-score 0.80
  EOF"#)]
pub struct BatchArgs {
    /// Read operations from a file, or stdin if omitted.
    #[arg(default_value = "-")]
    pub input: String,

    #[command(flatten)]
    pub write: crate::cli::global::WriteFlags,
}

/// Parse a single line into an Operation.
/// Small helper macro used inside parse_line arms to cut the repetitive
/// "Ok(Operation::Variant { ... })" boilerplate.
macro_rules! op {
    ($Variant:ident { $($tt:tt)* }) => {
        Ok(Operation::$Variant { $($tt)* })
    };
}

/// Helper macros for batch operation parsing.
///
/// `doc_psv!` handles the common doc pattern: 3 args = path + selector + json_value.
/// `doc_ps!` handles 2-arg doc ops: path + selector.
/// `md_phc!` handles 3-arg markdown ops: path + heading + content (third field varies).
macro_rules! doc_psv {
    ($op_name:expr, $args:expr, $line_num:expr, $Variant:ident) => {{
        require_args($op_name, $args, 3, $line_num)?;
        let value = parse_json_value(&$args[2])?;
        op!($Variant {
            path: $args[0].clone(),
            selector: $args[1].clone(),
            value
        })
    }};
}

macro_rules! md_phc {
    ($op_name:expr, $args:expr, $line_num:expr, $Variant:ident, $field3:ident) => {{
        require_args($op_name, $args, 3, $line_num)?;
        op!($Variant {
            path: $args[0].clone(),
            heading: $args[1].clone(),
            $field3: $args[2].clone()
        })
    }};
}

fn parse_line(line: &str, line_num: usize) -> anyhow::Result<Operation> {
    let tokens = tokenize(line).map_err(|e| {
        anyhow::Error::new(crate::exit::ParseErrorError {
            msg: format!("line {line_num}: {e}"),
        })
    })?;
    if tokens.is_empty() {
        return Err(anyhow::Error::new(crate::exit::ParseErrorError {
            msg: format!("line {line_num}: empty operation"),
        }));
    }
    let op = tokens[0].as_str();
    let args = &tokens[1..];

    match op {
        // -- doc operations (path + selector + value) --------------------------
        "doc.set" => doc_psv!(op, args, line_num, DocSet),
        "doc.ensure" => doc_psv!(op, args, line_num, DocEnsure),
        "doc.append" => doc_psv!(op, args, line_num, DocAppend),
        "doc.prepend" => doc_psv!(op, args, line_num, DocPrepend),
        "doc.update" => doc_psv!(op, args, line_num, DocUpdate),

        "doc.delete" => {
            require_args(op, args, 2, line_num)?;
            op!(DocDelete {
                path: args[0].clone(),
                selector: args[1].clone()
            })
        }
        "doc.merge" => {
            require_args(op, args, 2, line_num)?;
            let value = parse_json_value(&args[1])?;
            op!(DocMerge {
                path: args[0].clone(),
                value
            })
        }
        "doc.move" => {
            require_args(op, args, 3, line_num)?;
            op!(DocMove {
                path: args[0].clone(),
                from: args[1].clone(),
                to: args[2].clone()
            })
        }
        "doc.delete_where" => {
            require_args(op, args, 3, line_num)?;
            op!(DocDeleteWhere {
                path: args[0].clone(),
                selector: args[1].clone(),
                predicate: args[2].clone()
            })
        }

        // -- replace -----------------------------------------------------------
        "replace" => parse_replace_line(args, line_num),

        // -- file operations ---------------------------------------------------
        // Content is path + remainder (joined). Agents write unquoted multi-word
        // text and unquoted JSON objects; exact-arity-2 rejected those.
        "file.append" => {
            let (path, content) = path_and_joined_content(op, args, line_num)?;
            op!(FileAppend { path, content })
        }
        "file.prepend" => {
            let (path, content) = path_and_joined_content(op, args, line_num)?;
            op!(FilePrepend { path, content })
        }
        "file.create" => {
            let (path, content) = path_and_joined_content(op, args, line_num)?;
            op!(FileCreate {
                path,
                content,
                force: None
            })
        }
        "file.delete" => {
            require_args(op, args, 1, line_num)?;
            op!(FileDelete {
                path: args[0].clone()
            })
        }
        "file.rename" => {
            require_args(op, args, 2, line_num)?;
            op!(FileRename {
                from: args[0].clone(),
                to: args[1].clone(),
                force: false
            })
        }

        // -- markdown operations -----------------------------------------------
        "md.upsert_bullet" => md_phc!(op, args, line_num, MdUpsertBullet, bullet),
        "md.table_append" => md_phc!(op, args, line_num, MdTableAppend, row),
        "md.replace_section" => md_phc!(op, args, line_num, MdReplaceSection, content),
        "md.insert_after_heading" => md_phc!(op, args, line_num, MdInsertAfterHeading, content),
        "md.insert_after_section" => md_phc!(op, args, line_num, MdInsertAfterSection, content),
        "md.insert_before_heading" => md_phc!(op, args, line_num, MdInsertBeforeHeading, content),

        "md.move_section" => {
            if args.len() == 4 {
                let (before, after) = parse_position_keyword(&args[2], line_num)?;
                Ok(Operation::MdMoveSection {
                    path: args[0].clone(),
                    heading: args[1].clone(),
                    to: None,
                    before: before.map(|_| args[3].clone()),
                    after: after.map(|_| args[3].clone()),
                })
            } else if args.len() == 5 {
                let (before, after) = parse_position_keyword(&args[3], line_num)?;
                Ok(Operation::MdMoveSection {
                    path: args[0].clone(),
                    heading: args[1].clone(),
                    to: Some(args[2].clone()),
                    before: before.map(|_| args[4].clone()),
                    after: after.map(|_| args[4].clone()),
                })
            } else {
                Err(anyhow::Error::new(crate::exit::ParseErrorError {
                    msg: format!(
                        "line {line_num}: md.move_section requires 4 args (same-file) or 5 args (cross-file)"
                    ),
                }))
            }
        }
        "md.dedupe_headings" => {
            require_args(op, args, 1, line_num)?;
            op!(MdDedupeHeadings {
                path: args[0].clone()
            })
        }
        "md.lint_agents" => {
            require_args(op, args, 1, line_num)?;
            op!(MdLintAgents {
                path: args[0].clone()
            })
        }

        // -- tidy --------------------------------------------------------------
        "tidy.fix" => {
            require_args(op, args, 1, line_num)?;
            op!(TidyFix {
                path: args[0].clone(),
                ensure_final_newline: None,
                trim_trailing_whitespace: None,
                normalize_eol: None,
                collapse_blanks: None,
                dedent: None,
                indent: None,
                lines: None,
            })
        }

        // -- AST operations (feature-gated) ------------------------------------
        #[cfg(feature = "ast")]
        "ast.rename" => {
            require_args(op, args, 3, line_num)?;
            op!(AstRename {
                path: args[0].clone(),
                old: args[1].clone(),
                new: args[2].clone(),
                lang: None
            })
        }
        #[cfg(feature = "ast")]
        "ast.replace" => {
            require_args(op, args, 4, line_num)?;
            op!(AstReplace {
                path: args[0].clone(),
                symbol: args[1].clone(),
                old: args[2].clone(),
                new_text: args[3].clone(),
                regex: false,
                lang: None
            })
        }
        #[cfg(feature = "ast")]
        "ast.rewrite_signature" => {
            // path old parameters [return_type]
            if args.len() < 3 || args.len() > 4 {
                return Err(anyhow::Error::new(crate::exit::ParseErrorError {
                    msg: format!(
                        "line {line_num}: 'ast.rewrite_signature' requires 3 or 4 arguments \
                         (path old parameters [return_type]), got {}",
                        args.len()
                    ),
                }));
            }
            op!(AstRewriteSignature {
                path: args[0].clone(),
                old: args[1].clone(),
                new_signature: None,
                visibility: None,
                parameters: Some(args[2].clone()),
                return_type: args.get(3).cloned(),
                lang: None
            })
        }

        _ => Err(anyhow::Error::new(crate::exit::ParseErrorError {
            msg: unknown_batch_op_msg(line_num, op),
        })),
    }
}

/// Batch op names accepted by [`parse_line`] (for "did you mean?" suggestions).
const KNOWN_BATCH_OPS: &[&str] = &[
    "doc.set",
    "doc.delete",
    "doc.merge",
    "doc.ensure",
    "doc.append",
    "doc.prepend",
    "doc.update",
    "doc.move",
    "doc.delete_where",
    "replace",
    "file.create",
    "file.delete",
    "file.rename",
    "file.append",
    "file.prepend",
    "md.upsert_bullet",
    "md.table_append",
    "md.replace_section",
    "md.insert_after_heading",
    "md.insert_after_section",
    "md.insert_before_heading",
    "md.move_section",
    "md.dedupe_headings",
    "md.lint_agents",
    "tidy.fix",
    "ast.rename",
    "ast.replace",
    "ast.rewrite_signature",
];

/// Ops that exist as standalone CLI / tx but not in batch line format.
fn batch_unsupported_hint(op: &str) -> Option<&'static str> {
    match op {
        "read" => Some("not supported in batch; use `patchloom read` or a tx plan"),
        "search" => Some("not supported in batch; use `patchloom search` or a tx plan"),
        "patch" | "patch.apply" => {
            Some("not supported in batch; use `patchloom patch` or a tx plan")
        }
        "status" | "git_status" => Some("not supported in batch; use `patchloom status`"),
        "undo" => Some("not supported in batch; use `patchloom undo`"),
        "explain" => Some("not supported in batch; use `patchloom explain`"),
        _ => None,
    }
}

/// Suggest close batch op names for typos and bare names (`create` → `file.create`).
fn suggest_batch_op(op: &str) -> Option<String> {
    // Wrong namespace for a bare op: agents invent `file.replace` / `doc.replace`
    // from `file.create` / `doc.set`. Prefer the real bare name over JW neighbors
    // like `file.rename` (fixrealloop 2026-07-15).
    if let Some((_, leaf)) = op.split_once('.')
        && KNOWN_BATCH_OPS.contains(&leaf)
    {
        return Some(leaf.to_string());
    }

    // Bare leaf name: `create` matches `file.create`, `append` matches both file/doc.
    let mut suffix: Vec<&str> = KNOWN_BATCH_OPS
        .iter()
        .copied()
        .filter(|k| k.rsplit_once('.').is_some_and(|(_, leaf)| leaf == op))
        .collect();
    if !suffix.is_empty() {
        suffix.sort_unstable();
        return Some(suffix.join(", "));
    }

    let mut scored: Vec<(&str, f64)> = KNOWN_BATCH_OPS
        .iter()
        .copied()
        .map(|k| (k, strsim::jaro_winkler(op, k)))
        .filter(|(_, score)| *score >= 0.86)
        .collect();
    scored.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));
    if scored.is_empty() {
        return None;
    }
    let top: Vec<&str> = scored.into_iter().take(3).map(|(k, _)| k).collect();
    Some(top.join(", "))
}

fn unknown_batch_op_msg(line_num: usize, op: &str) -> String {
    if let Some(hint) = batch_unsupported_hint(op) {
        return format!("line {line_num}: unknown operation '{op}' ({hint})");
    }
    if let Some(suggestion) = suggest_batch_op(op) {
        return format!("line {line_num}: unknown operation '{op}' (did you mean: {suggestion}?)");
    }
    format!("line {line_num}: unknown operation '{op}'")
}

/// Check that the exact number of arguments were provided.
/// Parse a position keyword ("before" or "after") and return which one was given.
fn parse_position_keyword(
    keyword: &str,
    line_num: usize,
) -> anyhow::Result<(Option<()>, Option<()>)> {
    match keyword {
        "before" => Ok((Some(()), None)),
        "after" => Ok((None, Some(()))),
        _ => Err(anyhow::Error::new(crate::exit::ParseErrorError {
            msg: format!("line {line_num}: expected 'before' or 'after', got '{keyword}'"),
        })),
    }
}

fn require_args(op: &str, args: &[String], expected: usize, line_num: usize) -> anyhow::Result<()> {
    if args.len() != expected {
        let s = if expected == 1 { "" } else { "s" };
        return Err(anyhow::Error::new(crate::exit::ParseErrorError {
            msg: format!(
                "line {line_num}: '{op}' requires exactly {expected} argument{s}, got {}",
                args.len()
            ),
        }));
    }
    Ok(())
}

/// Parse batch `replace path old new [--flags…]` (#1724).
///
/// First three non-flag tokens are path/old/new. Remaining tokens are optional
/// CLI-style flags shared with `patchloom replace`.
fn parse_replace_line(args: &[String], line_num: usize) -> anyhow::Result<Operation> {
    if args.len() < 3 {
        return Err(anyhow::Error::new(crate::exit::ParseErrorError {
            msg: format!(
                "line {line_num}: 'replace' requires at least 3 arguments (path old new), got {}",
                args.len()
            ),
        }));
    }

    let mut path = None;
    let mut old = None;
    let mut new_text = None;
    let mut case_insensitive = false;
    let mut if_exists = false;
    let mut word_boundary = false;
    let mut require_change = false;
    let mut command_position = false;
    let mut fuzzy = false;
    let mut min_fuzzy_score = None;
    let mut allow_absent_old = false;
    let mut i = 0usize;

    while i < args.len() {
        let tok = args[i].as_str();
        // Known flags only. Unknown dash-leading tokens remain positionals so
        // `replace f.md "- old" "- new"` still works (review #1724).
        let is_flag = matches!(
            tok,
            "--fuzzy"
                | "--if-exists"
                | "--require-change"
                | "--command-position"
                | "--word-boundary"
                | "-w"
                | "-i"
                | "--case-insensitive"
                | "--min-fuzzy-score"
                | "--allow-absent-old"
        ) || tok.starts_with("--min-fuzzy-score=");

        if is_flag {
            match tok {
                "--fuzzy" => fuzzy = true,
                "--allow-absent-old" => allow_absent_old = true,
                "--if-exists" => if_exists = true,
                "--require-change" => require_change = true,
                "--command-position" => command_position = true,
                "--word-boundary" | "-w" => word_boundary = true,
                "-i" | "--case-insensitive" => case_insensitive = true,
                flag if flag == "--min-fuzzy-score" || flag.starts_with("--min-fuzzy-score=") => {
                    let raw = if let Some(v) = flag.strip_prefix("--min-fuzzy-score=") {
                        v.to_string()
                    } else {
                        i += 1;
                        args.get(i).cloned().ok_or_else(|| {
                            anyhow::Error::new(crate::exit::ParseErrorError {
                                msg: format!(
                                    "line {line_num}: --min-fuzzy-score requires a value (0.0..=1.0)"
                                ),
                            })
                        })?
                    };
                    let score: f64 = raw.parse().map_err(|_| {
                        anyhow::Error::new(crate::exit::ParseErrorError {
                            msg: format!(
                                "line {line_num}: invalid --min-fuzzy-score value '{raw}' (expected number)"
                            ),
                        })
                    })?;
                    if !(0.0..=1.0).contains(&score) {
                        return Err(anyhow::Error::new(crate::exit::ParseErrorError {
                            msg: format!(
                                "line {line_num}: --min-fuzzy-score must be in 0.0..=1.0 (got {score})"
                            ),
                        }));
                    }
                    min_fuzzy_score = Some(score);
                }
                other => {
                    return Err(anyhow::Error::new(crate::exit::ParseErrorError {
                        msg: format!(
                            "line {line_num}: unknown replace flag '{other}' \
                             (supported: --fuzzy, --allow-absent-old, --min-fuzzy-score, --word-boundary/-w, \
                             --command-position, --require-change, -i/--case-insensitive, --if-exists); \
                             advanced flags (regex, context, nth) need `tx` plan JSON"
                        ),
                    }));
                }
            }
            i += 1;
            continue;
        }

        if tok.starts_with('-')
            && path.is_some()
            && old.is_some()
            && new_text.is_some()
            && tok != "-"
        {
            // After positionals are filled, unknown dash tokens are flag typos.
            return Err(anyhow::Error::new(crate::exit::ParseErrorError {
                msg: format!(
                    "line {line_num}: unknown replace flag '{tok}' \
                     (supported: --fuzzy, --allow-absent-old, --min-fuzzy-score, --word-boundary/-w, \
                     --command-position, --require-change, -i/--case-insensitive, --if-exists); \
                     advanced flags (regex, context, nth) need `tx` plan JSON"
                ),
            }));
        }

        // Positional: path, old, new (in order). May start with '-' (bullet text, flags as values).
        if path.is_none() {
            path = Some(args[i].clone());
        } else if old.is_none() {
            old = Some(args[i].clone());
        } else if new_text.is_none() {
            new_text = Some(args[i].clone());
        } else {
            return Err(anyhow::Error::new(crate::exit::ParseErrorError {
                msg: format!(
                    "line {line_num}: unexpected argument '{}' after replace path/old/new \
                     (use flags like --fuzzy, or `tx` for full replace options)",
                    args[i]
                ),
            }));
        }
        i += 1;
    }

    let path = path.ok_or_else(|| {
        anyhow::Error::new(crate::exit::ParseErrorError {
            msg: format!("line {line_num}: 'replace' requires a path argument"),
        })
    })?;
    let old = old.ok_or_else(|| {
        anyhow::Error::new(crate::exit::ParseErrorError {
            msg: format!("line {line_num}: 'replace' requires an old pattern argument"),
        })
    })?;
    let new_text = new_text.ok_or_else(|| {
        anyhow::Error::new(crate::exit::ParseErrorError {
            msg: format!("line {line_num}: 'replace' requires a new text argument"),
        })
    })?;

    if command_position
        && let Some(msg) = crate::ops::shell_token::command_position_combo_error(
            crate::ops::shell_token::CommandPositionIncompat {
                regex: false,
                case_insensitive,
                word_boundary,
                whole_line: false,
                multiline: false,
                nth: false,
                insert_before: false,
                insert_after: false,
                before_context: false,
                after_context: false,
                fuzzy,
            },
        )
    {
        return Err(anyhow::Error::new(crate::exit::ParseErrorError {
            msg: format!("line {line_num}: {msg}"),
        }));
    }

    Ok(Operation::Replace {
        glob: None,
        path: Some(path),
        regex: false,
        old,
        new_text: Some(new_text),
        nth: None,
        insert_before: None,
        insert_after: None,
        case_insensitive,
        multiline: false,
        if_exists,
        whole_line: false,
        range: None,
        word_boundary,
        before_context: None,
        after_context: None,
        unique: false,
        require_change,
        command_position,
        fuzzy,
        min_fuzzy_score,
        allow_absent_old,
    })
}

/// Parse a string as a JSON value. Delegates to `doc::parse_value` which
/// handles JSON literals, quoted strings, booleans, null, numbers, and
/// bare-string fallback.
fn parse_json_value(s: &str) -> anyhow::Result<serde_json::Value> {
    Ok(crate::ops::doc::parse_value(s))
}

/// Path + free-form content for file.create/append/prepend.
///
/// Requires at least a path. Remaining tokens are joined with a single space so
/// unquoted multi-word content (`file.create f.txt hello world`) works. Prefer
/// JSON-aware tokens (see [`tokenize`]) so unquoted `{"x":1}` is not mangled.
/// Content expands `\n` `\t` `\r` `\\` `\"` so agents can write multi-line files
/// on one batch line (fixrealloop: TOML/Rust scaffolds).
fn path_and_joined_content(
    op: &str,
    args: &[String],
    line_num: usize,
) -> anyhow::Result<(String, String)> {
    if args.is_empty() {
        return Err(anyhow::Error::new(crate::exit::ParseErrorError {
            msg: format!("line {line_num}: '{op}' requires a path argument, got 0"),
        }));
    }
    let path = args[0].clone();
    let raw = if args.len() == 1 {
        String::new()
    } else {
        args[1..].join(" ")
    };
    Ok((path, expand_content_escapes(&raw)))
}

/// Expand common escapes in batch file content (`\n` `\t` `\r` `\\` `\"`).
fn expand_content_escapes(s: &str) -> String {
    let mut out = String::with_capacity(s.len());
    let mut chars = s.chars().peekable();
    while let Some(c) = chars.next() {
        if c != '\\' {
            out.push(c);
            continue;
        }
        match chars.next() {
            Some('n') => out.push('\n'),
            Some('t') => out.push('\t'),
            Some('r') => out.push('\r'),
            Some('\\') => out.push('\\'),
            Some('"') => out.push('"'),
            Some(other) => {
                out.push('\\');
                out.push(other);
            }
            None => out.push('\\'),
        }
    }
    out
}

/// Tokenize a line using shell-like quoting rules.
/// - Whitespace separates tokens
/// - Double-quoted strings preserve spaces and allow escapes (\", \\)
/// - Unquoted `{...}` / `[...]` are brace-balanced (JSON objects/arrays) so
///   agents can write `file.create f.json {"x":1}` without silent quote stripping
pub fn tokenize(line: &str) -> anyhow::Result<Vec<String>> {
    let mut tokens = Vec::new();
    let mut chars = line.chars().peekable();
    let mut current = String::new();
    // Track whether we've entered a token (via quoting or non-whitespace).
    // This ensures empty quoted strings like "" produce an empty-string token
    // instead of being silently dropped.
    let mut in_token = false;

    while let Some(&ch) = chars.peek() {
        if ch.is_whitespace() {
            if in_token {
                tokens.push(std::mem::take(&mut current));
                in_token = false;
            }
            chars.next();
        } else if ch == '"' {
            in_token = true;
            chars.next(); // consume opening quote
            loop {
                match chars.next() {
                    Some('\\') => match chars.next() {
                        Some('"') => current.push('"'),
                        Some('\\') => current.push('\\'),
                        Some(other) => {
                            current.push('\\');
                            current.push(other);
                        }
                        None => {
                            return Err(anyhow::Error::new(crate::exit::ParseErrorError {
                                msg: "unexpected end of line after backslash".into(),
                            }));
                        }
                    },
                    Some('"') => break,
                    Some(c) => current.push(c),
                    None => {
                        return Err(anyhow::Error::new(crate::exit::ParseErrorError {
                            msg: "unterminated double quote".into(),
                        }));
                    }
                }
            }
        } else if !in_token && (ch == '{' || ch == '[') {
            // Brace-balanced JSON token (preserves internal double quotes).
            in_token = true;
            let open = ch;
            let close = if ch == '{' { '}' } else { ']' };
            let mut depth = 0i32;
            let mut in_string = false;
            let mut escape = false;
            loop {
                let c = chars.next().ok_or_else(|| {
                    anyhow::Error::new(crate::exit::ParseErrorError {
                        msg: format!("unterminated JSON value starting with '{open}'"),
                    })
                })?;
                current.push(c);
                if in_string {
                    if escape {
                        escape = false;
                    } else if c == '\\' {
                        escape = true;
                    } else if c == '"' {
                        in_string = false;
                    }
                } else if c == '"' {
                    in_string = true;
                } else if c == open {
                    depth += 1;
                } else if c == close {
                    depth -= 1;
                    if depth == 0 {
                        break;
                    }
                }
            }
        } else {
            in_token = true;
            current.push(ch);
            chars.next();
        }
    }
    if in_token {
        tokens.push(current);
    }
    Ok(tokens)
}

pub fn run(args: BatchArgs, global: &GlobalFlags) -> anyhow::Result<u8> {
    crate::verbose!("batch: input={}", args.input);
    // Read input. Relative paths are resolved under --cwd (same as `tx` plan files).
    let input = if args.input == "-" {
        std::io::read_to_string(std::io::stdin())?
    } else {
        let input_path = global.resolve_user_path(&args.input)?;
        std::fs::read_to_string(&input_path).map_err(|e| {
            if e.kind() == std::io::ErrorKind::NotFound {
                std::io::Error::new(
                    std::io::ErrorKind::NotFound,
                    format!("failed to read '{}': {e}", input_path.display()),
                )
                .into()
            } else {
                anyhow::Error::new(crate::exit::InvalidInputError {
                    msg: format!("failed to read '{}': {e}", input_path.display()),
                })
            }
        })?
    };

    // Parse lines into operations.
    let mut operations = Vec::new();
    for (i, line) in input.lines().enumerate() {
        let trimmed = line.trim();
        if trimmed.is_empty() || trimmed.starts_with('#') {
            continue;
        }
        match parse_line(trimmed, i + 1) {
            Ok(op) => operations.push(op),
            Err(e) => {
                global.emit_error_json_kind(Some("parse_error"), &e.to_string())?;
                return Ok(exit::PARSE_ERROR);
            }
        }
    }

    crate::verbose!("batch: parsed {} operations from input", operations.len());
    if operations.is_empty() {
        if !global.emit_json(&serde_json::json!({
            "ok": true,
            "status": "success",
            "applied": false,
            "files_changed": 0,
            "files_created": 0,
            "files_deleted": 0,
            "changes": []
        }))? && !global.quiet
        {
            eprintln!("batch: no operations found in input");
        }
        return Ok(exit::SUCCESS);
    }

    if operations.len() > MAX_BATCH_OPERATIONS {
        let msg = format!(
            "batch: too many operations ({}, max {MAX_BATCH_OPERATIONS})",
            operations.len()
        );
        global.emit_error_json_kind(Some("invalid_input"), &msg)?;
        return Ok(exit::FAILURE);
    }

    // Build a plan and run the shared tx engine in-process (no temp plan file).
    // Under --contain, re-entering via an absolute NamedTempFile path would be
    // rejected by resolve_user_path; keep the Plan in memory instead.
    let plan = Plan {
        version: crate::plan::SCHEMA_VERSION,
        cwd: None,
        write_policy: None,
        strict: None,
        operations,
        format: None,
        validate: None,
        verify: None,
        for_each: None,
    };
    // Write flags are already merged into global by dispatch before run().
    let _ = args.write;
    crate::cmd::tx::run_parsed_plan(plan, false, &[], global)
}

#[path = "batch_tests.rs"]
#[cfg(test)]
mod tests;