App: replace a silent socket instead of hanging; blink the terminal cursor on a timer; keep one markdown cache entry per streamed reply

A socket that outlived a sleep or network change looked open, so every
queued request waited its own 30 s timeout behind the last until the page
was reopened. The client now replaces a socket whose request times out
unheard (or after two timeouts in a row), and probes it with a ping after
a quiet spell or on returning to the foreground.

The terminal's blinking cursor ran a Ticker (a frame every vsync); a 530 ms
timer now flips it. Streamed deltas no longer flood the markdown cache and
evict finished answers.

Adds tool/perf/idle_session_bench.mjs (idle frames, long-session heap,
snapshot census) and notes/perf-hangs-idle.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-01 20:47:22 +00:00
parent 23d8658e80
commit 2889d59119
17 changed files with 589 additions and 47 deletions
@@ -1,58 +1,56 @@
import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:flutter/scheduler.dart';
/// How long a blinking cursor stays shown, then hidden (xterm's 600 ms is
/// close; most terminals use about half a second).
const Duration chatShellCursorBlinkPhase = Duration(milliseconds: 530);
/// The blink of a cursor the program asked to blink (DECSCUSR 1, 3 or 5).
/// It runs on a [Ticker] only while [update] says to blink: the terminal
/// is focused, the shape blinks and motion is not reduced. Otherwise the
/// cursor is steady ([visible] stays true), so an unfocused terminal or a
/// test schedules nothing. Typing or output shows it again at once
/// ([restart]), as terminals do.
/// It runs only while [update] says to blink: the terminal is focused, the
/// shape blinks and motion is not reduced. Otherwise the cursor is steady
/// ([visible] stays true), so an unfocused terminal or a test schedules
/// nothing. Typing or output shows it again at once ([restart]), as
/// terminals do.
///
/// A timer flips the phase, so a blinking cursor costs one frame per phase
/// rather than a ticker's frame on every vsync of an otherwise idle page.
class ChatShellCursorBlink {
ChatShellCursorBlink(TickerProvider vsync) {
_ticker = vsync.createTicker(_tick);
}
late final Ticker _ticker;
Duration _now = Duration.zero;
Duration _since = Duration.zero;
Timer? _timer;
/// Whether the cursor is drawn in the current phase.
final ValueNotifier<bool> visible = ValueNotifier(true);
bool get blinking => _ticker.isActive;
bool get blinking => _timer != null;
void update({required bool blink}) {
if (blink == _ticker.isActive) return;
if (blink == blinking) return;
if (blink) {
_now = Duration.zero;
_since = Duration.zero;
_ticker.start();
_start();
} else {
_ticker.stop();
_timer?.cancel();
_timer = null;
}
visible.value = true;
}
/// Shows the cursor and starts the phase again.
void restart() {
_since = _now;
visible.value = true;
if (blinking) _start();
}
void _tick(Duration elapsed) {
_now = elapsed;
final phase =
(elapsed - _since).inMicroseconds ~/
chatShellCursorBlinkPhase.inMicroseconds;
visible.value = phase.isEven;
void _start() {
_timer?.cancel();
_timer = Timer.periodic(
chatShellCursorBlinkPhase,
(_) => visible.value = !visible.value,
);
}
void dispose() {
_ticker.dispose();
_timer?.cancel();
_timer = null;
visible.dispose();
}
}
@@ -78,7 +78,6 @@ class ChatShellTerminalView extends StatefulWidget {
class _ChatShellTerminalViewState extends State<ChatShellTerminalView>
with
SingleTickerProviderStateMixin,
ChatShellTerminalFindHost<ChatShellTerminalView>,
ChatShellTerminalFeatures<ChatShellTerminalView> {
@override
@@ -18,7 +18,7 @@ import 'chat_shell_terminal_shortcuts.dart';
/// [ChatShellTerminalFindHost]). The view supplies its emulator's parts and
/// calls [initFeatures] and [disposeFeatures].
mixin ChatShellTerminalFeatures<T extends StatefulWidget>
on State<T>, TickerProvider, ChatShellTerminalFindHost<T> {
on State<T>, ChatShellTerminalFindHost<T> {
ScrollController get scroll;
GlobalKey<TerminalViewState> get viewKey;
@@ -44,7 +44,7 @@ mixin ChatShellTerminalFeatures<T extends StatefulWidget>
view: viewKey,
);
hover = ChatShellLinkHover(_terminal, viewKey);
blink = ChatShellCursorBlink(this);
blink = ChatShellCursorBlink();
_guard = ChatShellSelectionGuard(_terminal, controller);
scroll.addListener(hover.refresh);
_terminal.addListener(_terminalChanged);
@@ -7,8 +7,13 @@ import 'chat_markdown.dart';
/// A lazily built transcript disposes rows scrolled out of view, so scrolling
/// back would parse the same answer again. Blocks are immutable and depend on
/// the text alone, so one parse serves every later view of that text. Bounded
/// by entries and by held characters, evicting the least recently used first,
/// so a long streamed reply's intermediate texts cannot grow it without limit.
/// by entries and by held characters, evicting the least recently used first.
///
/// A streamed reply asks for a longer text on every delta. Each new text
/// replaces the newest entry when that entry is its prefix, so a reply holds
/// one entry while it streams instead of one per delta: its intermediate
/// texts would otherwise fill the character budget and evict every finished
/// answer, which then parse again when scrolled back into view.
class ChatMarkdownCache {
ChatMarkdownCache({this.maxEntries = 400, this.maxChars = 1000000});
@@ -28,6 +33,13 @@ class ChatMarkdownCache {
final blocks = List<MarkdownBlock>.unmodifiable(parseMarkdown(text));
parses++;
if (text.length > maxChars) return blocks;
if (_blocks.isNotEmpty) {
final newest = _blocks.keys.last;
if (newest.length < text.length && text.startsWith(newest)) {
_blocks.remove(newest);
_chars -= newest.length;
}
}
_blocks[text] = blocks;
_chars += text.length;
while (_blocks.length > maxEntries || _chars > maxChars) {