From 6dd4c3656ca4204729569dbf7a61ed29dded39e0 Mon Sep 17 00:00:00 2001 From: Tristan Simas Date: Wed, 18 Nov 2026 09:56:37 -0500 Subject: [PATCH 1/2] qt: a CLI's newest item shows when Core publishes it, not when its next one arrives The Qt window followed only `CoordinationObserved`, which CLI drafts and finished items never change (they feed no thread status), so a CLI thread's view re-read its transcript only when some status change happened by. Measured on the live bus against this session's own CLI (tools/qt_drive.py cli_sync): an item reached Core in under 0.1 s and stayed off the Qt view for 4-33 s, until the CLI wrote its next item. The TUI already follows `ExternalTranscriptChanged`. Both toolkits now route that event through one owner, `SourcedConversation. external_transcript_changed`, which asks the view's operational sources to read: the Textual presentation calls it on its widget, and the Qt window calls it on every open view (shown or parked); a page without a transcript answers with nothing to read. After: 0.2 s and 0.6 s from Core's store to the Qt view, ahead of a direct Core read. Drive scenarios added: `cli_sync` (CLI transcript vs Qt rows vs direct Core reads, with a frame recording), `peer_profile` (ask a tailnet peer for a UI profile as the bus picker does) and `idle` (this process's CPU per thread while following a bus untouched). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01RnH6dT7wdh7i4tjJZQdFu3 --- src/toad/client/session_sources.py | 6 + src/toad/qt/page.py | 3 + src/toad/qt/window.py | 15 ++- src/toad/session_presentation.py | 2 +- tools/qt_drive.py | 188 +++++++++++++++++++++++++++++ 5 files changed, 212 insertions(+), 2 deletions(-) diff --git a/src/toad/client/session_sources.py b/src/toad/client/session_sources.py index 24935cfa9..4ad87b0cd 100644 --- a/src/toad/client/session_sources.py +++ b/src/toad/client/session_sources.py @@ -159,3 +159,9 @@ def queue_source(self) -> QueueSource: def begin_submission(self, owner, submission): """Where this view's input goes: its operational sources answer for agent and CLI views.""" return self.operational_sources.begin_submission(owner, submission) + + def external_transcript_changed(self) -> None: + """Core published this thread's CLI drafts or finished items: its sources read them, + shown or parked. Both toolkits route `ExternalTranscriptChanged` here; a view that + waited for a status change instead held a CLI's newest item until its next one.""" + self.operational_sources.external_transcript_changed(self) diff --git a/src/toad/qt/page.py b/src/toad/qt/page.py index 4b811a631..21c833604 100644 --- a/src/toad/qt/page.py +++ b/src/toad/qt/page.py @@ -27,6 +27,9 @@ class Page: def reload(self) -> None: """A page fed by its own subscription has nothing to read again.""" + def external_transcript_changed(self) -> None: + """A page showing no thread's transcript has no CLI items to read.""" + def shown(self, window: "QtToadApp", visited: "Callable[[CommsTab], object]") -> None: """Selected: read again; a tab passes its identity to the visit history.""" self.reload() diff --git a/src/toad/qt/window.py b/src/toad/qt/window.py index 4719b5926..34b09aaca 100644 --- a/src/toad/qt/window.py +++ b/src/toad/qt/window.py @@ -108,7 +108,7 @@ from toad.client.tab_identity.kinds import CommsTab from toad.constants import WELCOME from toad.core import session_requests -from toad.core.events import CoordinationObserved, TabHistoryChanged +from toad.core.events import CoordinationObserved, ExternalTranscriptChanged, TabHistoryChanged from toad.launch_binding import AgentBinding, NoAgent from toad.navigation_context import NoNavigationOwner from toad.client.session_scope import HomeSessionOrigin @@ -1518,6 +1518,12 @@ def select(self, mode: str, visited: Callable[[CommsTab], object]) -> None: def detach(self, identity: str) -> ConversationView | None: return self.views.pop(identity, None) + def external_transcript_changed(self) -> None: + """Every open view reads its CLI items, shown or parked, as Textual's screens do: a + parked view that waited would draw what it missed as one burst on return.""" + for view in tuple(self.views.values()): + view.external_transcript_changed() + async def finish(self, view: ConversationView) -> None: # As Textual's `Conversation` unmount: the view's agent leaves its surface and stops # before the widgets it draws into are deleted. @@ -1675,6 +1681,10 @@ def __init__(self, connection: CoreConnection, project: Checkout, self.sidebar_layout.set_collapsed(ChannelsSidebarPlacement, self.settings.sidebar.hide) self.rearrange_sidebars() self.changes = self.coordination_access.events.subscribe(CoordinationObserved, self.stores_changed) + # CLI drafts and finished items change no thread status, so `CoordinationObserved` + # never carries them: without this a CLI's newest item waited for its next one. + self.transcripts = self.coordination_access.events.subscribe( + ExternalTranscriptChanged, self.external_transcript_changed) self.history = self.tab_order.events.subscribe(TabHistoryChanged, self.history_changed) self.history_changed() self.shortcuts = ShortcutLifecycle[ApplicationKeys](QApplication.instance()) @@ -1717,6 +1727,9 @@ def stores_changed(self, event: CoordinationObserved, subscription: object) -> N self.workspace_sessions.home.project.present(self) self.workspace_sessions.selected.reload() + def external_transcript_changed(self, event: ExternalTranscriptChanged, subscription: object) -> None: + self.workspace_sessions.external_transcript_changed() + def history_changed(self, *event: object) -> None: """Back and forward are live exactly when the visit history has a step that way.""" self.back.setEnabled(self.tab_order.history_target(-1) is not None) diff --git a/src/toad/session_presentation.py b/src/toad/session_presentation.py index 9f2a92340..368bcbc34 100644 --- a/src/toad/session_presentation.py +++ b/src/toad/session_presentation.py @@ -346,7 +346,7 @@ def external_transcript_changed(self) -> None: the rest as one burst. An unbuilt view reads its source on attach. """ if self.widget is not None: - self.sources.external_transcript_changed(self.widget) + self.widget.external_transcript_changed() async def remove_native_tree(self, screen: MainScreen) -> None: if (conversation := self.widget) is None: diff --git a/tools/qt_drive.py b/tools/qt_drive.py index 6187f1d11..535d0de42 100644 --- a/tools/qt_drive.py +++ b/tools/qt_drive.py @@ -2175,6 +2175,194 @@ def wrapper(*arguments, **options): queue_changes=len(toggles), largest_scroll_step=max(map(abs, jumps), default=0)) +class CliSyncScenario(Scenario): + summary = ("open TARGET (a Claude Code session's thread) and follow TRANSCRIPT (that session's " + "JSONL, what its CLI shows) for DURATION seconds; every SYNCPROBE-- " + "token is timed when the CLI's transcript gains it and when the Qt view's rows first " + "show it, and the window is recorded every FRAME_MS ms; prints the lag per token") + + token = r"SYNCPROBE-\d+-[A-Za-z]+\d*" + width: ClassVar[int] = 412 + + def shown_texts(self) -> list[str]: + """Every row the reader's view holds, saved or still live: the model's own fragments.""" + from toad.qt.history import HistoryRows + rows = self.page.messages.model_rows + return [rows.index(row).data(HistoryRows.FRAGMENT).clipboard_text for row in range(rows.rowCount())] + + async def run(self) -> None: + import json + import re + from PySide6.QtCore import QTimer + target = os.environ["TARGET"] + transcript = Path(os.environ["TRANSCRIPT"]) + duration = float(os.environ.get("DURATION", "240")) + frame_ms = int(os.environ.get("FRAME_MS", "200")) + self.app.resize(self.width, 915) + await self.listed() + await self.open(await self.found(lambda row: isinstance(row, ConversationRow) + and target in (row.key, row.target.name), + f"thread {target}", rows=self.thread_rows), target) + self.report("1-opened", rows=self.page.messages.model_rows.rowCount()) + pattern = re.compile(self.token) + cli: dict[str, tuple[float, str]] = {} + qt: dict[str, float] = {} + frames = self.out / "frames" + frames.mkdir(exist_ok=True) + source = transcript.open("rb") + source.seek(0, os.SEEK_END) + pending = b"" + frame = 0 + print(f"following {transcript} from byte {source.tell()}; recording every {frame_ms} ms", flush=True) + # Core asked directly each second, beside the observation the view follows: a token Core + # serves on asking but the view has not shown is held in the push, not in Core's read. + import threading + from agent_comms.nominal.field_codec import FieldCodec + from agent_comms.transport.tools.reads import TranscriptPageRead + core_seen: dict[str, float] = {} + reading = threading.Event() + reading.set() + asked = self.app.coordination_access.open_current().core + + def ask_core() -> None: + while reading.is_set(): + begun = time.time() + try: + text = json.dumps(FieldCodec.encode(asked.call(TranscriptPageRead(thread=target)))) + except Exception as error: # a failed read is printed, never a quiet gap + print(f"core read failed: {error!r}"[:200], flush=True) + text = "" + for name in set(pattern.findall(text)) - core_seen.keys(): + core_seen[name] = begun + print(f"core {name} at {begun:.3f} (read took {time.time() - begun:.2f}s)", flush=True) + time.sleep(max(0.0, 1.0 - (time.time() - begun))) + asker = threading.Thread(target=ask_core, daemon=True) + asker.start() + + def poll() -> None: + nonlocal pending + now = time.time() + pending += source.read() + *lines, pending = pending.split(b"\n") + for line in lines: + tokens = set(pattern.findall(line.decode(errors="replace"))) + if not tokens: + continue + written = json.loads(line).get("timestamp", "") + for name in tokens - cli.keys(): + cli[name] = (now, written) + print(f"cli {name} at {now:.3f} (line stamped {written})", flush=True) + for text in self.shown_texts(): + for name in set(pattern.findall(text)) - qt.keys(): + qt[name] = now + print(f"qt {name} at {now:.3f}", flush=True) + + def record() -> None: + nonlocal frame + stamp = time.strftime("%H%M%S", time.localtime()) + f"{time.time() % 1:.2f}"[1:] + self.app.grab().save(str(frames / f"{frame:05d}-{stamp}.jpg"), "JPG", 70) + frame += 1 + timers = [] + for interval, call in ((50, poll), (frame_ms, record)): + timer = QTimer() + timer.setInterval(interval) + timer.timeout.connect(call) + timer.start() + timers.append(timer) + started = time.monotonic() + while time.monotonic() - started < duration: + ends = [name for name in cli if name.endswith("-END")] + if ends and ends[0] in qt: + await asyncio.sleep(2.0) + break + await asyncio.sleep(0.25) + for timer in timers: + timer.stop() + reading.clear() + poll() + rows = [] + for name in sorted(cli, key=lambda name: cli[name][0]): + seen, written = cli[name] + lag = None if name not in qt else round((qt[name] - seen) * 1000) + served = None if name not in core_seen else round((core_seen[name] - seen) * 1000) + rows.append({"token": name, "cli": round(seen, 3), "line_timestamp": written, + "core": core_seen.get(name), "core_ms": served, + "qt": qt.get(name), "lag_ms": lag}) + print(f"lag {name}: core {'never' if served is None else f'{served} ms'}, " + f"view {'never shown' if lag is None else f'{lag} ms'}", flush=True) + only_qt = sorted(set(qt) - set(cli)) + (self.out / "sync.json").write_text(json.dumps({"tokens": rows, "qt_only": only_qt}, indent=1)) + lags = sorted(row["lag_ms"] for row in rows if row["lag_ms"] is not None) + if lags: + print(f"lag over {len(lags)} tokens: min {lags[0]} ms, median {lags[len(lags) // 2]} ms, " + f"max {lags[-1]} ms; {len(rows) - len(lags)} never shown; {frame} frames", flush=True) + self.report("2-done", tokens=len(rows), shown=len(lags)) + + +class IdleScenario(Scenario): + summary = ("sit on the welcome sidebar for SETTLE seconds, then open TARGET (#channel or " + "thread) and sit for DURATION seconds, touching nothing; every 5 s print this " + "process's CPU use and its busiest threads, the UI's own cost of following the bus") + + @staticmethod + def thread_ticks() -> dict[str, int]: + """CPU ticks per thread name, from /proc: utime + stime.""" + ticks: dict[str, int] = {} + for task in Path("/proc/self/task").iterdir(): + try: + stat = (task / "stat").read_text() + except OSError: + continue + name = stat[stat.index("(") + 1:stat.rindex(")")] + fields = stat[stat.rindex(")") + 2:].split() + ticks[name] = ticks.get(name, 0) + int(fields[11]) + int(fields[12]) + return ticks + + async def sample(self, label: str, seconds: float) -> list[float]: + hertz = os.sysconf("SC_CLK_TCK") + shares: list[float] = [] + ended = time.monotonic() + seconds + while time.monotonic() < ended: + wall, cpu, before = time.monotonic(), time.process_time(), self.thread_ticks() + await asyncio.sleep(5.0) + share = 100 * (time.process_time() - cpu) / (time.monotonic() - wall) + shares.append(share) + after = self.thread_ticks() + busiest = sorted(((after[name] - before.get(name, 0)) / hertz / 5 * 100, name) + for name in after)[-3:] + print(f"{label} cpu {share:5.1f}% busiest: " + + ", ".join(f"{name} {percent:.0f}%" for percent, name in reversed(busiest)), flush=True) + return shares + + async def run(self) -> None: + settle = float(os.environ.get("SETTLE", "60")) + duration = float(os.environ.get("DURATION", "120")) + target = os.environ["TARGET"] + await self.listed() + home = await self.sample("home", settle) + await self.open_named(target) + self.report("1-opened", rows=self.page.messages.model_rows.rowCount()) + opened = await self.sample(target, duration) + for label, shares in (("home", home), (target, opened)): + ordered = sorted(shares) + print(f"{label}: median {ordered[len(ordered) // 2]:.1f}% cpu, max {ordered[-1]:.1f}% " + f"over {len(shares) * 5} s", flush=True) + self.report("2-done", rows=self.page.messages.model_rows.rowCount()) + + +class PeerProfileScenario(Scenario): + summary = ("ask PEER (a tailnet machine, at ADDRESS) for a UI profile as the app's own " + "bus picker does, replacing the profile held for it; prints what the peer answered") + + async def run(self) -> None: + from agent_comms.transport.tailnet_buses import NamedHost, TailnetPeer + from agent_comms.transport.tailnet_profiles import TailnetProfiles + await self.listed() + found = NamedHost(peer=TailnetPeer(name=os.environ["PEER"], address=os.environ["ADDRESS"], online=True)) + asked = await asyncio.to_thread(self.app.hold_peer_profile, found) + print(f"peer answered: {type(asked).__name__}; held at {TailnetProfiles.path_for(found)}", flush=True) + + class DeliveryPreviewScenario(Scenario): summary = ("open TARGET (#scratch), send 2 messages with ui.input.delivery_preview on, then 2 with " "it off, turning it off and on live: the queue rows show only while it is on, the info " From ec6348143b371fd8397608cf6b058c4aa258a1e8 Mon Sep 17 00:00:00 2001 From: Tristan Simas Date: Wed, 18 Nov 2026 10:02:09 -0500 Subject: [PATCH 2/2] qt_drive idle: count the sidebar wave's row repaints and how many moved its band Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01RnH6dT7wdh7i4tjJZQdFu3 --- tools/qt_drive.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/tools/qt_drive.py b/tools/qt_drive.py index 535d0de42..529ef3250 100644 --- a/tools/qt_drive.py +++ b/tools/qt_drive.py @@ -2330,14 +2330,34 @@ async def sample(self, label: str, seconds: float) -> list[float]: after = self.thread_ticks() busiest = sorted(((after[name] - before.get(name, 0)) / hertz / 5 * 100, name) for name in after)[-3:] + waves = self.waves print(f"{label} cpu {share:5.1f}% busiest: " - + ", ".join(f"{name} {percent:.0f}%" for percent, name in reversed(busiest)), flush=True) + + ", ".join(f"{name} {percent:.0f}%" for percent, name in reversed(busiest)) + + f" wave: {waves['frames'] / 5:.0f} row repaints/s over {len(waves['rows'])} rows, " + f"{waves['moved'] / 5:.0f}/s moved the band", flush=True) + waves.update(frames=0, moved=0, rows=set()) return shares async def run(self) -> None: settle = float(os.environ.get("SETTLE", "60")) duration = float(os.environ.get("DURATION", "120")) target = os.environ["TARGET"] + # Each sidebar wave frame repaints its row; a frame whose band covers the same cells as + # the last one paints the same pixels. Counted, so the sample says what the clock costs. + waves = {"frames": 0, "moved": 0, "rows": set()} + spans: dict[int, object] = {} + original = ConversationRow.repaint + + def counted(row) -> None: + span = row.wave_span() + waves["frames"] += 1 + waves["rows"].add(id(row)) + if spans.get(id(row)) != span: + waves["moved"] += 1 + spans[id(row)] = span + original(row) + ConversationRow.repaint = counted + self.waves = waves await self.listed() home = await self.sample("home", settle) await self.open_named(target)