Settle bottom-anchored restores, flush open timings in the probe, record numbers

This commit is contained in:
2026-11-02 00:55:03 +00:00
parent a5f0eb3811
commit c74065dc48
5 changed files with 123 additions and 33 deletions
@@ -203,6 +203,7 @@ class _ChatThreadViewState extends State<ChatThreadView> {
double? _restoreOffset;
bool _restoring = false;
double _restoreMeasuredExtent = 0;
double _restoreMeasuredStart = 0;
/// True during a pointer/touch/wheel scroll. Programmatic placement and
/// follow jumps never set it, so they cannot release the follow intent.
@@ -434,6 +435,7 @@ class _ChatThreadViewState extends State<ChatThreadView> {
_restoring = false;
_restoreOffset = null;
_restoreMeasuredExtent = 0;
_restoreMeasuredStart = 0;
_lastOffset = 0;
} else if (oldWidget.syncToken != widget.syncToken) {
// The socket was replaced: conditions recorded before the gap are stale,
@@ -585,6 +587,7 @@ class _ChatThreadViewState extends State<ChatThreadView> {
if (_scroll.position.pixels != target) _scroll.jumpTo(target);
_lastOffset = target;
_restoreMeasuredExtent = _scroll.position.maxScrollExtent;
_restoreMeasuredStart = _scroll.position.minScrollExtent;
_distanceFromBottom = chatBottomDistance(
maxScrollExtent: _restoreMeasuredExtent,
pixels: _scroll.position.pixels,
@@ -607,8 +610,12 @@ class _ChatThreadViewState extends State<ChatThreadView> {
}
if (_restoring) {
final measuredBefore = _restoreMeasuredExtent;
final startBefore = _restoreMeasuredStart;
_applyRestore();
if (_scroll.position.maxScrollExtent == measuredBefore) {
// Bottom-anchored, the older rows' estimated extent above the centre
// settles too, and a saved offset there is clamped to it.
if (_scroll.position.maxScrollExtent == measuredBefore &&
_scroll.position.minScrollExtent == startBefore) {
// The measured extent stopped changing: the restored offset is the
// operator's real position again, so follow intent can be judged.
_restoring = false;
+84
View File
@@ -0,0 +1,84 @@
# Chat transcript open performance
Performance round 2 on opening a long chat. Started from measurements; no
intended behaviour change apart from the differences listed below.
Follows `notes/chat-stream-perf.md`.
## Finding
The transcript was a top-anchored `ListView`, and the view jumped to its end
after the first layout. A list can reach its end only by laying out every row
in between, so opening a 300-message chat built 301 message rows and parsed
150 markdown answers in one frame before the newest message showed.
## Change
- `ChatTranscriptScrollView` (`lib/features/chat/conversation/`) is a
`CustomScrollView` with a centre. Rows up to an anchor row sit in a sliver
that grows upwards from the centre. Later rows, plus queued, unsent and
command lines, sit in a sliver that grows downwards. With `anchor: 1`, offset
0 puts the centre at the viewport's bottom, so opening lays out only the
rows on screen.
- `chatTranscriptAnchor` (`chat_transcript_anchor.dart`) picks the message row
before the newest message row, and before any reply that is still
streaming. The anchor is chosen once, when the history arrives, and stays
fixed while the chat is open. Growth on either side therefore never moves
the other side: older pages are prepended above, and replies stream below.
- A transcript whose older side fits in the viewport (where `minScrollExtent`
is 0 after the first layout) switches back to top-anchored, exactly as
before. Short chats still start at the top with no gap above them.
- Saved positions are now `({offset, anchor})`. An offset is restored only
against the same anchor message id. If that row is gone, the chat opens at
its newest message. While a restore settles, the code now also waits for
`minScrollExtent` to settle.
- Follow, the jump control, the arrival count, keyboard and composer insets,
and per-message selection use the same logic as before. The end of the
transcript is still `maxScrollExtent`.
## Numbers
Widget harness `test/features/chat/perf/chat_stream_rebuild_perf_test.dart`:
debug VM, 1000x1600 viewport, two runs each. "Open" covers the frames from
the first one that builds a message row to the one where the newest message
shows.
| | before | after |
| --- | --- | --- |
| open 300: rows built / markdown parses | 301 / 150 | 4 / 2 |
| open 300: frames / frame time sum / longest frame | 30 / 1212–1301 ms / 1090–1184 ms | 2 / 99–113 ms / 95–109 ms |
| open 1000: rows built / markdown parses | 1001 / 500 | 4 / 2 |
| open 1000: frames / frame time sum / longest frame | 38 / 2508–2616 ms / 2465–2566 ms | 2 / 47–54 ms / 44–51 ms |
| stream 200 deltas (300 msgs): message builds | 200 | 200 |
| stream: frame mean (debug) | 5.5–5.9 ms | 7.2–7.8 ms (build counts by widget type unchanged; not seen in release) |
| scroll 80 drags: parses | 0 | 22 (rows not built at open are parsed the first time they show) |
| scroll: frame mean | 5.5–6.0 ms | 5.4–6.4 ms |
Release web probe `node tool/chat_perf_probe.mjs <build> <n> 200`, built from
`tool/chat_composer_probe_entry.dart`. Headless Chrome rasterises in software.
Before and after runs alternated. The new open phase counts the frames that do
work after the history reply (`PERF_HISTORY`).
| | before | after |
| --- | --- | --- |
| open 300: build sum (longest frame) | 608–1314 ms (590–1289) | 12.5–24 ms (5.8–11.9) |
| open 300: total frame span sum | 1012–1878 ms | 218–490 ms |
| open 1000: build sum (longest frame) | 1585–2945 ms (1565–2925) | 6.3–13.8 ms (4.8–8.5) |
| open 1000: total frame span sum | 2101–3792 ms | 99–289 ms |
| streaming build mean, 300 / 1000 | 6.1–7.2 / 11.3–16.5 ms | 5.9–12.0 / 11.5–19.5 ms (noise) |
| scrolling build mean, 300 / 1000 | 1.6–2.5 / 1.7–2.6 ms | 2.3–5.1 / 2.0–3.3 ms |
For 300 messages, two before runs are used (the first overlapped another
probe). The fixture's `_PlatformViewPlaceholderBox` null-check error, logged
by the old layout on web, no longer appears. Browser captures of the
300-message open, the scroll back, the short chat and the 12-message chat look
the same before and after.
## Behaviour differences
- **Load older** keeps the rows the operator was reading in place, and the
new page appears above them. Before, the view stayed at the top, so the
operator landed on the oldest rows of the new page.
- When the viewport shrinks while the operator reads above the end (keyboard,
resize), the bottom of the view stays put instead of the top.
- A transcript replaced by a repair that no longer contains the anchor row is
split again. A view that is not following can then move.
@@ -5,6 +5,7 @@ import 'package:shared_preferences/shared_preferences.dart';
import 'package:pipelines_app/application/app_route.dart';
import 'package:pipelines_app/application/app_router.dart';
import 'package:pipelines_app/core/api_gateway.dart';
import 'package:pipelines_app/features/chat/conversation/chat_transcript_scroll_view.dart';
import 'package:pipelines_app/features/chat/screen.dart';
import 'package:pipelines_app/shell/adaptive_shell.dart';
import 'package:pipelines_app/shell/destination.dart';
@@ -130,17 +131,16 @@ void main() {
reason: 'Use the stock Navigator Back action without a chat override',
);
final transcript = find.byKey(const ValueKey('chat-thread-s'));
final scroll = tester.widget<ListView>(transcript).controller!;
await tester.drag(
transcript,
Offset(0, scroll.position.maxScrollExtent + 500),
);
final scroll = tester
.widget<ChatTranscriptScrollView>(transcript)
.controller;
// A long transcript is bottom-anchored: older rows sit above offset 0.
final span =
scroll.position.maxScrollExtent - scroll.position.minScrollExtent;
await tester.drag(transcript, Offset(0, span + 500));
await tester.pumpAndSettle();
expect(scroll.offset, closeTo(scroll.position.minScrollExtent, 1));
await tester.drag(
transcript,
Offset(0, -scroll.position.maxScrollExtent - 500),
);
await tester.drag(transcript, Offset(0, -span - 500));
await tester.pumpAndSettle();
expect(scroll.offset, closeTo(scroll.position.maxScrollExtent, 1));
// A continuously animated processing control must not keep Back open.
+8
View File
@@ -52,6 +52,14 @@ void markChatPerfHistory() {
if (chatPerfMessages == 0) return;
// ignore: avoid_print
print('PERF_HISTORY');
// The web engine hands timings over in batches at the end of a frame, so a
// quiet page can hold the open's last frames back; two late frames flush
// them. Each costs well under a millisecond and the probe ignores them.
for (final ms in [2500, 2700]) {
Timer(Duration(milliseconds: ms), () {
WidgetsBinding.instance.scheduleFrame();
});
}
}
/// Starts the frame report and, when asked, the streamed reply through [emit].
+14 -23
View File
@@ -6,8 +6,8 @@
// then:
// node tool/chat_perf_probe.mjs build/chat-perf [messages=300] [deltas=200]
//
// Three phases, each on a fresh page: open the transcript (the frames after the
// history reply until the page is quiet), wheel-scroll it up and back down,
// Three phases, each on a fresh page: open the transcript (the frames that do
// work after the history reply), wheel-scroll it up and back down,
// then stream deltas into a new reply. Flutter's own FrameTiming (build,
// raster, total) arrives as console lines from tool/chat_perf_fixture.dart.
// Prints one JSON report. Headless Chrome rasterises in software, so raster
@@ -59,10 +59,10 @@ async function phase(origin, query, drive) {
if (recording) frames.push(text.split(' ').slice(1).map(Number));
} else if (text.startsWith('PERF_')) {
marks.push(text);
if (text === 'PERF_HISTORY') historyAt = { at: Date.now(), frame: allFrames.length };
if (text === 'PERF_HISTORY') historyAt = { frame: allFrames.length };
}
if (text.startsWith('PERF_FRAME ')) {
allFrames.push({ at: Date.now(), f: text.split(' ').slice(1).map(Number) });
allFrames.push({ f: text.split(' ').slice(1).map(Number) });
}
});
const opened = Date.now();
@@ -90,28 +90,17 @@ async function phase(origin, query, drive) {
const server = await serveBuild(dir);
try {
const open = await phase(server.origin, `perf=${messages}`, async ({ opened }) => {
// The page is quiet once no frame has arrived for a second.
// The fixture flushes the timings 2.5 s after the history reply.
const end = Date.now() + 20000;
for (;;) {
const { historyAt, allFrames } = opened();
const last = allFrames.at(-1)?.at ?? 0;
if (historyAt && Date.now() - Math.max(last, historyAt.at) > 1000) break;
if (Date.now() > end) break;
await delay(100);
}
while (!opened().historyAt && Date.now() < end) await delay(100);
await delay(3500);
const { historyAt, allFrames } = opened();
if (!historyAt) return { open: null };
const after = allFrames.slice(historyAt.frame);
const sum = (i) => +(after.reduce((a, x) => a + x.f[i], 0) / 1000).toFixed(1);
return {
open: {
...stats(after.map((x) => x.f)),
buildSumMs: sum(0),
totalSumMs: sum(2),
// Wall time from the history reply to the last frame of the burst.
settleMs: after.length ? after.at(-1).at - historyAt.at : 0,
},
};
// Frames that did work after the history reply: the cost of opening. The
// fixture's flush frames do none.
const after = allFrames.slice(historyAt.frame).map((x) => x.f).filter((f) => f[0] >= 1000);
const sum = (i) => +(after.reduce((a, f) => a + f[i], 0) / 1000).toFixed(1);
return { open: { ...stats(after), buildSumMs: sum(0), totalSumMs: sum(2) } };
});
const scroll = await phase(server.origin, `perf=${messages}`, async ({ page, start, stop }) => {
start();
@@ -139,3 +128,5 @@ try {
} finally {
await server.close();
}
// A keep-alive connection can hold the server open after close.
process.exit(0);