feat(md, cli): Stream top-level paragraphs incrementally#805
Merged
Conversation
Long assistant paragraphs — a single prose line with no internal newlines — previously stalled the terminal until the whole paragraph arrived, sometimes over a minute for a reasoning block. The buffer now streams a top-level paragraph incrementally as its content grows, so output appears as it is generated rather than only when the block terminates. The output is byte-for-byte identical to today's whole-paragraph rendering: the renderer accumulates the paragraph's source, re-renders the growing buffer on each chunk, and emits only the stable committed prefix (everything up to the last wrapped newline), holding the in-progress visual line until a later chunk or the terminator commits it. The concatenation of all printed deltas equals `format_terminal_with(full_paragraph, opts)` exactly. Four guards manage the known ambiguities: the partial-line classifier enters paragraph mode as soon as the line's opening token rules out every block starter (header, fence, list, HTML, thematic break, reference); a source-byte setext threshold holds the first ~128 bytes so a short setext underline can still turn a run into a heading; an inline ground-state scan stops the committed prefix before any open inline construct (emphasis, code span, link, angle bracket); and the renderer holds the in-progress visual line. The one accepted exception: a setext heading whose content exceeds the threshold has already begun streaming as prose and never becomes a heading — realistic short headings are unaffected. `Buffer::with_streaming_paragraphs(false)` opts out, restoring whole-paragraph `Event::Block` emission. `Event` gains `#[non_exhaustive]` alongside the new `Event::ParagraphChunk` variant so this is the last forced match-site breakage. `OrphanedFenceFixup` is updated to derive its embedded-fence flag from the accumulated paragraph chunks rather than a single `Event::Block`, since the inline scanner can split an embedded fence across chunk boundaries. The design and all four guards are documented in RFD 089. Signed-off-by: Jean Mertz <git@jeanmertz.com>
Signed-off-by: Jean Mertz <git@jeanmertz.com>
fcec6fc to
aaa9664
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Long assistant paragraphs — a single prose line with no internal newlines — previously stalled the terminal until the whole paragraph arrived, sometimes over a minute for a reasoning block. The buffer now streams a top-level paragraph incrementally as its content grows, so output appears as it is generated rather than only when the block terminates.
Streamed output is byte-for-byte identical to whole-paragraph rendering: the renderer accumulates the paragraph's source, re-renders the growing buffer on each chunk, and prints only the stable committed prefix (everything up to the last wrapped newline), holding the in-progress visual line until a later chunk or the terminator commits it. The concatenation of all printed deltas equals
format_terminal_with(full_paragraph, opts)exactly.Several guards manage the ambiguities that would otherwise break that guarantee:
#/`/|/ digit / etc. waits for the newline.Byte-identity has two documented exceptions, both almost never produced by LLM output: a setext heading whose content exceeds the threshold streams as prose instead of becoming a heading, and a GFM table whose header has no leading pipe (legal per the GFM spec but unexemplified there) is not detected and may stream with mis-padded columns.
Buffer::with_streaming_paragraphs(false)opts out, restoring whole-paragraphEvent::Blockemission.Eventgains#[non_exhaustive]alongside the newEvent::ParagraphChunkvariant, so this is the last forced match-site breakage.OrphanedFenceFixupderives its embedded-fence flag from the accumulated paragraph source rather than a singleEvent::Block, since the inline scanner can split an embedded fence across chunk boundaries.Tested by a byte-identity harness (streamed vs. whole, with per-shape latency assertions) over an adversarial corpus, plus an expanded set of flowing-markdown fixtures cross-validated against comrak; the buffer-half of the guarantee is checked over the fixture corpus in
jp_md.The design and guards are documented in RFD 089.