Skip to content

fix(director): Keep a reply's line breaks out of the bubble's ceiling - #702

Merged
omesser merged 2 commits into
mainfrom
fix/701-reply-line-breaks
Sep 14, 2026
Merged

omesser merged 2 commits into
mainfrom
fix/701-reply-line-breaks

Conversation

@omesser

@omesser omesser commented Sep 14, 2026

Copy link
Copy Markdown
Owner

Why

A reply that obeys the Behavior contract had every line break destroyed before any surface saw it — in crates/core, by the parser that exists to find the Behavior name.

parse_proposal normalised lines to find the contract line, then rebuilt the dialogue from that same normalised list. Three losses in one expression: str::trim took the indentation, filter took the blank lines, join(" ") took the newlines. So

wave
Here's what I found:

- the roster loads
- the session resumed

reached every surface as Here's what I found: - the roster loads - the session resumed.

Two things this contradicted:

  • src/bubble.js is newline-aware and never saw a newline. wrapText wraps paragraph by paragraph over text.split("\n"); on this path the split always yielded one element.
  • The same model got two different answers. as_speech keeps the text whole, so a reply that ignored the format contract kept its formatting while one that obeyed it was flattened. The obedient reply was the mangled one.

It also blocks #677: a Markdown renderer handed a space-joined line cannot recover lists, paragraphs or fences.

What changed

The normalised list is gone rather than mapped. The issue proposed finding the index over the filtered list and mapping it back to the originals. That mapping is the obvious place for an off-by-one, so instead at now indexes reply.lines() directly and no mapping exists to get wrong:

let lines: Vec<&str> = reply.lines().collect();
let (at, (name, inline)) = lines
    .iter()
    .enumerate()
    .find_map(|(at, line)| contract_line(line.trim()).map(|found| (at, found)))
    .ok_or(ParseError)?;

Two facts make dropping the filter safe: contract_line needs the line trimmed, and it already rejects an empty line — identifier requires an alphanumeric — so the old .filter(|l| !l.is_empty()) only ever shortened the scan and could never change which line matched. Filtering preserves relative order, so the first match in the filtered list was always the first match in the original.

Dialogue is then joined with "\n" between the first and last non-blank lines. Blank lines inside the model's text survive as its paragraph breaks; blank padding at the ends does not.

The signature is untouched, so harness probe is unaffected, and the behavior-name parse is byte-identical.

The bubble now sees newlines, and that is deliberate

src/bubble.js is unchanged. It keeps the line breaks it now receives, so truncated goes true more often and the #547 control into Chat appears more frequently.

Collapsing them in bubble.js was considered and rejected: it would render a run-on line that reads worse than a clamped list, and it would hide the overflow instead of handing it off. The six-line clamp plus the control into Chat is the designed answer to content the bubble cannot hold (ADR-0019; the clamp matches the 8s reading limit). Adding a collapse would be a second, quieter ceiling on top of MAX_LINES — which is the mistake this PR is about, moved one file over.

A blank line costs nothing: an empty paragraph produces no line, so Here's:\n\n- one\n- two is three lines, not four.

Two tests/bubble.test.js tests pin the change rather than leaving it to be discovered — one asserting the three-line shape with truncated === false, one asserting the same reply fits when flattened and truncates when structured.

How to verify

cargo test --workspace
cargo clippy --workspace --all-targets -- -D warnings
node --test tests/

425 + 483 + 23 Rust tests pass, 126 node tests pass, clippy clean.

Differential check against the old implementation. The base parse_proposal was copied verbatim and run against the new one over 37 inputs. Behavior-name differences: none. The awkward cases:

input old dialogue new dialogue
"\n\n\nwave\nHello" leading blanks "Hello" "Hello"
" wave\nHello" indented name "Hello" "Hello"
"Hello there\nwave" name last "Hello there" "Hello there"
"wave\nA\n\n\n\nB" consecutive blanks "A B" "A\n\n\n\nB"
"wave" contract line only None None

Mutation results, each restored from a scratch copy:

mutation result
parser reverted to the base shape 2 fail — a_reply_keeps_its_own_line_structure, the_contract_line_is_cut_from_the_middle
end-trimming dropped 1 fail — blank_lines_at_the_ends_are_padding_not_speech
bubble.js collapses newlines 4 fail — both new tests plus two pre-existing ones

That last row is worth reading twice: text truncates with ellipsis past 6 lines and exactly six lines is not truncation have been asserting newline behaviour on input the production path could never produce. They were dormant coverage; they are now live.

The three tests the issue flagged as canaries — director.rs:1305, :1620, :1643 — are unchanged and pass as written. The banner test still sees starts_with("pi v0.85.1") and ends_with("Hello!"), now with the banner's own lines intact between them.

Notes

  • Blank padding is trimmed at the extremes only. A contract line in the middle leaves its surrounding blanks in place, so two gaps merge into a wider one. Collapsing it would mean guessing which of the model's own breaks were intended, and one extra blank line is invisible in the bubble. The doc comment says what the code does, and a test pins the middle case.
  • as_speech parity is blank-line parity, not whitespace parity. as_speech trims horizontal whitespace at the ends; this does not, so "wave\n Hello " keeps its spaces and a leading tab survives. Arguably desirable — an indented list item keeps its indent — and harmless in the bubble, where wrapText swallows empty words. Chat renders via textContent, so a preserved four-space indent cannot turn a list into a code block.
  • No GUI run. The rendering claims are read off main.js, main.css and chat-ui.css, not observed.
  • fix(chat): The composer drops the newlines out of a pasted file #686 is the mirror bug on the input side: the composer is <input type="text">, which strips LF/CR per the HTML spec. Together they are why a multi-line block currently survives in neither direction.

Closes #701

— Claude Code agent, on @omesser's behalf.

🤖 Generated with Claude Code

omesser and others added 2 commits September 15, 2026 01:07
The parser normalises lines to find the Behavior name and rebuilt the
dialogue from that same list, costing Chat and the Action Log their
paragraphs. The bubble keeps the breaks it now receives.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Only the ones at the ends. Padding either side of a name cut from the
middle survives as one wider gap; an assertion now pins that.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@omesser
omesser merged commit 0d09fbd into main Sep 14, 2026
5 checks passed
@omesser
omesser deleted the fix/701-reply-line-breaks branch September 14, 2026 23:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(director): A reply's line breaks are flattened before Chat or the bubble sees them

1 participant