From 69588187fa2e9ebaae08c9b80be357a9daff6e4e Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 16 Nov 2026 16:53:18 +0000 Subject: [PATCH 1/4] fix(tui): keep the view in place when an older page is measured late ThreadView.show(prepended=True) scrolls past the added height in one call_after_refresh. On a loaded loop that refresh can run before the layout that measures the new rows, so the height had not grown, the scroll moved by 0, and the reader was left at the top of the longer thread. That is the remaining failure of test_older_rows_keep_the_view_where_it_was after pepper-cli#195 made it wait (merge-queue run 43496483442: "condition not reached" after 5s). keep_place now waits for up to five more refreshes while the height has not grown. Running keep_place before the layout (instead of after the refresh) fails the test on the old code and passes it with this change. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01ALQbQ2fKvgEPYLj9LMKnnH --- src/pepper_cli/tui/thread.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/pepper_cli/tui/thread.py b/src/pepper_cli/tui/thread.py index 9f39af4..8ec0c77 100644 --- a/src/pepper_cli/tui/thread.py +++ b/src/pepper_cli/tui/thread.py @@ -14,6 +14,9 @@ from textual.app import ComposeResult from textual.widget import Widget +#: Refreshes an older page's scroll-past waits through for the new rows to be measured. +_KEEP_PLACE_TRIES = 5 + class ThreadScroll(VerticalScroll): """The scrolling body. ↑/↓ select a message (for ``r``) instead of scrolling a line; @@ -142,8 +145,14 @@ def show( self.query_one("#thread-body", Static).update(body) if prepended and not reset: - def keep_place() -> None: + def keep_place(tries: int = _KEEP_PLACE_TRIES) -> None: grown = scroller.virtual_size.height - old_height + if grown <= 0 and tries > 1: + # The refresh this waited for can land before the layout that measures + # the new rows (a loaded loop); scrolling by 0 would leave the reader at + # the top of the longer thread. Wait for the next one. + scroller.call_after_refresh(keep_place, tries - 1) + return scroller.scroll_to(y=old_y + max(grown, 0), animate=False) scroller.call_after_refresh(keep_place) From fc42c78ffa018803272aed52f81b10f854ed5022 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 16 Nov 2026 17:01:46 +0000 Subject: [PATCH 2/4] Let a reader's scroll win over a pending keep-place retry Answers VR-6b3a8f9f on pepper-cli#273. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01ALQbQ2fKvgEPYLj9LMKnnH --- src/pepper_cli/tui/thread.py | 3 +++ tests/test_tui_parity.py | 26 ++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/src/pepper_cli/tui/thread.py b/src/pepper_cli/tui/thread.py index 8ec0c77..60e98b3 100644 --- a/src/pepper_cli/tui/thread.py +++ b/src/pepper_cli/tui/thread.py @@ -146,6 +146,9 @@ def show( if prepended and not reset: def keep_place(tries: int = _KEEP_PLACE_TRIES) -> None: + if scroller.scroll_y != old_y: + # The reader scrolled while this waited: their position wins. + return grown = scroller.virtual_size.height - old_height if grown <= 0 and tries > 1: # The refresh this waited for can land before the layout that measures diff --git a/tests/test_tui_parity.py b/tests/test_tui_parity.py index 5d90c1e..fcf4e92 100644 --- a/tests/test_tui_parity.py +++ b/tests/test_tui_parity.py @@ -329,6 +329,32 @@ async def test_older_rows_keep_the_view_where_it_was() -> None: await until(pilot, lambda: scroller.scroll_y > 0) # not left at the top +async def test_a_scroll_made_while_keep_place_waits_is_not_undone() -> None: + """VR-6b3a8f9f: the scroll past older rows waits a refresh or more; a reader who moves + in that window keeps the place they moved to.""" + server = ts.standard_server() + _a1( + server, + *( + ts.message(f"m{i:02d}", f"2026-09-16T10:{i:02d}:00+00:00", f"body {i}") + for i in range(14) + ), + ) + app = tui_app.PepperApp(server.client()) + async with app.run_test(size=(100, 14)) as pilot: + await _open_a1(app, pilot, "body 13") + thread = app.thread + scroller = thread.scroller + scroller.scroll_to(y=0, animate=False) + await pilot.pause() + longer = "\n".join(f"older {i}" for i in range(40)) + "\n" + body_text(app) + thread.show(longer, prepended=True) + scroller.scroll_y = 3 # the reader moves before keep_place runs + for _ in range(8): + await pilot.pause() + assert scroller.scroll_y == 3 + + async def test_o_opens_the_first_link_and_never_a_non_web_url() -> None: server = ts.standard_server() opened: list[str] = [] From 4d83377e8fe0cb18c750f05f73ccbae2a45c7c80 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 16 Nov 2026 17:30:31 +0000 Subject: [PATCH 3/4] Cancel a keep-place retry when another conversation opens; test the retry branch directly Answers Greptile's P1 and P2 on pepper-cli#273. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01ALQbQ2fKvgEPYLj9LMKnnH --- src/pepper_cli/tui/thread.py | 12 ++++- tests/test_tui_parity.py | 87 ++++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+), 2 deletions(-) diff --git a/src/pepper_cli/tui/thread.py b/src/pepper_cli/tui/thread.py index 60e98b3..ad3943d 100644 --- a/src/pepper_cli/tui/thread.py +++ b/src/pepper_cli/tui/thread.py @@ -105,6 +105,10 @@ def __init__(self) -> None: super().__init__(id="thread") self._unseen = 0 self._was_pinned = True + #: Bumped when a different conversation is shown, so a keep-place retry left over + #: from the previous one stands down. Not by every ``show``: a live refresh of the same + #: conversation must not cancel the scroll past an older page it has not reached yet. + self._conversation = 0 def compose(self) -> ComposeResult: with ThreadScroll(id="thread-scroll"): @@ -142,12 +146,16 @@ def show( stick = reset or (self.pinned and not prepended) scroller = self.scroller old_height, old_y = scroller.virtual_size.height, scroller.scroll_y + if reset: + self._conversation += 1 + conversation = self._conversation self.query_one("#thread-body", Static).update(body) if prepended and not reset: def keep_place(tries: int = _KEEP_PLACE_TRIES) -> None: - if scroller.scroll_y != old_y: - # The reader scrolled while this waited: their position wins. + if self._conversation != conversation or scroller.scroll_y != old_y: + # Another conversation was opened, or the reader scrolled, while this + # waited: either one owns the position now. return grown = scroller.virtual_size.height - old_height if grown <= 0 and tries > 1: diff --git a/tests/test_tui_parity.py b/tests/test_tui_parity.py index fcf4e92..4342569 100644 --- a/tests/test_tui_parity.py +++ b/tests/test_tui_parity.py @@ -3,11 +3,14 @@ from __future__ import annotations +import inspect import threading from datetime import UTC, datetime from typing import TYPE_CHECKING import pytest +from textual.geometry import Size +from textual.widget import Widget from pepper_cli.tui import app as tui_app from pepper_cli.tui import rows, state @@ -329,6 +332,90 @@ async def test_older_rows_keep_the_view_where_it_was() -> None: await until(pilot, lambda: scroller.scroll_y > 0) # not left at the top +def _older_thread_server() -> ts.FakeServer: + server = ts.standard_server() + _a1( + server, + *( + ts.message(f"m{i:02d}", f"2026-09-16T10:{i:02d}:00+00:00", f"body {i}") + for i in range(14) + ), + ) + return server + + +async def test_keep_place_retries_until_the_older_rows_are_measured(monkeypatch) -> None: + """The retry branch, driven by hand: the first callback runs before the layout that + measures the older rows, waits, and the next one scrolls past them. Out of tries, it + stops waiting.""" + app = tui_app.PepperApp(_older_thread_server().client()) + async with app.run_test(size=(100, 14)) as pilot: + await _open_a1(app, pilot, "body 13") + thread = app.thread + scroller = thread.scroller + scroller.scroll_y = 0 + await pilot.pause() + held: list = [] + scrolled: list[float] = [] + + def hold(cb, *_a, **_k) -> None: + # Only keep-place's own callbacks: the app may schedule others meanwhile. + if getattr(cb, "__name__", "") == "keep_place": + held.append(cb) + + monkeypatch.setattr(scroller, "call_after_refresh", hold) + monkeypatch.setattr(scroller, "scroll_to", lambda *, y, **_: scrolled.append(y)) + # Hold the measured height where it was, as a loaded loop does until its next layout. + real = inspect.getattr_static(Widget, "virtual_size") + old = scroller.virtual_size + measured = [old] + monkeypatch.setattr( + type(scroller), + "virtual_size", + property(lambda _w: measured[0], lambda w, v: real.__set__(w, v)), + raising=False, + ) + # Read here, not assumed 0: the app may itself have loaded an older page by now. + at = scroller.scroll_y + thread.show("\n".join(f"older {i}" for i in range(40)), prepended=True) + keep_place = held.pop() + keep_place() # not measured yet: grown is 0 + assert scrolled == [] # it did not scroll by 0 + retry = held.pop() # it waits for the next refresh instead + measured[0] = Size(old.width, old.height + 12) # that layout measures the rows + retry() + assert scrolled == [at + 12] # scrolled past exactly the rows added above + measured[0] = old + keep_place(1) # out of tries with nothing measured: scrolls by 0, waits no longer + assert scrolled == [at + 12, at] + assert held == [] + + +async def test_a_reset_stands_down_a_pending_keep_place(monkeypatch) -> None: + """A keep-place retry from an older page must not move a conversation shown after it.""" + app = tui_app.PepperApp(_older_thread_server().client()) + async with app.run_test(size=(100, 14)) as pilot: + await _open_a1(app, pilot, "body 13") + thread = app.thread + scroller = thread.scroller + scroller.scroll_y = 0 + await pilot.pause() + held: list = [] + monkeypatch.setattr(scroller, "call_after_refresh", lambda cb, *a, **k: held.append(cb)) + longer = "\n".join(f"older {i}" for i in range(40)) + "\n" + body_text(app) + thread.show(longer, prepended=True) + stale = next(cb for cb in reversed(held) if getattr(cb, "__name__", "") == "keep_place") + monkeypatch.undo() + thread.show("\n".join(f"other {i}" for i in range(120)), reset=True) + await until(pilot, lambda: scroller.scroll_y == scroller.max_scroll_y > 0) + # Back where the stale one started, so only its generation tells it apart. + scroller.scroll_y = 0 + await pilot.pause() + stale() + await pilot.pause() + assert scroller.scroll_y == 0 + + async def test_a_scroll_made_while_keep_place_waits_is_not_undone() -> None: """VR-6b3a8f9f: the scroll past older rows waits a refresh or more; a reader who moves in that window keeps the place they moved to.""" From 9b8d7720754c33e5def916ee64727411a1fd7ca2 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 16 Nov 2026 17:36:03 +0000 Subject: [PATCH 4/4] Make the reset keep-place test independent of the app's own older-page load The Windows run loaded an older page while the test waited at the top, so the view moved for a reason the test was not about. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01ALQbQ2fKvgEPYLj9LMKnnH --- tests/test_tui_parity.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/tests/test_tui_parity.py b/tests/test_tui_parity.py index 4342569..a824dfa 100644 --- a/tests/test_tui_parity.py +++ b/tests/test_tui_parity.py @@ -402,18 +402,21 @@ async def test_a_reset_stands_down_a_pending_keep_place(monkeypatch) -> None: await pilot.pause() held: list = [] monkeypatch.setattr(scroller, "call_after_refresh", lambda cb, *a, **k: held.append(cb)) + # Read here, not assumed 0: the app may itself have loaded an older page by now. + at = scroller.scroll_y longer = "\n".join(f"older {i}" for i in range(40)) + "\n" + body_text(app) thread.show(longer, prepended=True) stale = next(cb for cb in reversed(held) if getattr(cb, "__name__", "") == "keep_place") monkeypatch.undo() thread.show("\n".join(f"other {i}" for i in range(120)), reset=True) await until(pilot, lambda: scroller.scroll_y == scroller.max_scroll_y > 0) - # Back where the stale one started, so only its generation tells it apart. - scroller.scroll_y = 0 - await pilot.pause() - stale() - await pilot.pause() - assert scroller.scroll_y == 0 + # Back where the stale one started, so only its conversation tells it apart; with no + # await in between, nothing else gets to move the view. + scroller.scroll_y = at + scrolled: list[float] = [] + monkeypatch.setattr(scroller, "scroll_to", lambda *, y, **_: scrolled.append(y)) + stale(1) # its last try, where it would otherwise scroll whatever it measures + assert scrolled == [] async def test_a_scroll_made_while_keep_place_waits_is_not_undone() -> None: