Skip to content

Commit 306f11f

Browse files
author
Yogthos
committed
fix(ui): route reasoning through buffered render path to fix streaming staircase
Closes dirge-ypg (CRLF fix in 6172c4e didn't take effect — user reproduced staircase post-fix; deeper root cause). The Reasoning stream was using `renderer.write()` to paint chunks inline via per-segment `MoveTo(indent + self.col, r)` calls. Under the current LLM streaming cadence — DeepSeek-V4-pro reasoning chunks arriving rapidly — that path produces a staircase: each chunk on a new row, offset by the previous chunk's end-column. Couldn't isolate the exact failure mode from static analysis (MoveTo should reset col, CRLF emit should defeat raw-mode LF-without-CR, neither helped). Refactor: route reasoning through the same buffered render path the content stream uses. Add `reasoning_buf: String` + `reasoning_start_line: Option<usize>` mirrors of the existing `response_buf`/`response_start_line` pair. On each Reasoning event: 1. Append text to `reasoning_buf` (sanitized) 2. `wrap::soft_wrap` to line entries with DarkMagenta color 3. Prepend `<dirge> ` to the first entry 4. `renderer.replace_from(reasoning_start_line, styled)` — replaces the previously-rendered reasoning lines with the updated set 5. `renderer.render_viewport()` — paints buffer rows via per-row `MoveTo(0, i)` — explicitly anchored, no col drift possible The Token handler already uses this pattern (user confirmed content text doesn't staircase). Reasoning now inherits the same guarantee. Clear sites: `reasoning_buf` + `reasoning_start_line` are reset alongside `response_buf` + `response_start_line` at the 6 existing transition points (Token-after-Reasoning, ToolCall, Done, Interjected, ContextOverflow, Error). The CRLF fix in 6172c4e stays — it's defensive correct for raw mode and benefits any other code path using `write`/`write_line` streaming (e.g., chamber rendering's `write_line` for borders). Tests: 1214/1215 pass (1 pre-existing Clojure failure). Binary at ~/bin/dirge.
1 parent 3d26737 commit 306f11f

1 file changed

Lines changed: 80 additions & 5 deletions

File tree

src/ui/mod.rs

Lines changed: 80 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,21 @@ pub async fn run_interactive(
685685
#[cfg(feature = "plugin")]
686686
let mut current_turn_index: u32 = 0;
687687
let mut response_start_line: Option<usize> = None;
688+
// dirge-ypg: reasoning text buffer + buffer-position anchor.
689+
// Mirrors the Token handler's `response_buf`/`response_start_line`
690+
// pair so reasoning streams render via the same buffered
691+
// `replace_from + render_viewport` path the content stream uses.
692+
//
693+
// Previously reasoning used the inline `renderer.write()` path
694+
// which paints per-chunk directly to stdout via per-segment
695+
// `MoveTo`. Under certain conditions that path produces a
696+
// staircase pattern (each chunk on a new row, offset by the
697+
// previous chunk's end-column) — user-confirmed regression with
698+
// current LLM streaming behavior. Buffered rendering paints
699+
// every row at col=indent via `render_viewport`'s explicit per-
700+
// row `MoveTo(0, i)`, so the issue can't manifest.
701+
let mut reasoning_buf = String::new();
702+
let mut reasoning_start_line: Option<usize> = None;
688703
let mut show_reasoning = true;
689704
let mut was_reasoning = false;
690705
let mut todo_tools_enabled = false;
@@ -2051,12 +2066,52 @@ pub async fn run_interactive(
20512066
if !show_reasoning {
20522067
continue;
20532068
}
2054-
if !agent_line_started {
2055-
renderer.write("<dirge> ", Color::DarkMagenta)?;
2056-
agent_line_started = true;
2057-
}
2069+
// dirge-ypg: route reasoning through the same
2070+
// buffered path the Token handler uses.
2071+
// Inline `renderer.write` was staircasing under
2072+
// current LLM stream cadence; `replace_from +
2073+
// render_viewport` paints every row at col=indent
2074+
// via explicit `MoveTo(0, row)`, sidestepping
2075+
// the raw-mode CR-less LF risk entirely.
20582076
let safe = sanitize_output(&text);
2059-
renderer.write(&safe, Color::DarkMagenta)?;
2077+
reasoning_buf.push_str(&safe);
2078+
2079+
if reasoning_buf.is_empty() {
2080+
was_reasoning = true;
2081+
continue;
2082+
}
2083+
2084+
let max_width = renderer.content_width().saturating_sub(9);
2085+
// Reasoning is plain text (no markdown — pi
2086+
// historically renders it as a single
2087+
// continuous paragraph with soft wrap). Use
2088+
// `wrap::soft_wrap` directly to avoid the
2089+
// markdown-parser overhead per chunk.
2090+
let wrapped =
2091+
crate::ui::wrap::soft_wrap(&reasoning_buf, max_width, "");
2092+
let mut styled: Vec<LineEntry> = wrapped
2093+
.into_iter()
2094+
.map(|t| LineEntry {
2095+
text: CompactString::from(t),
2096+
color: Color::DarkMagenta,
2097+
})
2098+
.collect();
2099+
if !styled.is_empty() {
2100+
styled[0].text = CompactString::from(format!(
2101+
"<dirge> {}",
2102+
styled[0].text
2103+
));
2104+
}
2105+
2106+
if let Some(start) = reasoning_start_line {
2107+
renderer.replace_from(start, styled);
2108+
} else {
2109+
let start = renderer.buffer_len();
2110+
reasoning_start_line = Some(start);
2111+
renderer.replace_from(start, styled);
2112+
}
2113+
renderer.render_viewport()?;
2114+
agent_line_started = true;
20602115
was_reasoning = true;
20612116
}
20622117
AgentEvent::Token(text) => {
@@ -2067,6 +2122,16 @@ pub async fn run_interactive(
20672122
was_reasoning = false;
20682123
response_buf.clear();
20692124
response_start_line = None;
2125+
// dirge-ypg: end-of-reasoning marker. Keep
2126+
// the reasoning rendered in the scroll
2127+
// (already committed to buffer via the
2128+
// Reasoning handler's render_viewport
2129+
// pushes); just stop tracking it so the
2130+
// next reasoning burst (if any) anchors
2131+
// at a fresh buffer position below the
2132+
// content that's about to stream.
2133+
reasoning_buf.clear();
2134+
reasoning_start_line = None;
20702135
}
20712136
let safe = sanitize_output(&text);
20722137
response_buf.push_str(&safe);
@@ -2165,6 +2230,8 @@ pub async fn run_interactive(
21652230
}
21662231
response_buf.clear();
21672232
response_start_line = None;
2233+
reasoning_buf.clear();
2234+
reasoning_start_line = None;
21682235
// Tool-call line: rounded chamber TOP border
21692236
// with the tool name on it. Output lines below
21702237
// get `│ ` chamber rows; the chamber is closed
@@ -2689,6 +2756,8 @@ pub async fn run_interactive(
26892756
agent_line_started = false;
26902757
response_buf.clear();
26912758
response_start_line = None;
2759+
reasoning_buf.clear();
2760+
reasoning_start_line = None;
26922761

26932762
#[cfg(feature = "loop")]
26942763
let loop_running = loop_state.as_ref().is_some_and(|ls| ls.active);
@@ -3010,6 +3079,8 @@ pub async fn run_interactive(
30103079
agent_line_started = false;
30113080
response_buf.clear();
30123081
response_start_line = None;
3082+
reasoning_buf.clear();
3083+
reasoning_start_line = None;
30133084

30143085
if !cli.no_session
30153086
&& let Err(e) = crate::session::storage::save_session(session)
@@ -3076,6 +3147,8 @@ pub async fn run_interactive(
30763147
agent_line_started = false;
30773148
response_buf.clear();
30783149
response_start_line = None;
3150+
reasoning_buf.clear();
3151+
reasoning_start_line = None;
30793152

30803153
renderer.write_line(
30813154
"▒░ auto-compacting then retrying ░▒",
@@ -3264,6 +3337,8 @@ pub async fn run_interactive(
32643337
agent_line_started = false;
32653338
response_buf.clear();
32663339
response_start_line = None;
3340+
reasoning_buf.clear();
3341+
reasoning_start_line = None;
32673342

32683343
// Drop queued interjections — they were typed expecting
32693344
// the running turn to succeed; replaying them blindly

0 commit comments

Comments
 (0)