vtcode 0.157.0

A Rust-based terminal coding agent with modular architecture supporting multiple LLM providers
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
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
const OPEN_TAG: &str = "<proposed_plan>";
const CLOSE_TAG: &str = "</proposed_plan>";
const ALT_OPEN_TAG: &str = "<plan>";
const ALT_CLOSE_TAG: &str = "</plan>";

#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct ProposedPlanExtraction {
    pub stripped_text: String,
    pub plan_text: Option<String>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum ParseMode {
    Normal,
    InPlan { close_tag: &'static str },
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum PlanTag {
    Proposed,
    Alternate,
}

impl PlanTag {
    fn open_tag(self) -> &'static str {
        match self {
            Self::Proposed => OPEN_TAG,
            Self::Alternate => ALT_OPEN_TAG,
        }
    }

    fn close_tag(self) -> &'static str {
        match self {
            Self::Proposed => CLOSE_TAG,
            Self::Alternate => ALT_CLOSE_TAG,
        }
    }
}

/// Streaming parser that removes either supported plan block form from
/// assistant-visible text while collecting the plan body.
#[derive(Debug, Default)]
pub(crate) struct ProposedPlanStreamParser {
    mode: Option<ParseMode>,
    pending: String,
    policy_pending: String,
    plan_buffer: String,
    saw_plan_block: bool,
}

impl ProposedPlanStreamParser {
    pub(crate) fn new() -> Self {
        Self {
            mode: Some(ParseMode::Normal),
            pending: String::new(),
            policy_pending: String::new(),
            plan_buffer: String::new(),
            saw_plan_block: false,
        }
    }

    /// Consume streamed text and return only content that should remain visible
    /// to the assistant transcript.
    pub(crate) fn consume(&mut self, chunk: &str) -> String {
        let chunk = self.filter_policy_chunk(chunk, false);
        self.consume_plan_markup(&chunk)
    }

    fn consume_plan_markup(&mut self, chunk: &str) -> String {
        self.pending.push_str(chunk);
        let mut visible = String::new();

        loop {
            match self.mode.unwrap_or(ParseMode::Normal) {
                ParseMode::Normal => {
                    if let Some((index, tag)) = find_next_open_tag(&self.pending) {
                        visible.push_str(&self.pending[..index]);
                        self.pending.drain(..index + tag.open_tag().len());
                        self.mode = Some(ParseMode::InPlan { close_tag: tag.close_tag() });
                        self.saw_plan_block = true;
                        continue;
                    }

                    let keep_tail = OPEN_TAG.len().max(ALT_OPEN_TAG.len()).saturating_sub(1).min(self.pending.len());
                    let emit_len = safe_char_boundary(&self.pending, self.pending.len().saturating_sub(keep_tail));
                    visible.push_str(&self.pending[..emit_len]);
                    self.pending.drain(..emit_len);
                    break;
                }
                ParseMode::InPlan { close_tag } => {
                    if let Some(index) = self.pending.find(close_tag) {
                        self.plan_buffer.push_str(&self.pending[..index]);
                        self.pending.drain(..index + close_tag.len());
                        self.mode = Some(ParseMode::Normal);
                        continue;
                    }

                    let keep_tail = close_tag.len().saturating_sub(1).min(self.pending.len());
                    let append_len = safe_char_boundary(&self.pending, self.pending.len().saturating_sub(keep_tail));
                    self.plan_buffer.push_str(&self.pending[..append_len]);
                    self.pending.drain(..append_len);
                    break;
                }
            }
        }

        visible
    }

    /// Finish parsing and return any remaining visible text plus optional plan.
    pub(crate) fn finish(&mut self) -> ProposedPlanExtraction {
        let policy_trailing = self.flush_policy_pending();
        let mut trailing_visible = self.consume_plan_markup(&policy_trailing);
        match self.mode.unwrap_or(ParseMode::Normal) {
            ParseMode::Normal => {
                trailing_visible.push_str(&self.pending);
            }
            ParseMode::InPlan { .. } => {
                // Unterminated block: treat the remainder as plan content.
                self.plan_buffer.push_str(&self.pending);
            }
        }
        self.pending.clear();
        self.mode = Some(ParseMode::Normal);

        ProposedPlanExtraction {
            stripped_text: trailing_visible,
            plan_text: finalize_plan_text(self.saw_plan_block, &self.plan_buffer),
        }
    }

    pub(crate) fn has_unclosed_plan_block(&self) -> bool {
        matches!(self.mode, Some(ParseMode::InPlan { .. })) || has_partial_open_tag(&self.pending)
    }

    fn filter_policy_chunk(&mut self, chunk: &str, flush: bool) -> String {
        self.policy_pending.push_str(chunk);
        filter_policy_text(&mut self.policy_pending, flush)
    }

    fn flush_policy_pending(&mut self) -> String {
        self.filter_policy_chunk("", true)
    }
}

pub(crate) fn extract_proposed_plan(text: &str) -> ProposedPlanExtraction {
    let mut parser = ProposedPlanStreamParser::new();
    let mut stripped = parser.consume(text);
    let trailing = parser.finish();
    stripped.push_str(&trailing.stripped_text);

    ProposedPlanExtraction {
        stripped_text: stripped,
        plan_text: trailing.plan_text,
    }
}

pub(crate) fn extract_any_plan(text: &str) -> ProposedPlanExtraction {
    extract_proposed_plan(text)
}

/// Recovery synthesis accepts one canonical plan block only. Normal planning
/// responses continue to support the legacy `<plan>` alias, but the bounded
/// tool-free recovery contract must be unambiguous before it can be persisted
/// or shown for approval.
pub(crate) fn has_exactly_one_proposed_plan_block(text: &str) -> bool {
    let text = strip_plan_persistence_policy_line(text);
    let Some(open_index) = text.find(OPEN_TAG) else {
        return false;
    };
    let Some(close_index) = text.find(CLOSE_TAG) else {
        return false;
    };
    open_index < close_index
        && text.matches(OPEN_TAG).count() == 1
        && text.matches(CLOSE_TAG).count() == 1
        && !text.contains(ALT_OPEN_TAG)
        && !text.contains(ALT_CLOSE_TAG)
}

pub(crate) fn has_unclosed_plan_block(text: &str) -> bool {
    let mut parser = ProposedPlanStreamParser::new();
    parser.consume(text);
    parser.has_unclosed_plan_block()
}

/// Remove the runtime-owned persistence policy when a provider repeats it as
/// assistant prose around a plan block. The exact line is intentionally
/// bounded and prompt-owned; arbitrary model text is left untouched.
pub(crate) fn strip_plan_persistence_policy_line(text: &str) -> String {
    let stripped = text.replace(vtcode_core::prompts::system::PLANNING_WORKFLOW_PLAN_PERSISTENCE_POLICY_LINE, "");
    if stripped == text {
        text.to_string()
    } else {
        stripped.trim().to_string()
    }
}

/// Harness display contract for propose-plan markdown.
///
/// The harness must guarantee that any markdown content shown to the user is
/// parsed and rendered accurately: plan wrappers are removed, headings/lists
/// keep their structure, and failures produce clear, actionable feedback
/// instead of raw `<proposed_plan>` text.
///
/// Returns `(display_markdown, warnings)` where `display_markdown` is safe to
/// hand to the markdown renderer and `warnings` describes anything that had to
/// be repaired (unclosed blocks, empty plans, stray markup).
pub(crate) fn prepare_plan_markdown_for_display(raw: &str) -> (String, Vec<String>) {
    let mut warnings = Vec::new();
    if raw.trim().is_empty() {
        warnings.push("Plan content was empty; nothing to render.".to_string());
        return (String::new(), warnings);
    }

    let had_open = contains_plan_tag_outside_fences(raw, &[OPEN_TAG, ALT_OPEN_TAG]);
    let had_close = contains_plan_tag_outside_fences(raw, &[CLOSE_TAG, ALT_CLOSE_TAG]);
    if had_open && !had_close {
        warnings.push("Plan block was missing its closing tag; showing the partial draft.".to_string());
    }

    // Prefer the extracted plan body, but fall back to tag-stripped prose so a
    // tag-less structured synthesis still renders instead of vanishing.
    let mut body = extract_plan_body_for_display(raw).unwrap_or_else(|| strip_all_plan_tags(raw));
    body = strip_plan_persistence_policy_line(&body);

    if body.trim().is_empty() {
        warnings.push("Plan block was empty after removing markup; check the model output.".to_string());
        return (String::new(), warnings);
    }

    let normalized = normalize_plan_display_markdown(&body);

    if contains_plan_tag_outside_fences(&normalized, &[OPEN_TAG, CLOSE_TAG, ALT_OPEN_TAG, ALT_CLOSE_TAG]) {
        warnings.push("Stray plan markup remained after cleanup and was left as-is.".to_string());
    }

    (normalized, warnings)
}

/// Strip every plan wrapper tag outside fenced code for display purposes,
/// regardless of pairing. Literal tags in code examples remain untouched.
pub(crate) fn strip_all_plan_tags(text: &str) -> String {
    let mut out = String::with_capacity(text.len());
    let mut in_fenced_code = false;
    let mut inline_code_ticks = None;
    for (index, raw_line) in text.split_inclusive('\n').enumerate() {
        if index > 0 && !out.is_empty() && !out.ends_with('\n') {
            out.push('\n');
        }
        let line = raw_line.strip_suffix('\n').unwrap_or(raw_line);
        let is_fence = is_fence_delimiter(line);
        if in_fenced_code || is_fence {
            out.push_str(raw_line);
        } else {
            out.push_str(&strip_plan_tags(raw_line, &mut inline_code_ticks));
        }
        if is_fence {
            in_fenced_code = !in_fenced_code;
            inline_code_ticks = None;
        }
    }
    out.trim().to_string()
}

fn strip_plan_tags(text: &str, inline_code_ticks: &mut Option<usize>) -> String {
    let mut out = String::with_capacity(text.len());
    let mut cursor = 0;
    while cursor < text.len() {
        let Some((index, tag)) = find_plan_tag_outside_inline_code(
            &text[cursor..],
            &[OPEN_TAG, CLOSE_TAG, ALT_OPEN_TAG, ALT_CLOSE_TAG],
            inline_code_ticks,
        ) else {
            out.push_str(&text[cursor..]);
            break;
        };
        out.push_str(&text[cursor..cursor + index]);
        cursor += index + tag.len();
    }
    out
}

fn contains_plan_tag_outside_fences(text: &str, tags: &[&'static str]) -> bool {
    let mut in_fenced_code = false;
    let mut inline_code_ticks = None;
    for raw_line in text.split_inclusive('\n') {
        let line = raw_line.strip_suffix('\n').unwrap_or(raw_line);
        let is_fence = is_fence_delimiter(line);
        if !in_fenced_code
            && !is_fence
            && find_plan_tag_outside_inline_code(line, tags, &mut inline_code_ticks).is_some()
        {
            return true;
        }
        if is_fence {
            in_fenced_code = !in_fenced_code;
            inline_code_ticks = None;
        }
    }
    false
}

/// Find a plan wrapper outside inline-code spans while carrying the span state
/// across lines. The returned tag is static because all callers pass one of
/// the module's canonical wrapper constants.
fn find_plan_tag_outside_inline_code(
    text: &str,
    tags: &[&'static str],
    inline_code_ticks: &mut Option<usize>,
) -> Option<(usize, &'static str)> {
    let mut cursor = 0;
    while cursor < text.len() {
        let remainder = &text[cursor..];
        if remainder.starts_with('`') {
            let run_length = remainder.bytes().take_while(|byte| *byte == b'`').count();
            if inline_code_ticks.is_some_and(|ticks| ticks == run_length) {
                *inline_code_ticks = None;
            } else if inline_code_ticks.is_none() {
                *inline_code_ticks = Some(run_length);
            }
            cursor += run_length;
            continue;
        }

        if inline_code_ticks.is_none()
            && let Some(tag) = tags.iter().find(|tag| {
                remainder
                    .get(..tag.len())
                    .is_some_and(|prefix| prefix.eq_ignore_ascii_case(tag))
            })
        {
            return Some((cursor, *tag));
        }

        let character = remainder.chars().next().expect("cursor is on a character boundary");
        cursor += character.len_utf8();
    }
    None
}

/// Extract a plan body while ignoring wrapper-looking text inside fenced code.
/// The streaming parser remains responsible for live transcript suppression;
/// this full-input path is used for the approval display, where preserving the
/// exact code block is more important than incremental output.
fn extract_plan_body_for_display(text: &str) -> Option<String> {
    let mut close_tag: Option<&'static str> = None;
    let mut body = String::new();
    let mut in_fenced_code = false;
    let mut inline_code_ticks = None;
    let mut saw_plan = false;

    for raw_line in text.split_inclusive('\n') {
        let line = raw_line.strip_suffix('\n').unwrap_or(raw_line);
        let is_fence = is_fence_delimiter(line);
        if in_fenced_code || is_fence {
            if saw_plan {
                body.push_str(raw_line);
            }
            if is_fence {
                in_fenced_code = !in_fenced_code;
                inline_code_ticks = None;
            }
            continue;
        }

        let mut remaining = raw_line;
        loop {
            if let Some(expected_close) = close_tag {
                if let Some((index, tag)) =
                    find_plan_tag_outside_inline_code(remaining, &[expected_close], &mut inline_code_ticks)
                {
                    body.push_str(&remaining[..index]);
                    remaining = &remaining[index + tag.len()..];
                    close_tag = None;
                    continue;
                }
                body.push_str(remaining);
                break;
            }

            let Some((index, tag)) =
                find_plan_tag_outside_inline_code(remaining, &[OPEN_TAG, ALT_OPEN_TAG], &mut inline_code_ticks)
            else {
                break;
            };
            saw_plan = true;
            close_tag = Some(if tag == OPEN_TAG { CLOSE_TAG } else { ALT_CLOSE_TAG });
            remaining = &remaining[index + tag.len()..];
        }
    }

    saw_plan.then(|| body.trim().to_string())
}

/// Normalize plan prose so the markdown renderer preserves structure:
/// - `•` bullets become `-` (the marker pulldown-cmark recognizes)
/// - bare `Summary:` / `Test Cases and Validation:` / `Assumptions…` labels
///   become `##` headings so they read as sections instead of a wall of text
/// - long `1. Action -> files: […] -> verify: […]` steps are split so the
///   metadata renders as nested bullets instead of one unwrapped wall of text
/// - missing blank lines around headings/lists are repaired so the parser
///   emits distinct blocks instead of one collapsed paragraph.
fn normalize_plan_display_markdown(body: &str) -> String {
    const HEADING_ALIASES: &[(&str, &str)] = &[
        ("summary", "## Summary"),
        ("implementation steps", "## Implementation Steps"),
        ("test cases and validation", "## Test Cases and Validation"),
        ("validation", "## Test Cases and Validation"),
        ("assumptions and defaults", "## Assumptions and Defaults"),
        ("assumptions", "## Assumptions and Defaults"),
        ("open questions", "## Open Questions"),
    ];

    struct DisplayLine {
        text: String,
        structural: bool,
    }

    let mut lines: Vec<DisplayLine> = Vec::with_capacity(body.lines().count() + 8);
    let mut in_fenced_code = false;
    for line in body.lines() {
        let trimmed_start = line.trim_start();
        let is_fence = is_fence_delimiter(line);
        if in_fenced_code || is_fence {
            lines.push(DisplayLine { text: line.to_string(), structural: false });
            if is_fence {
                in_fenced_code = !in_fenced_code;
            }
            continue;
        }

        let indent_len = line.len() - trimmed_start.len();
        let indent = &line[..indent_len];

        // Normalize bullets first so list structure survives rendering.
        let mut current = if let Some(rest) = trimmed_start.strip_prefix("") {
            format!("{indent}- {rest}")
        } else if trimmed_start == "" {
            format!("{indent}-")
        } else {
            line.to_string()
        };

        // Promote sparse `Label:` lines to headings. Only exact label matches
        // (case-insensitive, optional trailing colon) or `Label: prose` inline
        // forms are rewritten; numbered steps and prose containing those words
        // elsewhere are left untouched.
        let trimmed_owned = current.trim().to_string();
        let trimmed: &str = &trimmed_owned;
        let label = trimmed.strip_suffix(':').unwrap_or(trimmed).trim();
        let lowered = label.to_ascii_lowercase();
        if !label.is_empty() && label.len() <= 64 {
            if let Some((_, heading)) = HEADING_ALIASES.iter().find(|(alias, _)| *alias == lowered) {
                if trimmed.ends_with(':') {
                    current = heading.to_string();
                }
            }
        }

        if trimmed.len() <= 256
            && let Some((_, heading)) = HEADING_ALIASES
                .iter()
                .find(|(alias, _)| trimmed.to_ascii_lowercase().starts_with(&format!("{alias}:")))
        {
            // e.g. `Summary: the fix ...` -> heading + paragraph.
            if let Some(colon) = trimmed.find(':') {
                let after = trimmed[colon + 1..].trim();
                let heading_owned = heading.to_string();
                if !after.is_empty() {
                    lines.push(DisplayLine { text: heading_owned, structural: true });
                    lines.push(DisplayLine { text: String::new(), structural: true });
                    lines.push(DisplayLine { text: after.to_string(), structural: true });
                    continue;
                }
                current = heading_owned;
            }
        }

        // Split long `1. Action -> files: […] -> verify: […]` steps so the
        // metadata renders as nested bullets. A single-line step with two
        // `->` clauses wraps without a hanging indent and reads as a wall of
        // text (Sep-04 screenshot); nested `- files:` / `- verify:` lines keep
        // the ordered-list structure while staying scannable. Fenced code is
        // already excluded above. Splits happen only at ` -> files:` /
        // ` -> verify:` outside inline-code spans so `` `a -> files: b` ``
        // and prose arrows (`A -> B`) stay intact.
        if is_ordered_list_item(current.trim())
            && let Some(parts) = split_ordered_step_metadata(current.trim())
        {
            let marker_width = ordered_marker_width(current.trim());
            let nested_prefix = format!("{indent}{:width$}- ", "", width = marker_width);
            lines.push(DisplayLine {
                text: format!("{indent}{}", parts[0].trim_end()),
                structural: true,
            });
            for part in &parts[1..] {
                let part = part.trim();
                if part.is_empty() {
                    continue;
                }
                lines.push(DisplayLine {
                    text: format!("{nested_prefix}{part}"),
                    structural: true,
                });
            }
            continue;
        }

        lines.push(DisplayLine { text: current, structural: true });
    }

    // Ensure blank lines around headings and lists so the markdown parser does
    // not collapse them into a single paragraph.
    let mut spaced: Vec<String> = Vec::with_capacity(lines.len() * 2);
    for (idx, line) in lines.iter().enumerate() {
        let trimmed = line.text.trim();
        let is_heading = line.structural && trimmed.starts_with("## ");
        let is_list = line.structural
            && (trimmed.starts_with("- ")
                || trimmed.starts_with("* ")
                || trimmed.starts_with("+ ")
                || is_ordered_list_item(trimmed));
        let prev_blank = spaced.last().is_some_and(|prev: &String| prev.trim().is_empty());
        if (is_heading || is_list) && idx > 0 && !prev_blank {
            spaced.push(String::new());
        }
        spaced.push(line.text.clone());
        let next_is_list_or_heading = lines.get(idx + 1).map(|next| {
            let t = next.text.trim();
            next.structural
                && (t.starts_with("## ") || t.starts_with("- ") || t.starts_with("* ") || is_ordered_list_item(t))
        });
        if (is_heading || is_list) && next_is_list_or_heading == Some(false) {
            if let Some(next) = lines.get(idx + 1)
                && !next.text.trim().is_empty()
            {
                spaced.push(String::new());
            }
        }
    }

    spaced.join("\n").trim().to_string()
}

fn is_fence_delimiter(line: &str) -> bool {
    vtcode_commons::formatting::is_markdown_fence_delimiter(line)
}

fn is_ordered_list_item(trimmed: &str) -> bool {
    let mut chars = trimmed.chars();
    let mut saw_digit = false;
    for ch in chars.by_ref() {
        if ch.is_ascii_digit() {
            saw_digit = true;
        } else {
            break;
        }
    }
    if !saw_digit {
        return false;
    }
    let rest: String = trimmed.chars().skip_while(|c| c.is_ascii_digit()).collect();
    rest.starts_with(". ") || rest.starts_with(") ")
}

/// Width of the ordered marker (`1. ` -> 3, `10. ` -> 4) so nested bullets
/// align under the item content instead of a fixed 3-space guess.
fn ordered_marker_width(trimmed: &str) -> usize {
    let digits = trimmed.chars().take_while(|c| c.is_ascii_digit()).count();
    digits + 2
}

/// Split `1. Action -> files: […] -> verify: […]` at metadata boundaries
/// outside inline-code spans. Returns head + metadata parts; `None` when no
/// ` -> files:` / ` -> verify:` marker exists outside backticks.
fn split_ordered_step_metadata(trimmed: &str) -> Option<Vec<String>> {
    let mut splits = Vec::new();
    let mut inline_ticks: Option<usize> = None;
    let mut cursor = 0;
    while cursor < trimmed.len() {
        let remainder = &trimmed[cursor..];
        if remainder.starts_with('`') {
            let run = remainder.bytes().take_while(|byte| *byte == b'`').count();
            if inline_ticks.is_some_and(|ticks| ticks == run) {
                inline_ticks = None;
            } else if inline_ticks.is_none() {
                inline_ticks = Some(run);
            }
            cursor += run;
            continue;
        }
        if inline_ticks.is_none() && (remainder.starts_with(" -> files:") || remainder.starts_with(" -> verify:")) {
            splits.push(cursor);
            cursor += " -> ".len();
            continue;
        }
        let character = remainder.chars().next().expect("cursor is on a character boundary");
        cursor += character.len_utf8();
    }
    if splits.is_empty() {
        return None;
    }
    let mut parts = Vec::with_capacity(splits.len() + 1);
    let mut start = 0;
    for split in splits {
        parts.push(trimmed[start..split].to_string());
        start = split + " -> ".len();
    }
    parts.push(trimmed[start..].to_string());
    Some(parts)
}

fn find_next_open_tag(text: &str) -> Option<(usize, PlanTag)> {
    [PlanTag::Proposed, PlanTag::Alternate]
        .into_iter()
        .filter_map(|tag| text.find(tag.open_tag()).map(|index| (index, tag)))
        .min_by_key(|(index, _)| *index)
}

fn has_partial_open_tag(text: &str) -> bool {
    [OPEN_TAG, ALT_OPEN_TAG]
        .into_iter()
        .any(|tag| (1..tag.len()).any(|prefix_len| text.ends_with(&tag[..prefix_len])))
}

fn filter_policy_text(buffer: &mut String, flush: bool) -> String {
    let policy = vtcode_core::prompts::system::PLANNING_WORKFLOW_PLAN_PERSISTENCE_POLICY_LINE;
    let mut visible = String::new();

    loop {
        let Some(start) = find_policy_candidate_start(buffer, policy) else {
            visible.push_str(buffer);
            buffer.clear();
            break;
        };

        visible.push_str(&buffer[..start]);
        buffer.drain(..start);
        if buffer.starts_with(policy) {
            buffer.drain(..policy.len());
            continue;
        }
        if !flush && policy.starts_with(buffer.as_str()) {
            break;
        }

        visible.push_str(buffer);
        buffer.clear();
        break;
    }

    visible
}

fn find_policy_candidate_start(text: &str, policy: &str) -> Option<usize> {
    let mut line_start = 0;
    loop {
        let candidate = &text[line_start..];
        if policy.starts_with(candidate) || candidate.starts_with(policy) {
            return Some(line_start);
        }
        let newline = text[line_start..].find('\n')?;
        line_start += newline + 1;
        if line_start >= text.len() {
            return None;
        }
    }
}

fn finalize_plan_text(saw_plan_block: bool, raw: &str) -> Option<String> {
    if !saw_plan_block {
        return None;
    }
    let trimmed = raw.trim();
    if trimmed.is_empty() {
        None
    } else {
        Some(trimmed.to_string())
    }
}

fn safe_char_boundary(text: &str, idx: usize) -> usize {
    if idx >= text.len() {
        return text.len();
    }
    if text.is_char_boundary(idx) {
        return idx;
    }
    text.char_indices()
        .take_while(|(pos, _)| *pos < idx)
        .last()
        .map(|(pos, _)| pos)
        .unwrap_or(0)
}

#[cfg(test)]
mod tests {
    use super::{
        ProposedPlanStreamParser, extract_any_plan, extract_proposed_plan, has_exactly_one_proposed_plan_block,
        has_unclosed_plan_block, prepare_plan_markdown_for_display, strip_plan_persistence_policy_line,
    };

    #[test]
    fn extracts_single_proposed_plan_block() {
        let extraction = extract_proposed_plan("Intro\n<proposed_plan>\n- A\n- B\n</proposed_plan>\nOutro");
        assert_eq!(extraction.stripped_text, "Intro\n\nOutro");
        assert_eq!(extraction.plan_text.as_deref(), Some("- A\n- B"));
    }

    #[test]
    fn keeps_text_when_no_plan_block_exists() {
        let extraction = extract_proposed_plan("No plan here");
        assert_eq!(extraction.stripped_text, "No plan here");
        assert!(extraction.plan_text.is_none());
    }

    #[test]
    fn handles_unterminated_plan_block() {
        let extraction = extract_proposed_plan("Before<proposed_plan>\n- Step 1\n- Step 2");
        assert_eq!(extraction.stripped_text, "Before");
        assert_eq!(extraction.plan_text.as_deref(), Some("- Step 1\n- Step 2"));
        assert!(has_unclosed_plan_block("Before<proposed_plan>\n- Step 1\n- Step 2"));
    }

    #[test]
    fn extracts_alternate_plan_block_through_shared_parser() {
        let extraction = extract_any_plan("Intro\n<plan>\n- A\n- B\n</plan>\nOutro");
        assert_eq!(extraction.stripped_text, "Intro\n\nOutro");
        assert_eq!(extraction.plan_text.as_deref(), Some("- A\n- B"));
        assert!(!has_unclosed_plan_block("Intro\n<plan>\n- A\n- B\n</plan>\nOutro"));
    }

    #[test]
    fn handles_unterminated_alternate_plan_block() {
        let extraction = extract_any_plan("Before<plan>\n- Step 1\n- Step 2");
        assert_eq!(extraction.stripped_text, "Before");
        assert_eq!(extraction.plan_text.as_deref(), Some("- Step 1\n- Step 2"));
        assert!(has_unclosed_plan_block("Before<plan>\n- Step 1\n- Step 2"));
        assert!(has_unclosed_plan_block("Before<plan"));
    }

    #[test]
    fn supports_streaming_chunks_with_split_tags() {
        let mut parser = ProposedPlanStreamParser::new();
        let mut visible = String::new();
        visible.push_str(&parser.consume("Intro\n<propo"));
        visible.push_str(&parser.consume("sed_plan>\n- Step"));
        visible.push_str(&parser.consume(" 1\n</proposed_plan>\nOutro"));
        let trailing = parser.finish();
        visible.push_str(&trailing.stripped_text);

        assert_eq!(visible, "Intro\n\nOutro");
        assert_eq!(trailing.plan_text.as_deref(), Some("- Step 1"));
    }

    #[test]
    fn supports_streaming_chunks_with_split_alternate_tags() {
        let mut parser = ProposedPlanStreamParser::new();
        let mut visible = String::new();
        visible.push_str(&parser.consume("Intro\n<pl"));
        visible.push_str(&parser.consume("an>\n- Step"));
        visible.push_str(&parser.consume(" 1\n</plan>\nOutro"));
        let trailing = parser.finish();
        visible.push_str(&trailing.stripped_text);

        assert_eq!(visible, "Intro\n\nOutro");
        assert_eq!(trailing.plan_text.as_deref(), Some("- Step 1"));
    }

    #[test]
    fn handles_multibyte_text_without_panicking() {
        let mut parser = ProposedPlanStreamParser::new();
        let mut visible = String::new();
        visible.push_str(&parser.consume("an’t exit on my"));
        let trailing = parser.finish();
        visible.push_str(&trailing.stripped_text);

        assert_eq!(visible, "an’t exit on my");
        assert!(trailing.plan_text.is_none());
    }

    #[test]
    fn strips_only_the_known_plan_persistence_policy_line() {
        let policy = vtcode_core::prompts::system::PLANNING_WORKFLOW_PLAN_PERSISTENCE_POLICY_LINE;
        let text = format!("{policy}\n\nResearch summary");
        assert_eq!(strip_plan_persistence_policy_line(&text), "Research summary");
        assert_eq!(strip_plan_persistence_policy_line("unrelated prose"), "unrelated prose");
    }

    #[test]
    fn policy_echo_does_not_look_like_an_unclosed_plan_block() {
        let policy = vtcode_core::prompts::system::PLANNING_WORKFLOW_PLAN_PERSISTENCE_POLICY_LINE;
        let response = format!("{policy}\n<plan>\n- Step 1\n</plan>");
        let extraction = extract_any_plan(&response);
        assert_eq!(extraction.plan_text.as_deref(), Some("- Step 1"));
        assert!(!extraction.stripped_text.contains("Emit exactly one final"));

        let split_at = policy.find("<proposed_plan>").expect("policy tag");
        let mut parser = ProposedPlanStreamParser::new();
        let mut visible = parser.consume(&policy[..split_at]);
        visible.push_str(&parser.consume(&policy[split_at..]));
        visible.push_str(&parser.consume("\n<plan>\n- Step 1\n</plan>"));
        let trailing = parser.finish();
        visible.push_str(&trailing.stripped_text);
        assert!(!visible.contains("Emit exactly one final"));
        assert_eq!(trailing.plan_text.as_deref(), Some("- Step 1"));
    }

    #[test]
    fn recovery_plan_shape_requires_one_canonical_block() {
        assert!(has_exactly_one_proposed_plan_block("<proposed_plan>\n- Step 1\n</proposed_plan>"));
        assert!(!has_exactly_one_proposed_plan_block("<plan>\n- Step 1\n</plan>"));
        assert!(!has_exactly_one_proposed_plan_block(
            "<proposed_plan>\n- A\n</proposed_plan>\n<proposed_plan>\n- B\n</proposed_plan>"
        ));
        assert!(!has_exactly_one_proposed_plan_block("<proposed_plan>\n- Missing close"));
        assert!(!has_exactly_one_proposed_plan_block("<proposed_plan>\n- A\n</proposed_plan>\n</proposed_plan>"));
        assert!(!has_exactly_one_proposed_plan_block("</proposed_plan>\n<proposed_plan>\n- A\n"));
    }

    #[test]
    fn display_preparation_strips_plan_wrappers_and_preserves_structure() {
        let raw = "<proposed_plan>\nSummary: Fix scrolling.\n\n1. Locate modal -> files: [src/modal.rs] -> verify: [cargo check]\n\nTest Cases and Validation:\n\n• Down reaches the final item.\n\nAssumptions and Defaults:\n\n• Paths are stale.\n</proposed_plan>";
        let (display, warnings) = prepare_plan_markdown_for_display(raw);
        assert!(!display.contains("<proposed_plan>"));
        assert!(!display.contains("</proposed_plan>"));
        assert!(display.contains("## Summary"));
        assert!(display.contains("## Test Cases and Validation"));
        assert!(display.contains("## Assumptions and Defaults"));
        assert!(display.contains("- Down reaches the final item."));
        assert!(display.contains("1. Locate modal"));
        // Long steps are split so metadata renders as nested bullets instead
        // of one unwrapped wall of text (Sep-04 screenshot).
        assert!(display.contains("- files: [src/modal.rs]"));
        assert!(display.contains("- verify: [cargo check]"));
        assert!(warnings.is_empty(), "well-formed plan should not warn: {warnings:?}");
    }

    #[test]
    fn display_preparation_splits_screenshot_style_long_steps() {
        let raw = "<proposed_plan>\nSummary: The approved /config scrolling fix could not be implemented.\n\n1. Locate the actual /config list-modal implementation -> files: [src/a.rs, crates/b.rs] -> verify: [rg -n \"x\" src]\n\nTest Cases and Validation:\n\n• Down reaches the final item.\n\nAssumptions and Defaults:\n\n• Paths are stale.\n</proposed_plan>";
        let (display, warnings) = prepare_plan_markdown_for_display(raw);
        assert!(!display.contains("<proposed_plan>"));
        assert!(display.contains("## Summary"));
        assert!(display.contains("1. Locate the actual /config list-modal implementation"));
        assert!(display.contains("- files: [src/a.rs, crates/b.rs]"));
        assert!(display.contains("- verify:"));
        assert!(display.contains("## Test Cases and Validation"));
        assert!(display.contains("- Down reaches the final item."));
        assert!(warnings.is_empty(), "well-formed plan should not warn: {warnings:?}");
    }

    #[test]
    fn display_preparation_warns_on_unclosed_and_empty_plans() {
        let (partial, warnings) = prepare_plan_markdown_for_display("<proposed_plan>\n- Step 1");
        assert!(partial.contains("- Step 1"));
        assert!(warnings.iter().any(|w| w.contains("closing tag")));

        let (empty, warnings) = prepare_plan_markdown_for_display("<proposed_plan>\n   \n</proposed_plan>");
        assert!(empty.is_empty());
        assert!(!warnings.is_empty());

        let (blank, warnings) = prepare_plan_markdown_for_display("   ");
        assert!(blank.is_empty());
        assert!(!warnings.is_empty());
    }

    #[test]
    fn display_preparation_preserves_fenced_code() {
        let raw = "<proposed_plan>\n## Summary\n```text\n• literal\nSummary:\n```\n</proposed_plan>";
        let (display, warnings) = prepare_plan_markdown_for_display(raw);

        assert!(warnings.is_empty(), "well-formed plan should not warn: {warnings:?}");
        assert!(display.contains("```text\n• literal\nSummary:\n```"));
    }

    #[test]
    fn display_preparation_does_not_treat_fenced_plan_tags_as_wrappers() {
        let raw = "<proposed_plan>\n```text\n<proposed_plan>\nliteral\n</proposed_plan>\n```\n</proposed_plan>";
        let (display, warnings) = prepare_plan_markdown_for_display(raw);

        assert!(warnings.is_empty(), "well-formed plan should not warn: {warnings:?}");
        assert!(display.contains("<proposed_plan>\nliteral\n</proposed_plan>"));

        let code_only = "```text\n<plan>\nliteral\n</plan>\n```";
        let (display, warnings) = prepare_plan_markdown_for_display(code_only);
        assert!(warnings.is_empty(), "code-only markdown should not warn: {warnings:?}");
        assert_eq!(display, code_only);
    }

    #[test]
    fn display_preparation_preserves_inline_plan_tags() {
        let code_only = "Use `<plan>` and `<proposed_plan>` as literal wrapper names.";
        let (display, warnings) = prepare_plan_markdown_for_display(code_only);

        assert!(warnings.is_empty(), "inline code should not be treated as plan markup: {warnings:?}");
        assert_eq!(display, code_only);
    }
}