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
@@ -302,7 +302,10 @@ void main() {
addTearDown(tester.view.resetDevicePixelRatio);
final channel = FakeChannel()..readyCompleter.complete();
final client = PipelinesClient(channelFactory: (_) => channel);
final client = PipelinesClient(
replaceSilentSocket: false,
channelFactory: (_) => channel,
);
final provider = PlatformRouteInformationProvider(
initialRouteInformation: RouteInformation(uri: Uri.parse('/chat/s1')),
);
@@ -410,7 +413,10 @@ void main() {
addTearDown(tester.view.resetDevicePixelRatio);
final channel = FakeChannel()..readyCompleter.complete();
final client = PipelinesClient(channelFactory: (_) => channel);
final client = PipelinesClient(
replaceSilentSocket: false,
channelFactory: (_) => channel,
);
final provider = PlatformRouteInformationProvider(
initialRouteInformation: RouteInformation(uri: Uri.parse('/chat/old')),
);
@@ -492,7 +498,10 @@ void main() {
addTearDown(tester.view.reset);
final channel = FakeChannel()..readyCompleter.complete();
final client = PipelinesClient(channelFactory: (_) => channel);
final client = PipelinesClient(
replaceSilentSocket: false,
channelFactory: (_) => channel,
);
final provider = PlatformRouteInformationProvider(
initialRouteInformation: RouteInformation(uri: Uri.parse('/chat')),
);
@@ -702,7 +711,10 @@ void main() {
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.reset);
final channel = FakeChannel()..readyCompleter.complete();
final client = PipelinesClient(channelFactory: (_) => channel);
final client = PipelinesClient(
replaceSilentSocket: false,
channelFactory: (_) => channel,
);
final provider = PlatformRouteInformationProvider(
initialRouteInformation: RouteInformation(uri: Uri.parse('/chat')),
);
@@ -784,7 +796,10 @@ void main() {
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.reset);
final channel = FakeChannel()..readyCompleter.complete();
final client = PipelinesClient(channelFactory: (_) => channel);
final client = PipelinesClient(
replaceSilentSocket: false,
channelFactory: (_) => channel,
);
final provider = PlatformRouteInformationProvider(
initialRouteInformation: RouteInformation(uri: Uri.parse('/chat')),
);
@@ -839,7 +854,10 @@ void main() {
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.reset);
final channel = FakeChannel()..readyCompleter.complete();
final client = PipelinesClient(channelFactory: (_) => channel);
final client = PipelinesClient(
replaceSilentSocket: false,
channelFactory: (_) => channel,
);
final provider = PlatformRouteInformationProvider(
initialRouteInformation: RouteInformation(uri: Uri.parse('/chat')),
);
@@ -908,7 +926,10 @@ void main() {
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.reset);
final channel = FakeChannel()..readyCompleter.complete();
final client = PipelinesClient(channelFactory: (_) => channel);
final client = PipelinesClient(
replaceSilentSocket: false,
channelFactory: (_) => channel,
);
final provider = PlatformRouteInformationProvider(
initialRouteInformation: RouteInformation(uri: Uri.parse('/chat/a')),
);
@@ -957,7 +978,10 @@ void main() {
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.reset);
final channel = FakeChannel()..readyCompleter.complete();
final client = PipelinesClient(channelFactory: (_) => channel);
final client = PipelinesClient(
replaceSilentSocket: false,
channelFactory: (_) => channel,
);
final provider = PlatformRouteInformationProvider(
initialRouteInformation: RouteInformation(uri: Uri.parse('/chat')),
);
@@ -15,7 +15,10 @@ void main() {
) async {
SharedPreferences.setMockInitialValues({});
final channel = FakeChannel()..readyCompleter.complete();
final client = PipelinesClient(channelFactory: (_) => channel);
final client = PipelinesClient(
replaceSilentSocket: false,
channelFactory: (_) => channel,
);
final provider = PlatformRouteInformationProvider(
initialRouteInformation: RouteInformation(uri: Uri.parse('/runs')),
);
+4 -1
View File
@@ -37,7 +37,10 @@ void main() {
PipelinesApp(
androidSupported: false,
server: webServer(),
client: PipelinesClient(channelFactory: (_) => channel),
client: PipelinesClient(
replaceSilentSocket: false,
channelFactory: (_) => channel,
),
routeInformationProvider: provider,
),
);
@@ -31,7 +31,10 @@ mount(
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.reset);
final channel = FakeChannel()..readyCompleter.complete();
final client = PipelinesClient(channelFactory: (_) => channel);
final client = PipelinesClient(
replaceSilentSocket: false,
channelFactory: (_) => channel,
);
final provider = PlatformRouteInformationProvider(
initialRouteInformation: RouteInformation(uri: Uri.parse(route)),
);
@@ -16,7 +16,10 @@ import 'templates_wiring_test.dart' show pumpApp, readTemplates;
void main() {
testWidgets('the gallery and detail scroll behind the bar', (tester) async {
final channel = FakeChannel()..readyCompleter.complete();
final client = PipelinesClient(channelFactory: (_) => channel);
final client = PipelinesClient(
replaceSilentSocket: false,
channelFactory: (_) => channel,
);
final provider = PlatformRouteInformationProvider(
initialRouteInformation: RouteInformation(uri: Uri.parse('/pipelines')),
);
@@ -135,6 +135,7 @@ void main() {
final channel = FakeChannel()..readyCompleter.complete();
var sockets = 0;
final client = PipelinesClient(
replaceSilentSocket: false,
channelFactory: (_) {
sockets++;
return channel;
@@ -229,6 +230,7 @@ void main() {
final channel = FakeChannel()..readyCompleter.complete();
var sockets = 0;
final client = PipelinesClient(
replaceSilentSocket: false,
channelFactory: (_) {
sockets++;
return channel;
@@ -14,6 +14,10 @@ void reply(FakeChannel channel, {int index = 0, String type = 'pong'}) =>
jsonEncode({'id': sent(channel, index)['id'], 'type': type}),
);
extension<T> on T {
void let(void Function(T) f) => f(this);
}
void main() {
test(
'requests serialize through replies, errors do not poison queue',
@@ -51,14 +55,126 @@ void main() {
requestTimeout: const Duration(milliseconds: 10),
);
await client.connect(Uri.parse('ws://example.test'));
await expectLater(client.call('ping'), throwsA(isA<TimeoutException>()));
final late = client.frames.first;
final call = client.call('ping');
await flush();
// The socket is alive (a pushed frame arrives while the call waits),
// so its first timeout keeps it.
channel.incoming.add(jsonEncode({'type': 'run_event'}));
await expectLater(call, throwsA(isA<TimeoutException>()));
expect(client.state.kind, ConnectionKind.connected);
final late = client.frames.firstWhere((f) => f['type'] == 'pong');
reply(channel);
expect((await late)['type'], 'pong');
await client.dispose();
},
);
test('a timeout on a silent socket replaces it and fails the queue at once '
'instead of timing out each queued request in turn', () async {
final channels = <FakeChannel>[];
final client = PipelinesClient(
channelFactory: (_) =>
(FakeChannel()..readyCompleter.complete())..let(channels.add),
requestTimeout: const Duration(milliseconds: 20),
backoffBase: const Duration(milliseconds: 5),
);
await client.connect(Uri.parse('ws://example.test'));
final first = client.call('ping');
final queued = client.call('list_runs');
final reconnected = client.states
.where((s) => s.kind == ConnectionKind.connected)
.first;
final clock = Stopwatch()..start();
await expectLater(first, throwsA(isA<TimeoutException>()));
await expectLater(queued, throwsStateError);
expect(clock.elapsedMilliseconds, lessThan(200));
await reconnected.timeout(const Duration(seconds: 2));
expect(channels, hasLength(2));
expect(channels.first.outgoing, hasLength(1));
await client.dispose();
});
test('a second timeout in a row replaces a socket that still pushes '
'frames: its request lane is stuck', () async {
final channels = <FakeChannel>[];
final client = PipelinesClient(
channelFactory: (_) =>
(FakeChannel()..readyCompleter.complete())..let(channels.add),
requestTimeout: const Duration(milliseconds: 20),
backoffBase: const Duration(milliseconds: 5),
);
await client.connect(Uri.parse('ws://example.test'));
for (var i = 0; i < 2; i++) {
final call = client.call('ping');
await flush();
channels.first.incoming.add(jsonEncode({'type': 'run_event'}));
await expectLater(call, throwsA(isA<TimeoutException>()));
if (i == 0) expect(client.state.kind, ConnectionKind.connected);
}
await client.states
.where((s) => s.kind == ConnectionKind.connected)
.first
.timeout(const Duration(seconds: 2));
expect(channels, hasLength(2));
await client.dispose();
});
test(
'the first request after a quiet spell is preceded by a probe',
() async {
final channel = FakeChannel()..readyCompleter.complete();
final client = PipelinesClient(
channelFactory: (_) => channel,
quietProbeAfter: const Duration(milliseconds: 20),
probeTimeout: const Duration(seconds: 5),
);
await client.connect(Uri.parse('ws://example.test'));
final first = client.call('list_runs');
await flush();
expect(channel.outgoing, hasLength(1), reason: 'not quiet yet');
reply(channel, type: 'runs');
await first;
await Future<void>.delayed(const Duration(milliseconds: 40));
final second = client.call('list_runs');
await flush();
expect(sent(channel, 1)['type'], 'ping');
expect(sent(channel, 2)['type'], 'list_runs');
reply(channel, index: 2, type: 'runs');
await second;
await client.dispose();
},
);
test(
'a probe keeps a socket that answers and replaces one that does not',
() async {
final channels = <FakeChannel>[];
final client = PipelinesClient(
channelFactory: (_) =>
(FakeChannel()..readyCompleter.complete())..let(channels.add),
probeTimeout: const Duration(milliseconds: 20),
backoffBase: const Duration(milliseconds: 5),
);
final frames = <Map<String, dynamic>>[];
client.frames.listen(frames.add);
await client.connect(Uri.parse('ws://example.test'));
client.probe();
final probe = sent(channels.first);
expect(probe['type'], 'ping');
reply(channels.first);
await Future<void>.delayed(const Duration(milliseconds: 40));
expect(channels, hasLength(1));
expect(frames, isEmpty, reason: 'the probe reply is not a frame');
client.probe();
await client.states
.where((s) => s.kind == ConnectionKind.connected)
.first
.timeout(const Duration(seconds: 2));
expect(channels, hasLength(2));
await client.dispose();
},
);
test(
'replacement fails queued calls rather than sending on new socket',
() async {
@@ -67,4 +67,19 @@ void main() {
cache.blocksFor(long);
expect(cache.parses, 6);
});
test('ChatMarkdownCache holds one entry for a streamed reply, so finished '
'answers are not evicted by its intermediate texts', () {
final cache = ChatMarkdownCache(maxEntries: 4, maxChars: 5000);
final finished = cache.blocksFor('# Finished answer');
var streamed = '';
for (var delta = 0; delta < 200; delta++) {
streamed += 'word$delta ';
cache.blocksFor(streamed);
}
expect(cache.blocksFor('# Finished answer'), same(finished));
final parses = cache.parses;
cache.blocksFor(streamed);
expect(cache.parses, parses, reason: 'the final text stays cached');
});
}
@@ -22,16 +22,22 @@ void main() {
expect(shown(tester, harness), isTrue);
await tester.pump(_phase + const Duration(milliseconds: 10));
expect(shown(tester, harness), isFalse);
// Between phase flips nothing asks for a frame: one frame per phase,
// not one per vsync.
expect(tester.binding.hasScheduledFrame, isFalse);
await tester.pump(_phase);
expect(shown(tester, harness), isTrue);
expect(tester.binding.hasScheduledFrame, isFalse);
// Output shows it again and restarts the phase.
await tester.pump(_phase);
expect(shown(tester, harness), isFalse);
await harness.write(tester, 'x');
expect(shown(tester, harness), isTrue);
// Unfocused: steady, and the ticker stopped.
// Unfocused: steady, and the blink stopped (the unfocused cursor's
// own repaint is the last frame).
harness.view(tester).focusNode!.unfocus();
await tester.pump();
await tester.pump(_phase * 2);
expect(shown(tester, harness), isTrue);
expect(tester.binding.hasScheduledFrame, isFalse);
});