Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
167 changes: 167 additions & 0 deletions frontends/tests/test_tuiapp_v2_stream_layout.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
"""Streaming repaint must not re-arrange the transcript on every token.

`Static.update()` defaults to `layout=True`, so the 20Hz streaming paint used to
mark the whole screen layout-dirty and make Textual re-arrange every message on
the next frame (5-15ms per chunk on a 30-turn session, ~430ms on long ones).

`_update_text()` only asks for a layout pass when the rendered height changes.
These tests pin both halves of that contract:
* same-line growth must not request a layout (the perf fix),
* content that grows new lines must still grow the widget (no clipping).
"""
import asyncio
import os
import sys
import time
from pathlib import Path
from types import SimpleNamespace

import pytest

ROOT = Path(__file__).resolve().parents[2]
if str(ROOT) not in sys.path:
sys.path.insert(0, str(ROOT))
FE = ROOT / "frontends"
if str(FE) not in sys.path:
sys.path.insert(0, str(FE))



def _repo_module_names() -> set:
"""Top-level module names that really exist on disk in this checkout."""
names = set()
for base in (ROOT, FE):
try:
for entry in os.listdir(base):
if entry.endswith(".py"):
names.add(entry[:-3])
except OSError:
pass
return names


def _purge_stub_modules() -> None:
"""Drop bare-namespace stubs that sibling tests leave in ``sys.modules``.

Several tests stub repo modules (``agentmain``, ``agent_loop``, ``llmcore``)
for isolation; a leftover stub has ``__file__ is None`` and makes the real
import fail with "cannot import name ... (unknown location)". Only names
that correspond to an actual ``.py`` in this checkout are considered, so
builtins such as ``sys`` (which legitimately has no ``__file__``) are safe.
"""
if sys.modules.get("sys") is not sys: # some tests swap this one out
sys.modules["sys"] = sys
for name in _repo_module_names():
module = sys.modules.get(name)
if module is not None and getattr(module, "__file__", None) is None:
sys.modules.pop(name, None)


def _import_tui():
"""Import the TUI module with the import chain cleaned up first."""
global T
_purge_stub_modules()
try:
import tuiapp_v2
return tuiapp_v2
except ImportError:
# A stubbed dependency deeper in the chain (imported by a real module)
# only surfaces as ImportError on the first try; purge and retry once.
for name in ("tuiapp_v2", "chatapp_common", "agentmain", "agent_loop", "llmcore"):
sys.modules.pop(name, None)
_purge_stub_modules()
import tuiapp_v2
return tuiapp_v2

T = None


class _StubAgent:
"""Enough of an agent for the app to mount a message without a network."""

def __init__(self):
self.log_path = ""
self.inc_out = True
self.task_dir = ""
self.extra_sys_prompts = []
self._rw_state = {}
self.history = []
self.name = "probe"
self.model_name = "stub"
self.last_tokens = (0, 0)
self.token_usage = {}


def _patch_layout_counter(app):
counts = {"n": 0, "t": 0.0}
screen_cls = type(app.screen)
original = screen_cls._refresh_layout

def counting(screen, *args, **kwargs):
start = time.perf_counter()
try:
return original(screen, *args, **kwargs)
finally:
counts["n"] += 1
counts["t"] += time.perf_counter() - start

screen_cls._refresh_layout = counting
return counts, lambda: setattr(screen_cls, "_refresh_layout", original)


async def _scenario():
app = T.GenericAgentTUI()
app.make_agent = lambda sess: setattr(sess, "agent", _StubAgent())
result = {}
async with app.run_test(size=(120, 40)) as pilot:
counts, restore = _patch_layout_counter(app)
try:
app.add_session("probe")
sess = app.current
m = T.ChatMessage(role="assistant", content="", done=False)
sess.messages.append(m)
app._mount_message(app.query_one("#messages"), m)

# 1) growth inside one line: no layout requests, height stays 1.
counts["n"] = 0
for i in range(20):
m.content = "x" * 10 + "y" * (i * 3)
app._stream_update_assistant(m)
await pilot.pause(0.01)
widget = m._segment_widgets[-1]
result["inline_layouts"] = counts["n"]
result["inline_height"] = widget.size.height

# 2) new lines: layout must be requested and the widget must grow.
counts["n"] = 0
for i in range(1, 9):
m.content = "\n".join("line %02d" % j for j in range(i + 1))
app._stream_update_assistant(m)
await pilot.pause(0.05)
result["line_layouts"] = counts["n"]
result["line_height"] = widget.size.height
last = widget.render_line(widget.size.height - 1)
result["last_line"] = "".join(seg.text for seg in last).strip()
finally:
restore()
return result


def test_streaming_repaint_is_height_aware():
global T
T = _import_tui()
r = asyncio.run(_scenario())

# A 20-chunk same-line stream must not re-arrange the screen per chunk.
assert r["inline_layouts"] <= 4, (
"same-line streaming requested %d layouts (expected <=4)" % r["inline_layouts"])
assert r["inline_height"] == 1, (
"single-line content occupies %d rows" % r["inline_height"])

# Growing to 9 lines must be visible, not clipped.
assert r["line_height"] == 9, (
"9-line content rendered in %d rows" % r["line_height"])
assert "line 08" in r["last_line"], (
"last line clipped, got %r" % r["last_line"])
assert r["line_layouts"] >= 6, (
"line growth requested only %d layouts" % r["line_layouts"])
67 changes: 55 additions & 12 deletions frontends/tuiapp_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,14 @@ def _hint_terminal_capabilities() -> None:
_hint_terminal_capabilities()


# Streaming paint coalescing window (seconds). Every token chunk used to be
# painted on a 50ms timer; each paint re-arranged the whole transcript because
# `Static.update()` defaults to `layout=True`. The layout cost is now avoided
# (`_update_text`), and 100ms halves the remaining repaint traffic without a
# visible difference in typing feel. Override: GA_TUI_PAINT_MS.
STREAM_PAINT_INTERVAL = max(0.02, float(os.environ.get("GA_TUI_PAINT_MS", "100")) / 1000.0)


# Strip terminal control sequences from subprocess stdout but keep SGR color codes,
# otherwise Text.from_ansi loses color downstream.
_ANSI_CONTROL_RE = re.compile(
Expand Down Expand Up @@ -6986,7 +6994,8 @@ def _update_assistant(self, agent_id, text, *, task_id=None, done=True,
# Keep data current for stop/copy/session switching; coalesce only paint.
self._pending_stream_renders[(agent_id, task_id)] = found
if self._stream_render_timer is None:
self._stream_render_timer = self.set_timer(0.05, self._flush_stream_renders)
self._stream_render_timer = self.set_timer(
STREAM_PAINT_INTERVAL, self._flush_stream_renders)
return
self._pending_stream_renders.pop((agent_id, task_id), None)
self._render_assistant_update(found)
Expand Down Expand Up @@ -7814,7 +7823,7 @@ def _mark_stopping(self, sess) -> None:
if m.role == "assistant" and not m.done:
self._capture_stop_summary(m)
if m._spinner_widget is not None:
try: m._spinner_widget.update(self._stopping_annotation(m))
try: self._update_text(m._spinner_widget, self._stopping_annotation(m))
except Exception: pass

def _has_streaming(self) -> bool:
Expand All @@ -7841,7 +7850,7 @@ def _spinner_tick(self) -> None:
if m.role == "assistant" and not m.done and m._spinner_widget is not None:
if m._stream_started_at is None:
self._mark_stream_start(m)
try: m._spinner_widget.update(self._spinner_annotation(m))
try: self._update_text(m._spinner_widget, self._spinner_annotation(m))
except Exception: pass
if not self._has_streaming():
self._ensure_spinner()
Expand Down Expand Up @@ -8010,7 +8019,7 @@ def _sync_spinner_widget(self, container, m: ChatMessage, anchor) -> None:
else: container.mount(w, after=anchor)
m._spinner_widget = w
else:
try: m._spinner_widget.update(self._done_annotation(m))
try: self._update_text(m._spinner_widget, self._done_annotation(m))
except Exception: pass
return
if m._spinner_widget is None:
Expand All @@ -8023,6 +8032,42 @@ def _sync_spinner_widget(self, container, m: ChatMessage, anchor) -> None:
container.mount(w, after=anchor)
m._spinner_widget = w

@staticmethod
def _rendered_height(widget, rendered, width: int):
"""Height `rendered` would occupy in `widget` at `width`, or None if unknown."""
try:
from textual.visual import visualize
visual = visualize(widget, rendered,
markup=getattr(widget, "_render_markup", True))
return visual.get_height(widget.styles, width)
except Exception:
return None

def _update_text(self, widget, rendered, *, width: int | None = None) -> None:
"""`Static.update()` that only asks for a layout pass when the rendered
height actually changes.

Why: `Static.update()` defaults to `layout=True`, which marks the whole
screen layout-dirty. Textual then re-arranges the entire transcript on
the next frame — measured at 5-15ms for a 30-turn session and ~430ms on
long ones. Streaming calls this 20x/s, so the TUI burned ~25% of the
main thread just recomputing positions for text that grew by three
characters on the same line.

`refresh(repaint=True)` (what `update()` does) already invalidates the
cached content height, so a skipped layout pass can never keep a stale
height: the very next real layout re-measures from the new visual.
"""
if widget is None:
return
if width is None:
width = getattr(widget.size, "width", 0) or self._messages_width()
new_h = self._rendered_height(widget, rendered, width)
old_h = getattr(widget, "_ga_last_height", None)
widget._ga_last_height = new_h
widget.update(rendered, layout=(old_h is None or new_h is None
or new_h != old_h))

def _stream_update_assistant(self, m: ChatMessage) -> None:
"""Cheap path for per-chunk streaming: if the fold topology is unchanged, only
the last text segment got new content, so render and update that one widget.
Expand All @@ -8047,9 +8092,9 @@ def _stream_update_assistant(self, m: ChatMessage) -> None:
rendered = self._render_md(last_text, width)
if isinstance(rendered, _MdRender):
last_widget._ga_render = rendered
last_widget.update(rendered.text)
self._update_text(last_widget, rendered.text, width=width)
else:
last_widget.update(rendered)
self._update_text(last_widget, rendered, width=width)
else:
last_widget._ga_render = None
# Normalise CRLF → LF before from_ansi. On Windows child stdout
Expand All @@ -8061,15 +8106,13 @@ def _stream_update_assistant(self, m: ChatMessage) -> None:
# Markdown to fold on completion. Lone `\r` (no `\n`) is kept so
# progress-bar overwrites still work.
display = last_text.replace("\r\n", "\n")
last_widget.update(Text.from_ansi(display, style=C_FG))
try:
self.query_one("#messages", VerticalScroll).refresh()
except Exception:
pass
self._update_text(last_widget,
Text.from_ansi(display, style=C_FG),
width=width)
if m.done and m._spinner_widget is not None:
# Convert the live spinner into the post-turn ⠿ card in place.
self._capture_done_summary(m)
try: m._spinner_widget.update(self._done_annotation(m))
try: self._update_text(m._spinner_widget, self._done_annotation(m))
except Exception: pass
return
self._remount_assistant_message(m)
Expand Down