newt-core 0.8.0

Newt-Agent core types, errors, and the NeMoCode-style tier router
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
//! Block-aware streaming writer (Step 25.3).
//!
//! The agentic loop streams the model's reply token-by-token. We cannot render
//! a half-open code fence or a table mid-arrival, so this writer line-buffers:
//! a *completed* inline line is rendered and emitted immediately (so the stream
//! still feels live), while a multi-line block construct — fenced code, GFM
//! table, list, blockquote — is **held** until it closes, then rendered whole
//! via [`render_markdown`]. Holding to completed lines also dissolves the
//! split-inline-marker problem: a `**` split across two tokens reunites in the
//! line buffer before the line is ever rendered.
//!
//! With `color == false` this is a byte-for-byte passthrough — identical to the
//! raw token stream today.

use super::{render_markdown, RenderOpts};
use std::io::{self, Write};

/// A multi-line block being accumulated until it closes.
#[derive(Clone, PartialEq)]
enum Hold {
    /// Inside a fenced code block; carries the opening fence run (``` / ~~~).
    Fence(String),
    /// Consecutive blockquote (`>`) lines.
    Quote,
    /// A list and its (indented) continuations.
    List,
    /// A pipe table. `confirmed` flips true once the delimiter row is seen;
    /// until then the opening line might just be a paragraph containing `|`.
    Table { confirmed: bool },
}

/// Streams Markdown to `out`, rendering block-by-block as content completes.
pub struct MarkdownStreamWriter<W: Write> {
    out: W,
    color: bool,
    cols: usize,
    /// The current line being accumulated (no trailing newline yet).
    line_buf: String,
    /// The multi-line block currently held open, if any.
    hold: Option<Hold>,
    /// Raw source of the held block.
    block_buf: String,
}

impl<W: Write> MarkdownStreamWriter<W> {
    pub fn new(out: W, opts: RenderOpts) -> Self {
        Self {
            out,
            color: opts.color,
            cols: opts.cols.max(8),
            line_buf: String::new(),
            hold: None,
            block_buf: String::new(),
        }
    }

    /// Feed a token delta. Completed lines are processed; the trailing partial
    /// line stays buffered until its newline arrives (or [`finish`]).
    pub fn push(&mut self, delta: &str) -> io::Result<()> {
        if !self.color {
            return self.out.write_all(delta.as_bytes());
        }
        self.line_buf.push_str(delta);
        while let Some(nl) = self.line_buf.find('\n') {
            let line: String = self.line_buf.drain(..=nl).collect();
            // Drop the trailing '\n' — line classifiers want the bare line.
            let line = line.trim_end_matches('\n').to_string();
            self.consume_line(line)?;
        }
        Ok(())
    }

    /// Give the sink back after [`finish`] (`BufWriter::into_inner`'s shape,
    /// minus the error case — nothing is buffered here once `finish` has run).
    ///
    /// #123: the streamed-answer path hands its output sink to this writer and
    /// needs it back to print the cut-stream notice *below* the answer, on the
    /// same sink. Taking the sink back is what keeps ownership single —
    /// the alternative is a second handle to the same stream, which is how
    /// two writers end up interleaving on one terminal line.
    pub fn into_inner(self) -> W {
        self.out
    }

    /// Flush the trailing partial line and any still-open block at end of stream.
    pub fn finish(&mut self) -> io::Result<()> {
        if self.color && !self.line_buf.is_empty() {
            let line = std::mem::take(&mut self.line_buf);
            self.consume_line(line)?;
        }
        match self.hold.clone() {
            // An opening `|` line that never got its delimiter row is just a
            // paragraph — emit the buffered lines as inline.
            Some(Hold::Table { confirmed: false }) => {
                let buf = std::mem::take(&mut self.block_buf);
                self.hold = None;
                for l in buf.lines() {
                    self.emit_inline(l)?;
                }
            }
            Some(_) => self.flush_block()?,
            None => {}
        }
        self.out.flush()
    }

    fn consume_line(&mut self, line: String) -> io::Result<()> {
        // `line` may need re-processing in a fresh state after a block flushes
        // (a non-matching line both *ends* a block and *starts* whatever's next).
        let mut pending = Some(line);
        while let Some(line) = pending.take() {
            match self.hold.clone() {
                Some(Hold::Fence(marker)) => {
                    self.append_block(&line);
                    if closes_fence(&line, &marker) {
                        self.flush_block()?;
                    }
                }
                Some(Hold::Quote) => {
                    if is_quote(&line) {
                        self.append_block(&line);
                    } else {
                        self.flush_block()?;
                        pending = Some(line);
                    }
                }
                Some(Hold::List) => {
                    if is_blank(&line) {
                        self.flush_block()?;
                        self.out.write_all(b"\n")?;
                    } else if is_list(&line) || is_indented(&line) {
                        self.append_block(&line);
                    } else {
                        self.flush_block()?;
                        pending = Some(line);
                    }
                }
                Some(Hold::Table { confirmed: false }) => {
                    if is_delimiter_row(&line) {
                        self.append_block(&line);
                        self.hold = Some(Hold::Table { confirmed: true });
                    } else {
                        // Not a table after all — emit the opener as inline.
                        let buf = std::mem::take(&mut self.block_buf);
                        self.hold = None;
                        for l in buf.lines() {
                            self.emit_inline(l)?;
                        }
                        pending = Some(line);
                    }
                }
                Some(Hold::Table { confirmed: true }) => {
                    if has_pipe(&line) && !is_blank(&line) {
                        self.append_block(&line);
                    } else {
                        self.flush_block()?;
                        pending = Some(line);
                    }
                }
                None => self.classify(line)?,
            }
        }
        Ok(())
    }

    /// Classify a line with no block open: start a hold, or emit it inline.
    fn classify(&mut self, line: String) -> io::Result<()> {
        if is_blank(&line) {
            self.out.write_all(b"\n")?;
        } else if let Some(marker) = fence_marker(&line) {
            self.start_hold(Hold::Fence(marker), &line);
        } else if is_quote(&line) {
            self.start_hold(Hold::Quote, &line);
        } else if is_list(&line) {
            self.start_hold(Hold::List, &line);
        } else if has_pipe(&line) {
            self.start_hold(Hold::Table { confirmed: false }, &line);
        } else {
            self.emit_inline(&line)?;
        }
        Ok(())
    }

    fn start_hold(&mut self, kind: Hold, line: &str) {
        self.hold = Some(kind);
        self.block_buf.clear();
        self.append_block(line);
    }

    fn append_block(&mut self, line: &str) {
        self.block_buf.push_str(line);
        self.block_buf.push('\n');
    }

    /// Render and emit the held block, then clear the hold.
    fn flush_block(&mut self) -> io::Result<()> {
        let block = std::mem::take(&mut self.block_buf);
        self.hold = None;
        let rendered = render_markdown(
            &block,
            RenderOpts {
                color: true,
                cols: self.cols,
            },
        );
        self.out.write_all(rendered.as_bytes())?;
        self.out.write_all(b"\n")
    }

    /// Render and emit one inline (single-line) construct.
    fn emit_inline(&mut self, line: &str) -> io::Result<()> {
        let rendered = render_markdown(
            line,
            RenderOpts {
                color: true,
                cols: self.cols,
            },
        );
        self.out.write_all(rendered.as_bytes())?;
        self.out.write_all(b"\n")
    }
}

// ---- line classifiers ----

fn is_blank(line: &str) -> bool {
    line.trim().is_empty()
}

fn is_quote(line: &str) -> bool {
    line.trim_start().starts_with('>')
}

fn is_indented(line: &str) -> bool {
    line.starts_with(' ') || line.starts_with('\t')
}

fn has_pipe(line: &str) -> bool {
    line.contains('|')
}

/// A bullet (`- * +`) or ordered (`1.` / `1)`) list item.
fn is_list(line: &str) -> bool {
    let t = line.trim_start();
    if let Some(rest) = t.strip_prefix(['-', '*', '+']) {
        return rest.is_empty() || rest.starts_with(' ');
    }
    let digits: String = t.chars().take_while(char::is_ascii_digit).collect();
    if !digits.is_empty() {
        if let Some(rest) = t[digits.len()..].strip_prefix(['.', ')']) {
            return rest.is_empty() || rest.starts_with(' ');
        }
    }
    false
}

/// The opening run of a code fence (``` or ~~~, length ≥ 3), if any.
fn fence_marker(line: &str) -> Option<String> {
    let t = line.trim_start();
    for ch in ['`', '~'] {
        let run: String = t.chars().take_while(|&c| c == ch).collect();
        if run.len() >= 3 {
            return Some(run);
        }
    }
    None
}

/// Whether `line` closes a fence opened with `marker`.
fn closes_fence(line: &str, marker: &str) -> bool {
    let t = line.trim();
    let ch = match marker.chars().next() {
        Some(c) => c,
        None => return false,
    };
    let run = t.chars().take_while(|&c| c == ch).count();
    run >= marker.len() && !t.is_empty() && t.chars().all(|c| c == ch)
}

/// A GFM table delimiter row: only `| - : space`, with at least one `-`.
fn is_delimiter_row(line: &str) -> bool {
    let t = line.trim();
    t.contains('-') && t.chars().all(|c| matches!(c, '|' | '-' | ':' | ' '))
}

#[cfg(test)]
mod tests {
    use super::super::RenderOpts;
    use super::MarkdownStreamWriter;

    /// Stream `src` through the writer in `chunk`-sized byte slices.
    fn stream(src: &str, chunk: usize, color: bool, cols: usize) -> String {
        let mut buf: Vec<u8> = Vec::new();
        {
            let mut w = MarkdownStreamWriter::new(&mut buf, RenderOpts { color, cols });
            let bytes = src.as_bytes();
            let mut i = 0;
            while i < bytes.len() {
                let end = (i + chunk).min(bytes.len());
                // Respect UTF-8 boundaries.
                let mut e = end;
                while e < bytes.len() && (bytes[e] & 0xC0) == 0x80 {
                    e += 1;
                }
                w.push(std::str::from_utf8(&bytes[i..e]).unwrap()).unwrap();
                i = e;
            }
            w.finish().unwrap();
        }
        String::from_utf8(buf).unwrap()
    }

    const BOLD: &str = "\x1b[1m";
    const RESET: &str = "\x1b[0m";
    const BODY: &str = "\x1b[38;5;7m";
    const CODE: &str = "\x1b[38;5;15m";

    #[test]
    fn inline_line_renders_per_line() {
        // Whatever the chunking, a completed inline line renders identically.
        for chunk in [1, 2, 3, 100] {
            assert_eq!(
                stream("**bold** x\n", chunk, true, 80),
                format!("{BOLD}{CODE}bold{RESET}{BODY} x{RESET}\n")
            );
        }
    }

    #[test]
    fn marker_split_across_chunks_still_bolds() {
        // The classic failure: `**` split over two tokens reunites in line_buf.
        let mut buf = Vec::new();
        {
            let mut w = MarkdownStreamWriter::new(
                &mut buf,
                RenderOpts {
                    color: true,
                    cols: 80,
                },
            );
            for d in ["a **bo", "ld** b\n"] {
                w.push(d).unwrap();
            }
            w.finish().unwrap();
        }
        assert_eq!(
            String::from_utf8(buf).unwrap(),
            format!("{BODY}a{RESET}{BOLD}{CODE} bold{RESET}{BODY} b{RESET}\n")
        );
    }

    /// The code line exactly as a whole-document render styles it. These tests
    /// pin buffering, not color: fenced code is dim by default and
    /// syntax-highlighted under `markdown-syntect`, which rich-tui enables.
    fn rendered_code_line() -> String {
        super::super::render_markdown(
            "```\nlet x = 1;\n```",
            RenderOpts {
                color: true,
                cols: 80,
            },
        )
    }

    #[test]
    fn fenced_block_held_until_close() {
        let out = stream("```\nlet x = 1;\n```\nafter\n", 1, true, 80);
        assert_eq!(
            out,
            format!("{}\n{BODY}after{RESET}\n", rendered_code_line())
        );
    }

    #[test]
    fn unterminated_fence_flushes_at_finish() {
        // No dropped content even if the closing fence never arrives.
        let out = stream("```\nlet x = 1;\n", 1, true, 80);
        assert_eq!(out, format!("{}\n", rendered_code_line()));
    }

    #[test]
    fn table_streamed_matches_whole_render() {
        let src = "| a | b |\n|---|---|\n| 1 | 2 |\n";
        let whole = super::super::render_markdown(
            src.trim_end(),
            RenderOpts {
                color: true,
                cols: 80,
            },
        );
        // Streamed output is the whole-render plus the writer's trailing newline.
        assert_eq!(stream(src, 1, true, 80), format!("{whole}\n"));
    }

    #[test]
    fn pipe_paragraph_without_delimiter_is_not_a_table() {
        // `a | b` with no delimiter row must render as inline text, not a table.
        let out = stream("a | b\n", 1, true, 80);
        assert!(
            !out.contains('┌'),
            "no box for a non-table pipe line: {out:?}"
        );
        assert!(
            out.contains("a | b") || out.contains("a |"),
            "renders inline: {out:?}"
        );
    }

    #[test]
    fn color_off_is_raw_passthrough_for_any_chunking() {
        let src = "**x**\n| a | b |\n```\ncode\n```\n> q\n";
        for chunk in [1, 4, 1000] {
            assert_eq!(stream(src, chunk, false, 80), src);
        }
    }
}