Skip to content
Open
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
175 changes: 175 additions & 0 deletions frontends/tests/test_tuiapp_v2_sidebar_refresh.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
"""Sidebar repaint policy: paint only when what it draws actually changed.

Background: the streaming path calls `_refresh_sidebar()` on every 50ms paint
batch. Before this policy that rebuilt the whole session table 20x/s.

A plain time-based throttle is *wrong* for this widget. `render_sidebar()`
draws exactly four things per row — session id, name, status, and the Q/S
preview lines — and all four are covered by `_sidebar_visible_sig()`. So the
correct policy is content-driven, not clock-driven:

* **While tokens stream**: status and history are frozen (messages are only
appended at turn boundaries), so the signature is stable → strictly zero
repaints, not "one every 5s".
* **The instant something really changes** (running→idle when `done` arrives, a
new prompt, a fresh summary) → a single repaint, with no timer in between.
This is the same edge-triggered behaviour the desktop notifier gets from its
`agent_after` ping.
* `force=True` (switch / rename / rewind / theme) → paint regardless.

Run: pytest frontends/tests/test_tuiapp_v2_sidebar_refresh.py -v
"""
import asyncio
import sys
import threading
import time
from pathlib import Path
from types import SimpleNamespace

ROOT = Path(__file__).resolve().parent.parent.parent
sys.path.insert(0, str(ROOT))
sys.path.insert(0, str(ROOT / "frontends"))

import tuiapp_v2 as T # noqa: E402


class _StubAgent:
"""Minimal stand-in for GenericAgent: idle run loop + attrs add_session sets."""

def __init__(self):
self.log_path = ""
self.inc_out = True
self.task_dir = ""
self.extra_sys_prompts = []
self._rw_store = None
self.history = []
self._stop = threading.Event()
self.llmclient = None

def abort(self):
pass

def run(self):
self._stop.wait(3600)


def _attach_history(sess, messages):
sess.agent.llmclient = SimpleNamespace(
backend=SimpleNamespace(history=messages))


def test_sidebar_repaints_only_when_rendered_content_changes():
async def scenario():
app = T.GenericAgentTUI(agent_factory=lambda: _StubAgent())
async with app.run_test(size=(120, 40)) as pilot:
await pilot.pause()

paints = []
real_paint = app._paint_sidebar

def counting_paint():
paints.append(time.monotonic())
real_paint()

app._paint_sidebar = counting_paint

sid = next(iter(app.sessions))
sess = app.sessions[sid]
hist = [{"role": "user", "content": "hello"}]
_attach_history(sess, hist)
sess.status = "idle"

# 0) baseline: force paints once and settles the painted signature
app._refresh_sidebar(force=True)
assert len(paints) == 1

# 1) stream hot path: 20 calls, nothing rendered changed → zero paints
for _ in range(20):
app._refresh_sidebar()
assert len(paints) == 1, "unchanged sidebar must not repaint"

# 2) status flip (task finished) → immediate, no timer involved
sess.status = "running"
app._refresh_sidebar()
assert len(paints) == 2, "status change must paint immediately"

sess.status = "idle"
started = time.monotonic()
app._refresh_sidebar()
assert len(paints) == 3, "running→idle must paint immediately"
assert time.monotonic() - started < 1.0, \
"done must land without waiting for any throttle window"

# 3) turn boundary: Q/S preview changes → immediate repaint
hist.append({"role": "assistant", "content": "<summary>did it</summary>"})
app._refresh_sidebar()
assert len(paints) == 4, "new summary preview must paint immediately"

# 4) streaming into the same turn (backend.history untouched) → no paint
for _ in range(20):
app._refresh_sidebar()
assert len(paints) == 4, "streaming must not repaint the sidebar"

# 5) user action forces a paint even with an identical signature
app._refresh_sidebar(force=True)
assert len(paints) == 5

# 6) no timer machinery: repaint is purely content-driven, so the
# app must never arm a delayed sidebar repaint.
assert not hasattr(app, "_sidebar_timer")
assert not hasattr(app, "_sidebar_dirty")

asyncio.run(scenario())


def test_sidebar_preview_cache_avoids_rescanning_history():
"""Q/S derivation walks + regexes the history; memoise it per (list, len)."""
app = T.GenericAgentTUI(agent_factory=lambda: _StubAgent())
sess = T.AgentSession(agent_id=1, name="s", agent=_StubAgent())
hist = [{"role": "user", "content": "hi"}]
_attach_history(sess, hist)
app.sessions = {1: sess}

first = app._sidebar_visible_sig()
calls = []
real_user = T._sidebar_last_user

def counting_user(s):
calls.append(s)
return real_user(s)

T._sidebar_last_user = counting_user
try:
second = app._sidebar_visible_sig()
assert second == first
assert calls == [], "unchanged history must hit the cache"

hist.append({"role": "assistant", "content": "<summary>ok</summary>"})
third = app._sidebar_visible_sig()
assert third != second, "appended history must invalidate the cache"
assert len(calls) == 1
finally:
T._sidebar_last_user = real_user


def test_sidebar_signature_covers_everything_render_sidebar_draws():
"""Guard against a future render_sidebar field that the signature ignores."""
import inspect
src = inspect.getsource(T.render_sidebar)
for token in ('sess.name', 'sess.status', '_sidebar_last_user',
'_sidebar_last_summary', 'sessions.items()'):
assert token in src, f"render_sidebar draws {token}; keep the sig in sync"


def test_stream_path_uses_non_forced_refresh():
"""Streaming/fast paths must go through the signature check, not force."""
src = (ROOT / "frontends" / "tuiapp_v2.py").read_text(encoding="utf-8")
# the 50ms stream flush calls the non-forced form
assert "self._refresh_sidebar()\n self._refresh_topbar()" in src
# the 1s watchdog keeps its cheap (sid, status) pre-check
# the non-forced path compares the full rendered-content signature
assert "self._sidebar_visible_sig()" in src
assert 'if not force and self._sidebar_visible_sig() == getattr(' in src
assert 'self, "_sidebar_painted_sig", None):' in src
assert "SIDEBAR_REFRESH_SEC" not in src, \
"time-based throttle was removed in favour of content-driven refresh"
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"])
Loading