From 371027a85f44470880b1436d59fa97774e9c8642 Mon Sep 17 00:00:00 2001 From: salmonik Date: Sun, 15 Nov 2026 17:43:11 +0100 Subject: 0.6.1: redraw from the caret row, not the block height A two-line prompt with the caret moved up made the next redraw overshoot by one row and clear the line above, erasing the model's output. The editor now tracks the caret's row. Adds scripts/ux-check.sh, which drives tmux and asserts the rendered screen: byte-level tests cannot see misplaced cursors or erased rows. --- CHANGELOG.md | 11 +++++++ Cargo.lock | 2 +- Cargo.toml | 2 +- scripts/ux-check.sh | 86 +++++++++++++++++++++++++++++++++++++++++++++++++ src/line.rs | 93 +++++++++++++++++++++++++++++++++++++++++++++-------- 5 files changed, 179 insertions(+), 15 deletions(-) create mode 100755 scripts/ux-check.sh diff --git a/CHANGELOG.md b/CHANGELOG.md index 1fd94b9..d33608d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,17 @@ break the interface, **PATCH** for bug fixes. file, and the tool schemas the model sees. Anything a script or a resumed session could depend on counts as API. +## 0.6.1 + +### Fixed +- The redraw moved up by the number of rows the prompt block occupied, but the + caret can be on any of those rows. With a two-line prompt and the caret moved + up (Home, Left, Up-history), the next redraw overshot by a row and cleared the + line above the prompt — usually the model's previous output. The editor now + tracks which row the caret is on and moves up by that. +- Added `scripts/ux-check.sh`: drives the editor inside tmux and asserts on the + rendered screen. Byte-level tests could not see this class of bug. + ## 0.6.0 ### Added diff --git a/Cargo.lock b/Cargo.lock index 803a619..8b92fd5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -10,7 +10,7 @@ checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa" [[package]] name = "agent686" -version = "0.6.0" +version = "0.6.1" dependencies = [ "crossterm", "libc", diff --git a/Cargo.toml b/Cargo.toml index e82f469..147541b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "agent686" -version = "0.6.0" +version = "0.6.1" edition = "2021" description = "A small coding agent for a 32-bit Debian box" publish = false diff --git a/scripts/ux-check.sh b/scripts/ux-check.sh new file mode 100755 index 0000000..019f63d --- /dev/null +++ b/scripts/ux-check.sh @@ -0,0 +1,86 @@ +#!/usr/bin/env bash +# Drives the editor on a real terminal (tmux) and checks the things byte-level +# tests cannot see: that a multi-line prompt is drawn where it belongs, and that +# moving the caret inside it does not erase the output above. +set -uo pipefail + +here=$(cd "$(dirname "$0")/.." && pwd) +port=${PORT:-8791} +session=agent686-ux +work=$(mktemp -d) +mock_pid= +fail=0 + +cleanup() { + tmux kill-session -t "$session" 2>/dev/null + [ -n "$mock_pid" ] && kill "$mock_pid" 2>/dev/null + rm -rf "$work" +} +trap cleanup EXIT + +fail_msg() { + echo "FAIL: $*" >&2 + fail=1 +} + +python3 "$here/scripts/mock-provider.py" --port "$port" --quiet --delay 0 >/dev/null 2>&1 & +mock_pid=$! +sleep 0.6 + +tmux kill-session -t "$session" 2>/dev/null +tmux new-session -d -s "$session" -x 80 -y 24 +tmux send-keys -t "$session" \ + "clear; cd $work && AGENT686_DATA=$work/.data OPENCODE_API_KEY=test \ + $here/target/debug/agent686 --yolo --base-url http://127.0.0.1:$port/v1" Enter +sleep 1.5 + +# One turn so there is output above the prompt that must survive. +tmux send-keys -t "$session" "hi" Enter +sleep 1.2 +screen=$(tmux capture-pane -p -t "$session") +echo "$screen" | grep -q "Hello from the mock provider" || fail_msg "the turn produced no answer" +grep_line=$(echo "$screen" | grep -n "Hello from the mock provider" | head -1 | cut -d: -f1) + +# Two-line prompt, no trailing newline. +printf 'line one\nline two' | tmux load-buffer -b uxcheck - +tmux paste-buffer -p -b uxcheck -t "$session" +sleep 0.5 +paste_y=$(tmux display-message -p -t "$session" '#{cursor_y}') + +tmux send-keys -t "$session" Home +sleep 0.4 +home_y=$(tmux display-message -p -t "$session" '#{cursor_y}') +[ "$home_y" -eq $((paste_y - 1)) ] || fail_msg "Home put the caret on row $home_y, expected $((paste_y - 1))" + +tmux send-keys -t "$session" Right +sleep 0.4 +right_y=$(tmux display-message -p -t "$session" '#{cursor_y}') +[ "$right_y" -eq "$home_y" ] || fail_msg "Right moved the caret to row $right_y, expected $home_y" + +screen=$(tmux capture-pane -p -t "$session") +if ! echo "$screen" | grep -q "Hello from the mock provider"; then + fail_msg "moving the caret inside a two-line prompt erased the output above it" +fi +if ! echo "$screen" | grep -q "line two"; then + fail_msg "the second prompt line disappeared" +fi +if ! echo "$screen" | grep -q "^> line one"; then + fail_msg "the first prompt line is not drawn with its prompt" +fi + +# Submitting must not duplicate the block or leave the answer mid-line. +tmux send-keys -t "$session" Enter +sleep 1.0 +screen=$(tmux capture-pane -p -t "$session") +[ "$(echo "$screen" | grep -c '^line two$')" -eq 1 ] \ + || fail_msg "the submitted prompt should appear exactly once" +if echo "$screen" | grep -q 'line two.*Hello from the mock'; then + fail_msg "the answer was written on the same line as the prompt" +fi + +if [ "$fail" -ne 0 ]; then + echo "--- final screen ---" >&2 + echo "$screen" >&2 + exit 1 +fi +echo "ux-check: ok (two-line prompt, caret movement, output above intact)" diff --git a/src/line.rs b/src/line.rs index 1ae210a..613dffe 100644 --- a/src/line.rs +++ b/src/line.rs @@ -43,27 +43,27 @@ impl Editor { let guard = RawGuard::enable()?; let mut buffer: Vec = Vec::new(); let mut cursor = 0usize; - let mut rows = 1usize; + let mut state = DrawState::default(); self.history_index = self.history.len(); self.draft.clear(); - draw(prompt, &buffer, cursor, &mut rows)?; + draw(prompt, &buffer, cursor, &mut state)?; loop { let event = event::read()?; match event { Event::Paste(text) => { insert(&mut buffer, &mut cursor, &text); - draw(prompt, &buffer, cursor, &mut rows)?; + draw(prompt, &buffer, cursor, &mut state)?; } Event::Resize(_, _) => { // The terminal reflowed the block we were tracking, so the // old row count is meaningless. Start a fresh prompt line // rather than risk erasing output above it. println!(); - rows = 1; - draw(prompt, &buffer, cursor, &mut rows)?; + state = DrawState::default(); + draw(prompt, &buffer, cursor, &mut state)?; } Event::Key(key) if key.kind == KeyEventKind::Press => { match handle_key(key, &mut buffer, &mut cursor, self) { @@ -71,7 +71,7 @@ impl Editor { Action::Submit => { // Redraw with the caret at the end so the submitted // line looks complete before we move on. - draw(prompt, &buffer, buffer.len(), &mut rows)?; + draw(prompt, &buffer, buffer.len(), &mut state)?; drop(guard); let line: String = buffer.iter().collect(); self.remember(&line); @@ -89,7 +89,7 @@ impl Editor { return Ok(Some(String::new())); } } - draw(prompt, &buffer, cursor, &mut rows)?; + draw(prompt, &buffer, cursor, &mut state)?; } _ => {} } @@ -303,15 +303,40 @@ fn cells(ch: char, col: usize) -> usize { unicode_width::UnicodeWidthChar::width(ch).unwrap_or(1) } -fn draw(prompt: &str, buffer: &[char], cursor: usize, rows: &mut usize) -> io::Result<()> { - let width = terminal_width(); +/// Where the caret physically is, relative to the first row of the input block. +/// The previous draw can leave the caret on any row, so a redraw must move up by +/// that row, not by the number of rows the block occupies. +#[derive(Default, Clone, Copy)] +struct DrawState { + rows: usize, + cursor_row: usize, +} + +fn draw( + prompt: &str, + buffer: &[char], + cursor: usize, + state: &mut DrawState, +) -> io::Result<()> { let mut out = io::stdout(); + draw_to(&mut out, prompt, buffer, cursor, state)?; + out.flush() +} + +fn draw_to( + out: &mut W, + prompt: &str, + buffer: &[char], + cursor: usize, + state: &mut DrawState, +) -> io::Result<()> { + let width = terminal_width(); let (end_row, _) = layout(prompt, buffer, buffer.len(), width); let (cursor_row, cursor_col) = layout(prompt, buffer, cursor, width); - if *rows > 1 { - execute!(out, MoveUp(clamp(*rows - 1)))?; + if state.cursor_row > 0 { + execute!(out, MoveUp(clamp(state.cursor_row)))?; } execute!(out, MoveToColumn(0), Clear(ClearType::FromCursorDown))?; @@ -330,9 +355,9 @@ fn draw(prompt: &str, buffer: &[char], cursor: usize, rows: &mut usize) -> io::R execute!(out, MoveUp(clamp(end_row - cursor_row)))?; } execute!(out, MoveToColumn(clamp(cursor_col)))?; - out.flush()?; - *rows = end_row + 1; + state.rows = end_row + 1; + state.cursor_row = cursor_row; Ok(()) } @@ -433,6 +458,48 @@ mod tests { assert!(terminal_width() >= 1); } + fn drawn(prompt: &str, buffer: &str, cursor: usize, state: &mut DrawState) -> String { + let chars: Vec = buffer.chars().collect(); + let mut out = Vec::new(); + draw_to(&mut out, prompt, &chars, cursor, state).unwrap(); + String::from_utf8(out).unwrap() + } + + #[test] + fn redraw_moves_up_by_the_caret_row_not_the_block_height() { + let mut state = DrawState::default(); + + // First draw with the caret at the end of a two-line buffer. + let first = drawn("> ", "one\ntwo", 7, &mut state); + assert_eq!(state.cursor_row, 1, "caret ends on the second row"); + assert!(!first.contains("\u{1b}[1A"), "first draw must not move up"); + + // Move the caret to the first row. The previous caret was on row 1, so + // the redraw may move up exactly one row — not `rows - 1` from row 0, + // which would land on the line above the prompt and erase it. + let second = drawn("> ", "one\ntwo", 0, &mut state); + assert!( + second.starts_with("\u{1b}[1A"), + "expected one MoveUp, got {:?}", + second + ); + assert_eq!(state.cursor_row, 0); + + // Drawing again with the caret already on row 0 must not move up + // *before* clearing; the only MoveUp is the one after writing, which + // puts the caret back on the first row. + let third = drawn("> ", "one\ntwo", 0, &mut state); + assert!( + !third.starts_with("\u{1b}[1A"), + "must not move up before clearing: {third:?}" + ); + assert_eq!( + third.matches("\u{1b}[1A").count(), + 1, + "exactly one repositioning move after writing: {third:?}" + ); + } + #[test] fn layout_counts_wide_and_combining_characters() { let wide: Vec = "日本語".chars().collect(); -- cgit v1.2.3