Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

fix(agent-tui): bound the live reasoning tail in rendered rows

Akarshan Biswas36a8fe7a85ae
diff --git a/src-tauri/src/core/cli/tui.rs b/src-tauri/src/core/cli/tui.rs
index 205621af6..cb81e8a5a 100644
--- a/src-tauri/src/core/cli/tui.rs
+++ b/src-tauri/src/core/cli/tui.rs
@@ -13375,6 +13375,32 @@ mod tests {
assert!(text[5].contains("step 20"), "ends at the newest: {text:?}");
}
+ /// The tail is bounded in *rendered* rows, not source lines. Reasoning
+ /// usually arrives as one long paragraph, so a newline-counted cap let a
+ /// single line wrap into an unpredictable number of screen rows and the
+ /// block's height jittered between frames as that line grew.
+ #[test]
+ fn live_tail_height_is_stable_as_one_long_line_grows() {
+ let width = 40u16;
+ let words: Vec<String> = (1..=120).map(|n| format!("word{n}")).collect();
+ let mut seen = 0u16;
+ for n in (1..=words.len()).step_by(3) {
+ let body = words[..n].join(" ");
+ let lines =
+ super::live_assistant_lines(&format!("<think>{body}"), &[], width, true, true);
+ let rows = lines.len() as u16;
+ assert_eq!(
+ rows,
+ super::wrapped_height(lines.clone(), width),
+ "a pre-wrapped tail must not re-wrap ({n} words)"
+ );
+ assert!(rows <= 6, "tail grew to {rows} rows ({n} words)");
+ assert!(rows >= seen, "tail shrank from {seen} to {rows} rows ({n} words)");
+ seen = rows;
+ }
+ assert_eq!(seen, 6, "tail never filled to its cap");
+ }
+
/// A block that already closed mid-turn folds to the very summary row the
/// commit will emit, so finalizing the turn does not move the transcript.
#[test]
diff --git a/src-tauri/src/core/cli/tui/markdown.rs b/src-tauri/src/core/cli/tui/markdown.rs
index 38cee0be1..483b8aaea 100644
--- a/src-tauri/src/core/cli/tui/markdown.rs
+++ b/src-tauri/src/core/cli/tui/markdown.rs
@@ -62,8 +62,7 @@ pub(super) fn live_assistant_lines(
if i < last {
lines.push(reasoning_summary_row(body.len()));
} else if stream_reasoning {
- let tail = &body[body.len().saturating_sub(LIVE_REASONING_TAIL_LINES)..];
- lines.extend(reasoning_detail_lines(&tail.join("\n")));
+ lines.extend(live_reasoning_tail(&body, width));
}
continue;
}
@@ -74,16 +73,46 @@ pub(super) fn live_assistant_lines(
lines
}
+/// Columns the reasoning gutter takes. Subtracted from the width before the
+/// live tail wraps, so a pre-wrapped row still fits and the terminal re-wraps
+/// nothing.
+const REASONING_GUTTER_COLS: usize = 2;
+
+fn reasoning_row(body: Vec<Span<'static>>) -> Line<'static> {
+ let mut spans = vec![Span::styled("┊ ", Style::new().dark_gray())];
+ spans.extend(body);
+ Line::from(spans)
+}
+
+fn reasoning_body_span(text: String) -> Span<'static> {
+ Span::styled(text, Style::new().dim().italic())
+}
+
/// A reasoning block's full dimmed lines (`┊ ` gutter, dim italic body).
pub(super) fn reasoning_detail_lines(seg: &str) -> Vec<Line<'static>> {
seg.lines()
.filter(|l| !l.trim().is_empty())
- .map(|l| {
- Line::from(vec![
- Span::styled("┊ ", Style::new().dark_gray()),
- Span::styled(l.to_string(), Style::new().dim().italic()),
- ])
- })
+ .map(|l| reasoning_row(vec![reasoning_body_span(l.to_string())]))
+ .collect()
+}
+
+/// The open block's tail, bounded to [`LIVE_REASONING_TAIL_LINES`] *rendered*
+/// rows rather than source lines. Reasoning usually arrives as one long
+/// paragraph, so a source-line cap bounded nothing on screen: the block's
+/// height moved with however far the newest line had wrapped, jittering by a
+/// row or two as it grew. Wrapping here also gives every continuation its own
+/// gutter instead of letting it start under the margin.
+fn live_reasoning_tail(body: &[&str], width: u16) -> Vec<Line<'static>> {
+ let max = (width as usize)
+ .saturating_sub(REASONING_GUTTER_COLS)
+ .max(1);
+ let rows: Vec<Vec<Span<'static>>> = body
+ .iter()
+ .flat_map(|l| wrap_spans_at_words(vec![reasoning_body_span(l.to_string())], max))
+ .collect();
+ rows[rows.len().saturating_sub(LIVE_REASONING_TAIL_LINES)..]
+ .iter()
+ .map(|spans| reasoning_row(spans.clone()))
.collect()
}
@@ -94,10 +123,7 @@ pub(super) fn reasoning_summary_row(n: usize) -> Line<'static> {
} else {
format!("reasoning ({n} lines)")
};
- Line::from(vec![
- Span::styled("┊ ", Style::new().dark_gray()),
- Span::styled(label, Style::new().dim().italic()),
- ])
+ reasoning_row(vec![reasoning_body_span(label)])
}
/// Render answer prose to styled lines. Every block -- prose, code fences,