Merge: perf round 2 — long chats open with only the visible rows

This commit is contained in:
2026-11-02 00:55:37 +00:00
14 changed files with 771 additions and 58 deletions
@@ -103,27 +103,39 @@ void main() {
expect(chatRestoreOffset(savedOffset: 0, maxScrollExtent: 640), 0);
expect(chatRestoreOffset(savedOffset: 900, maxScrollExtent: 640), 640);
});
test('a bottom-anchored position above the centre stays negative', () {
double restore(double saved) => chatRestoreOffset(
savedOffset: saved,
minScrollExtent: -2000,
maxScrollExtent: 300,
);
expect(restore(-750), -750);
expect(restore(-5000), -2000);
expect(restore(900), 300);
});
});
group('store', () {
test('remembers one conversation without touching another', () {
final store = MemoryChatScrollStore();
store.write('a', 120);
store.write('b', 30);
expect(store.read('a'), 120);
expect(store.read('b'), 30);
store.write('a', (offset: 120, anchor: null));
store.write('b', (offset: -30, anchor: 'm4'));
expect(store.read('a'), (offset: 120.0, anchor: null));
expect(store.read('b'), (offset: -30.0, anchor: 'm4'));
expect(store.read('c'), isNull);
store.clear('a');
expect(store.read('a'), isNull);
expect(store.entries, {'b': 30.0});
expect(store.entries, {'b': (offset: -30.0, anchor: 'm4')});
});
test('ignores an unusable offset instead of inventing a position', () {
final store = MemoryChatScrollStore();
store.write('a', double.nan);
store.write('a', double.infinity);
store.write('a', -5);
store.write('a', (offset: double.nan, anchor: 'm1'));
store.write('a', (offset: double.infinity, anchor: null));
// Only a bottom-anchored offset counts from a centre and can be negative.
store.write('a', (offset: -5, anchor: null));
expect(store.read('a'), isNull);
});
});
@@ -0,0 +1,211 @@
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart' as widgets;
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/core/api_gateway.dart';
import 'package:pipelines_app/features/chat/conversation/chat_scroll.dart';
import 'package:pipelines_app/features/chat/conversation/chat_thread_view.dart';
import 'package:pipelines_app/features/chat/ui/transcript/chat_message_view.dart';
import 'chat_thread_fixtures.dart';
import 'chat_thread_scroll_test.dart' show host, transcriptScroll;
// The transcript opens bottom-anchored: only the rows that show are built, and
// growth on either side of the split never moves the operator's place. In the
// 800x600 test viewport the first row starts below the floating header.
const _firstRowTop = 80.0;
List<JsonMap> messages(int count, {int from = 0, int sentences = 6}) => [
for (var index = from; index < from + count; index++)
chatMessage(
'm$index',
index.isEven ? 'user' : 'assistant',
parts: [
{
'type': 'text',
'text':
'Message $index. '
'${List.filled(sentences, 'A sentence that wraps across the transcript width.').join(' ')}',
},
],
),
];
Finder row(String id) => find.byWidgetPredicate(
(widget) =>
widget is ChatMessageView &&
(widget.row['key'] == id ||
(widget.row['messageIds'] as List?)?.contains(id) == true),
skipOffstage: false,
);
Finder get thread => find.byKey(const Key('chat-thread-s'));
Future<void> dragUntil(WidgetTester tester, Finder target) async {
for (var drag = 0; drag < 80 && !tester.any(target); drag++) {
await tester.drag(thread, const Offset(0, 500));
await tester.pump();
}
await tester.pumpAndSettle();
}
Future<void> openThread(WidgetTester tester, FakeChatGateway gateway) async {
await tester.pumpWidget(
host(ChatThreadView(gateway: gateway, sessionId: 's', onBack: () {})),
);
await tester.pumpAndSettle();
}
void main() {
testWidgets('a long transcript opens at its newest message, building only '
'what shows', (tester) async {
final gateway = FakeChatGateway();
addTearDown(gateway.close);
gateway.history = (_) async => chatPage(messages(200));
final built = <String>{};
widgets.debugOnRebuildDirtyWidget = (element, _) {
final widget = element.widget;
if (widget is ChatMessageView) built.add('${widget.row['key']}');
};
addTearDown(() => widgets.debugOnRebuildDirtyWidget = null);
await openThread(tester, gateway);
widgets.debugOnRebuildDirtyWidget = null;
final scroll = transcriptScroll(tester);
expect(
scroll.position.pixels,
closeTo(scroll.position.maxScrollExtent, 0.5),
);
expect(scroll.position.minScrollExtent, lessThan(0));
final newest = tester.getRect(row('m199'));
expect(newest.bottom, lessThanOrEqualTo(600));
expect(newest.bottom, greaterThan(400));
expect(built, isNot(contains('m0')));
expect(built.length, lessThan(20));
expect(find.byKey(const Key('chat-jump-to-latest')), findsNothing);
// A reply streaming below the split is followed while at the end.
gateway.emitEvent(chatTextFrame('Streaming answer. ', messageId: 'live'));
await tester.pump();
await tester.pump();
expect(
scroll.position.pixels,
closeTo(scroll.position.maxScrollExtent, 0.5),
);
});
testWidgets('scrolling up reaches the first row with no gap above it', (
tester,
) async {
final gateway = FakeChatGateway();
addTearDown(gateway.close);
gateway.history = (_) async => chatPage(messages(40));
await openThread(tester, gateway);
await dragUntil(tester, row('m0'));
await tester.fling(thread, const Offset(0, 3000), 3000);
await tester.pumpAndSettle();
final scroll = transcriptScroll(tester);
expect(
scroll.position.pixels,
closeTo(scroll.position.minScrollExtent, 0.5),
);
expect(tester.getRect(row('m0')).top, closeTo(_firstRowTop, 1));
expect(find.byKey(const Key('chat-jump-to-latest')), findsOneWidget);
});
for (final tall in [false, true]) {
testWidgets('a short transcript still reads from the top '
'(tall newest row: $tall)', (tester) async {
final gateway = FakeChatGateway();
addTearDown(gateway.close);
// The rows above the newest one fit the viewport.
gateway.history = (_) async => chatPage([
...messages(2, sentences: 1),
...messages(1, from: 2, sentences: tall ? 60 : 1),
]);
await openThread(tester, gateway);
final scroll = transcriptScroll(tester);
expect(scroll.position.minScrollExtent, 0);
expect(
scroll.position.pixels,
closeTo(scroll.position.maxScrollExtent, 0.5),
);
if (!tall) {
expect(scroll.position.maxScrollExtent, 0);
expect(tester.getRect(row('m0')).top, closeTo(_firstRowTop, 1));
return;
}
// At its end the tall newest row shows its end; scrolling back reaches
// the first row at the top padding.
expect(tester.getRect(row('m2')).bottom, lessThanOrEqualTo(600));
await tester.fling(thread, const Offset(0, 3000), 3000);
await tester.pumpAndSettle();
expect(tester.getRect(row('m0')).top, closeTo(_firstRowTop, 1));
});
}
testWidgets('loading an older page keeps the rows the operator reads in '
'place', (tester) async {
final gateway = FakeChatGateway();
addTearDown(gateway.close);
gateway.history = (_) async =>
chatPage(messages(30, from: 30), cursor: 'older', hasMore: true);
await openThread(tester, gateway);
await dragUntil(tester, find.text('Load older'));
await tester.fling(thread, const Offset(0, 3000), 3000);
await tester.pumpAndSettle();
final held = tester.getRect(row('m30'));
gateway.history = (_) async => chatPage(messages(30));
await tester.tap(find.text('Load older'));
await tester.pumpAndSettle();
expect(find.text('Load older'), findsNothing);
expect(tester.getRect(row('m30')).top, closeTo(held.top, 0.5));
// The older page sits above, reachable by scrolling up.
await dragUntil(tester, row('m0'));
await tester.fling(thread, const Offset(0, 3000), 3000);
await tester.pumpAndSettle();
expect(tester.getRect(row('m0')).top, closeTo(_firstRowTop, 1));
});
testWidgets('a reopened conversation keeps its place after messages arrived '
'while it was closed', (tester) async {
final gateway = FakeChatGateway();
addTearDown(gateway.close);
gateway.history = (_) async => chatPage(messages(60));
final positions = MemoryChatScrollStore();
Widget view() => host(
ChatThreadView(
gateway: gateway,
sessionId: 's',
scrollPositions: positions,
onBack: () {},
),
);
await tester.pumpWidget(view());
await tester.pumpAndSettle();
for (var drag = 0; drag < 4; drag++) {
await tester.drag(thread, const Offset(0, 400));
await tester.pump();
}
await tester.pumpAndSettle();
final shown = find.byWidgetPredicate((widget) => widget is ChatMessageView);
final top = shown.evaluate().first.widget as ChatMessageView;
final id = '${top.row['key']}';
final held = tester.getRect(row(id));
expect(positions.read('s')!.anchor, isNotNull);
await tester.pumpWidget(host(const SizedBox()));
gateway.history = (_) async => chatPage(messages(64));
await tester.pumpWidget(view());
await tester.pumpAndSettle();
expect(tester.getRect(row(id)).top, closeTo(held.top, 1));
expect(find.text('Jump to latest · 4 new messages'), findsNothing);
expect(find.byKey(const Key('chat-jump-to-latest')), findsOneWidget);
});
}
@@ -35,8 +35,14 @@ JsonMap tallPage() => chatPage([
),
]);
ScrollController transcriptScroll(WidgetTester tester) =>
tester.widget<ListView>(find.byKey(const Key('chat-thread-s'))).controller!;
ScrollController transcriptScroll(WidgetTester tester) => tester
.widget<CustomScrollView>(
find.descendant(
of: find.byKey(const Key('chat-thread-s')),
matching: find.byType(CustomScrollView),
),
)
.controller!;
void main() {
testWidgets('a streaming transcript stays pinned to its newest end', (
@@ -181,14 +187,14 @@ void main() {
);
await tester.pumpAndSettle();
final saved = scroll.position.pixels;
expect(saved, greaterThan(0));
expect(positions.read('s'), closeTo(saved, 0.5));
expect(saved, lessThan(scroll.position.maxScrollExtent - 300));
expect(positions.read('s')!.offset, closeTo(saved, 0.5));
// Leaving the conversation disposes its view but not the destination's
// store.
await tester.pumpWidget(host(const SizedBox()));
await tester.pumpAndSettle();
expect(positions.read('s'), closeTo(saved, 0.5));
expect(positions.read('s')!.offset, closeTo(saved, 0.5));
await tester.pumpWidget(
host(
@@ -0,0 +1,68 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/core/api_gateway.dart';
import 'package:pipelines_app/features/chat/conversation/chat_transcript_anchor.dart';
JsonMap user(String id) => {'kind': 'user', 'key': id};
JsonMap assistant(List<String> ids, {bool streaming = false}) => {
'kind': 'assistant',
'key': ids.isEmpty ? 'chat-pending-response' : ids.first,
'messageIds': ids,
if (streaming) 'streaming': true,
};
void main() {
test('splits before the newest message row', () {
final rows = [
user('u1'),
assistant(['a2', 'a3']),
user('u4'),
];
expect(chatTranscriptAnchor(rows), 'a3');
expect(chatTranscriptAnchorIndex(rows, 'a3'), 1);
// Any message the folded turn holds finds it, as paging prepends into it.
expect(chatTranscriptAnchorIndex(rows, 'a2'), 1);
});
test('a streaming reply and the rows after it stay below the centre', () {
// A queued follow-up can be recorded after the reply still arriving.
final rows = [
user('u1'),
assistant(['a2']),
user('u3'),
assistant(['a4'], streaming: true),
user('u5'),
];
expect(chatTranscriptAnchor(rows), 'u3');
});
test('cards, activity lines and the pending shell are never the anchor', () {
final rows = [
user('u1'),
{'kind': 'activity', 'key': 'chat-activity-a2'},
assistant(['a2']),
assistant(const [], streaming: true),
{'kind': 'permission', 'key': 'chat-permission-p1'},
];
// The newest message row is a2, so the anchor is the row before it.
expect(chatTranscriptAnchor(rows), 'u1');
});
test('fewer than two message rows stay top-anchored', () {
expect(chatTranscriptAnchor(const []), isNull);
expect(chatTranscriptAnchor([user('u1')]), isNull);
expect(
chatTranscriptAnchor([
user('u1'),
{'kind': 'question', 'key': 'chat-question-q1'},
]),
isNull,
);
});
test('a missing anchor reports no split', () {
final rows = [user('u1'), user('u2')];
expect(chatTranscriptAnchorIndex(rows, null), -1);
expect(chatTranscriptAnchorIndex(rows, 'gone'), -1);
});
}
@@ -9,9 +9,11 @@ import 'package:pipelines_app/features/chat/ui/transcript/chat_message_view.dart
import '../conversation/chat_thread_fixtures.dart';
/// Measurement harness: streams 200 text deltas into a 300-message transcript
/// and counts message builds, markdown builds/parses and per-delta frame time;
/// then scrolls the long transcript up and back down.
/// Measurement harness: opens 300- and 1000-message transcripts and counts the
/// rows built and parsed until the newest message shows; streams 200 text
/// deltas into a 300-message transcript and counts message builds, markdown
/// builds/parses and per-delta frame time; then scrolls the long transcript up
/// and back down.
const _messages = 300;
const _deltas = 200;
@@ -23,8 +25,8 @@ String _answer(int index) =>
'\n```\n\n'
'- one\n- two\n- three\n';
List<JsonMap> _transcript() => [
for (var index = 0; index < _messages; index++)
List<JsonMap> _transcript([int count = _messages]) => [
for (var index = 0; index < count; index++)
// The newest message is the operator's, so the reply opens a new turn.
index.isOdd
? chatMessage(
@@ -77,6 +79,92 @@ String _stats(List<int> samples) {
void main() {
setUp(chatMarkdownCache.clear);
for (final count in [300, 1000]) {
testWidgets('opening a $count-message transcript builds what shows', (
tester,
) async {
tester.view.physicalSize = const Size(1000, 1600);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.reset);
final gateway = FakeChatGateway();
addTearDown(gateway.close);
gateway.history = (_) async => chatPage(_transcript(count));
var messageBuilds = 0;
widgets.debugOnRebuildDirtyWidget = (element, _) {
if (element.widget is ChatMessageView) messageBuilds++;
};
addTearDown(() => widgets.debugOnRebuildDirtyWidget = null);
// The newest message is the operator's question.
final newestId = 'u${count - 1}';
final newest = find.byWidgetPredicate(
(widget) =>
widget is ChatMessageView &&
(widget.row['key'] == newestId ||
widget.row['message']?['id'] == newestId),
);
final watch = Stopwatch()..start();
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: ChatThreadView(
gateway: gateway,
sessionId: 's',
onBack: () {},
),
),
),
);
// From the first frame that builds a message row to the frame that shows
// the newest one: the open cost the operator waits through.
int? firstRowFrame;
int? shownFrame;
var openUs = 0;
var maxFrameUs = 0;
bool newestShows() {
if (newest.evaluate().isEmpty) return false;
final rect = tester.getRect(newest);
return rect.bottom > 0 && rect.top < 1600;
}
final frame = Stopwatch();
for (var index = 1; index <= 300 && shownFrame == null; index++) {
final rowsBefore = messageBuilds;
frame
..reset()
..start();
await tester.pump(const Duration(milliseconds: 16));
frame.stop();
if (firstRowFrame == null && messageBuilds > rowsBefore) {
firstRowFrame = index;
}
if (firstRowFrame != null) {
openUs += frame.elapsedMicroseconds;
if (frame.elapsedMicroseconds > maxFrameUs) {
maxFrameUs = frame.elapsedMicroseconds;
}
}
if (newestShows()) shownFrame = index;
}
final rowsAtShow = messageBuilds;
final parsesAtShow = chatMarkdownCache.parses;
await tester.pumpAndSettle();
watch.stop();
widgets.debugOnRebuildDirtyWidget = null;
// ignore: avoid_print
print(
'PERF open$count: rowsBuiltAtShow=$rowsAtShow '
'parsesAtShow=$parsesAtShow '
'framesToShow=${shownFrame == null ? null : shownFrame - firstRowFrame! + 1} '
'openMs=${(openUs / 1000).toStringAsFixed(1)} '
'maxFrameMs=${(maxFrameUs / 1000).toStringAsFixed(1)} '
'settled rows=$messageBuilds parses=${chatMarkdownCache.parses} '
'wallMs=${watch.elapsedMilliseconds}',
);
expect(shownFrame, isNotNull);
expect(tester.getRect(newest).bottom, lessThanOrEqualTo(1600));
});
}
testWidgets('streaming deltas rebuild only the live turn', (tester) async {
final gateway = await _open(tester);
var messageBuilds = 0;