diff --git a/app/lib/features/chat/conversation/chat_row_keys.dart b/app/lib/features/chat/conversation/chat_row_keys.dart index 8415982a..4a95d3e8 100644 --- a/app/lib/features/chat/conversation/chat_row_keys.dart +++ b/app/lib/features/chat/conversation/chat_row_keys.dart @@ -59,7 +59,14 @@ class ChatRowKeys { return keys; } - static List _messageIds(JsonMap row) { + /// A row's ids, held per row object: rows are values (see + /// `transcriptRows`), and an unchanged turn is the same row across builds. + static final Expando> _idsOf = Expando('chat row ids'); + + static List _messageIds(JsonMap row) => + _idsOf[row] ??= _computeMessageIds(row); + + static List _computeMessageIds(JsonMap row) { // A sent message is the same row before and after the server's copy // replaces the local one, even if the copy carries another id. final message = row['message']; diff --git a/app/lib/features/chat/conversation/chat_thread_controller.dart b/app/lib/features/chat/conversation/chat_thread_controller.dart index 99ae866d..c301434f 100644 --- a/app/lib/features/chat/conversation/chat_thread_controller.dart +++ b/app/lib/features/chat/conversation/chat_thread_controller.dart @@ -414,10 +414,7 @@ class ChatThreadController extends ChangeNotifier { // A frame for a cached message is fresh state the next read must not drop. final changed = body['message_id']; if (changed != null) _untouchedCached?.remove(changed); - _messages = (next['messages'] as List? ?? const []) - .whereType() - .map((message) => Map.from(message)) - .toList(); + _messages = chatMessageList(next['messages']); _running = next['running'] == true; if (!_running) { _liveAssistantMessageId = null; diff --git a/app/lib/features/chat/conversation/chat_thread_view.dart b/app/lib/features/chat/conversation/chat_thread_view.dart index 2ee2675e..b22cfbdc 100644 --- a/app/lib/features/chat/conversation/chat_thread_view.dart +++ b/app/lib/features/chat/conversation/chat_thread_view.dart @@ -234,6 +234,12 @@ class _ChatThreadViewState extends State { ChatShellRunsController(widget.gateway, widget.sessionId) ..addListener(_reportFinishedRuns); late ChatRowKeys _rowKeys = ChatRowKeys(); + // Keeps the rows of unchanged turns across builds; a streamed delta only + // re-derives the live turn. + ChatTranscriptRows _transcriptRows = ChatTranscriptRows(); + // The last [_messageIds] and the message list it was read from. + List? _idsSource; + List _ids = const []; final _sentEntrances = ChatSentEntrances(); final _rowWidgets = ChatRowWidgets(); @@ -711,6 +717,7 @@ class _ChatThreadViewState extends State { _controller = _attach(); _composer = _attachComposer(); _rowKeys = ChatRowKeys(); + _transcriptRows = ChatTranscriptRows(); _rowWidgets.clear(); _chrome.clear(); _dismissals = ChatNoticeDismissals(); @@ -797,10 +804,21 @@ class _ChatThreadViewState extends State { /// The ids of the transcript's messages in view order. Cards are conversation /// state, not messages, so a pending approval does not count as an arrival. - List _messageIds() => [ - for (final message in _visibleMessages()) - if ('${message['id'] ?? ''}'.trim().isNotEmpty) '${message['id']}'.trim(), - ]; + /// + /// The controller replaces its message list on every change, so the ids are + /// read once per list rather than on every call within a build. + List _messageIds() { + final messages = _visibleMessages(); + if (!identical(messages, _idsSource)) { + _idsSource = messages; + _ids = List.unmodifiable([ + for (final message in messages) + if ('${message['id'] ?? ''}'.trim().isNotEmpty) + '${message['id']}'.trim(), + ]); + } + return _ids; + } String? _newestMessageId() { final ids = _messageIds(); @@ -1119,7 +1137,7 @@ class _ChatThreadViewState extends State { groupSettledQuestions( groupSettledPermissions( withChatShownItems( - transcriptRows( + _transcriptRows.rows( messages, trailing: cards, running: diff --git a/app/lib/features/chat/domain/chat_shown_items.dart b/app/lib/features/chat/domain/chat_shown_items.dart index 1757ad7d..9297abb6 100644 --- a/app/lib/features/chat/domain/chat_shown_items.dart +++ b/app/lib/features/chat/domain/chat_shown_items.dart @@ -72,7 +72,39 @@ int? _rowTime(JsonMap row) { final own = _createdAt(row); if (own != null) return own; final message = row['message']; - return message is Map ? _createdAt(Map.from(message)) : null; + if (message is! Map) return null; + return _createdAt( + message is JsonMap ? message : Map.from(message), + ); +} + +// Derived per input object. Rows and items are values that are never changed +// in place (see [transcriptRows]), so an unchanged turn keeps one derived row +// object across builds instead of an equal copy per build. +final Expando> _withoutTools = Expando('chat row without tools'); +final Expando _shownRows = Expando('chat shown row'); + +/// [row] without its represented Pipelines tool calls: the row itself when it +/// has none, nothing when they were all it held. +List _withoutToolCalls(JsonMap row) { + final parts = row['parts']; + if (row['kind'] != 'assistant' || parts is! List) return [row]; + final kept = [ + for (final part in parts.whereType()) + if (!chatPipelinesToolRepresented( + part is JsonMap ? part : Map.from(part), + )) + part, + ]; + if (kept.length == parts.length) return [row]; + if (kept.isNotEmpty || + row['streaming'] == true || + row['activityRecords'] != null) { + return [ + {...row, 'parts': kept}, + ]; + } + return const []; } /// [rows] (from [transcriptRows]) with the Pipelines tool calls removed from @@ -88,42 +120,26 @@ List withChatShownItems( List items, { required bool hasMore, }) { - final out = []; - for (final row in rows) { - final parts = row['parts']; - if (row['kind'] != 'assistant' || parts is! List) { - out.add(row); - continue; - } - final kept = [ - for (final part in parts.whereType()) - if (!chatPipelinesToolRepresented(Map.from(part))) - part, - ]; - if (kept.length == parts.length) { - out.add(row); - } else if (kept.isNotEmpty || - row['streaming'] == true || - row['activityRecords'] != null) { - out.add({...row, 'parts': kept}); - } - } + final out = [ + for (final row in rows) ...(_withoutTools[row] ??= _withoutToolCalls(row)), + ]; if (items.isEmpty) return out; final timed = [ for (final item in items) if (_createdAt(item) != null) item, ]..sort((a, b) => _createdAt(a)!.compareTo(_createdAt(b)!)); + final times = [for (final row in out) _rowTime(row)]; int? oldest; var lastTimed = -1; for (var index = 0; index < out.length; index++) { - final time = _rowTime(out[index]); + final time = times[index]; if (time == null) continue; oldest ??= time; lastTimed = index; } final result = []; var next = 0; - JsonMap rowOf(JsonMap item) => { + JsonMap rowOf(JsonMap item) => _shownRows[item] ??= { 'kind': chatShownRowKind, 'key': 'chat-shown-${item['id']}', 'messageIds': ['shown:${item['id']}'], @@ -136,7 +152,7 @@ List withChatShownItems( } } for (var index = 0; index < out.length; index++) { - final time = _rowTime(out[index]); + final time = times[index]; if (time != null) { while (next < timed.length && _createdAt(timed[next])! < time) { result.add(rowOf(timed[next++])); diff --git a/app/lib/features/chat/domain/chat_task_notices.dart b/app/lib/features/chat/domain/chat_task_notices.dart index b131cbf3..59a9ed24 100644 --- a/app/lib/features/chat/domain/chat_task_notices.dart +++ b/app/lib/features/chat/domain/chat_task_notices.dart @@ -59,6 +59,20 @@ ChatTaskNotice? _rowNotice(JsonMap row) { : null; } +// The group row last built for a run, by the run's first row. Rows are values +// (see `transcriptRows`), so the same run yields the same group object. +final Expando<({List run, JsonMap group})> _groups = Expando( + 'chat task notice group', +); + +bool _identicalRows(List a, List b) { + if (a.length != b.length) return false; + for (var index = 0; index < a.length; index++) { + if (!identical(a[index], b[index])) return false; + } + return true; +} + /// Folds each run of two or more consecutive background-task notice rows into /// one `task_notices` row, the way a turn's tool calls fold into one chip. /// @@ -73,14 +87,21 @@ List groupTaskNotices(List rows) { if (run.length == 1) { result.add(run.single); } else if (run.isNotEmpty) { - result.add({ - 'kind': 'task_notices', - 'key': run.first['key'], - // Every grouped message's id, so the row keeps one widget identity as - // notices join it at either end (see ChatRowKeys). - 'messageIds': [for (final row in run) '${row['key'] ?? ''}'], - 'messages': [for (final row in run) row['message']], - }); + final held = _groups[run.first]; + if (held != null && _identicalRows(held.run, run)) { + result.add(held.group); + } else { + final group = { + 'kind': 'task_notices', + 'key': run.first['key'], + // Every grouped message's id, so the row keeps one widget identity + // as notices join it at either end (see ChatRowKeys). + 'messageIds': [for (final row in run) '${row['key'] ?? ''}'], + 'messages': [for (final row in run) row['message']], + }; + _groups[run.first] = (run: List.of(run), group: group); + result.add(group); + } } run.clear(); } diff --git a/app/lib/features/chat/domain/chat_transcript.dart b/app/lib/features/chat/domain/chat_transcript.dart index 47bdb37c..f9a1a721 100644 --- a/app/lib/features/chat/domain/chat_transcript.dart +++ b/app/lib/features/chat/domain/chat_transcript.dart @@ -1,5 +1,6 @@ import '../../../core/api_gateway.dart'; -import 'chat_task_notices.dart'; + +export 'chat_transcript_rows.dart'; /// Merge chronological history pages by message id. Incoming authoritative /// copies replace held copies without changing their order; cursor is opaque. @@ -56,158 +57,6 @@ JsonMap refreshCachedChatHistory( return mergeChatHistory({'messages': live}, page, older: true); } -/// Consecutive assistant messages form one answer turn. All part fields (also -/// future/unknown protocol types) are preserved in original order. -/// -/// `key` is the turn's first assistant message id, which is only a display -/// anchor: paging an older assistant message into the turn changes it, so widget -/// identity uses `messageIds`. That list carries every message the folded turn -/// currently holds so the view can keep the same row element while the turn -/// grows at either end. -/// -/// A recorded activity sequence is not an answer bubble. Parts whose `type` is -/// `activity` or `compaction` are lifted out of the turn and collapse into one -/// `activity` row before it, carrying the recorded records, the web wording for -/// the latest record and that record's lifecycle state. Several updates of one -/// compaction are therefore one line, never one line per update. -/// -/// A message the server marks with a `kind` is content the harness delivered, -/// not a turn: an `agent_report` becomes its own `agent_report` row and a -/// `notice` its own `system_notice` row, on the agent's side, never folded into -/// an answer and never drawn as the operator's bubble (see [chatInjectedKind]). -/// Two or more consecutive background-task notices fold into one -/// `task_notices` row (see [groupTaskNotices]). -/// -/// [trailing] rows are conversation cards (permission requests and questions), -/// not messages. They keep the server's order after the transcript, the way the -/// web renders approvals and forms at the end of the conversation. -List transcriptRows( - List messages, { - List trailing = const [], - bool running = false, - bool awaitingResponse = false, - String? liveMessageId, -}) { - final rows = []; - String? key; - String model = ''; - final ids = []; - final parts = []; - final activity = []; - String? activityKey; - int? createdAt; - void flush() { - final compactions = activity - .where((p) => chatActivityKind(p) == 'compaction') - .toList(); - final passive = activity - .where((p) => chatActivityKind(p) != 'compaction') - .toList(); - if (compactions.isNotEmpty) { - final latest = compactions.last; - rows.add({ - 'kind': 'activity', - 'key': activityKey == null ? '' : 'chat-activity-$activityKey', - 'records': compactions, - 'label': chatActivityLabel(latest), - 'state': chatActivityState(latest).toLowerCase(), - }); - } - if (key != null && (parts.isNotEmpty || passive.isNotEmpty)) { - rows.add({ - 'kind': 'assistant', - 'key': key, - 'messageIds': List.of(ids), - 'parts': List.of(parts), - if (passive.isNotEmpty) 'activityRecords': passive, - // The model that answered, as the server recorded it on the reply. - if (model.isNotEmpty) 'model': model, - 'created_at': ?createdAt, - }); - } - key = null; - model = ''; - ids.clear(); - parts.clear(); - activity.clear(); - activityKey = null; - createdAt = null; - } - - for (final message in messages) { - final injected = chatInjectedKind(message); - if (injected != null) { - flush(); - rows.add({ - 'kind': injected == chatKindAgentReport - ? 'agent_report' - : 'system_notice', - 'key': message['id'], - 'message': message, - }); - continue; - } - if (message['role'] == 'assistant') { - final id = '${message['id'] ?? ''}'.trim(); - key ??= id; - createdAt ??= chatMessageCreatedAt(message); - final recorded = '${message['model'] ?? ''}'.trim(); - if (recorded.isNotEmpty) model = recorded; - if (id.isNotEmpty) ids.add(id); - for (final part in (message['parts'] as List? ?? const [])) { - if (part is! Map) continue; - final record = Map.from(part); - if (chatActivityType(record)) { - activityKey ??= id.isEmpty ? null : id; - activity.add(record); - } else { - parts.add(record); - } - } - } else { - flush(); - rows.add({'kind': 'user', 'key': message['id'], 'message': message}); - } - } - flush(); - // Prefer the assistant identity that actually streamed: a queued follow-up - // can be later in the transcript while this same reply is still arriving. - // Before a first delta, do not reopen an older reply behind a new user turn. - final answers = [ - for (final message in messages) - if (chatInjectedKind(message) == null) message, - ]; - final streamingId = - liveMessageId ?? - (answers.isNotEmpty && answers.last['role'] == 'assistant' - ? '${answers.last['id'] ?? ''}' - : null); - var hasStreamingRow = false; - if (running && streamingId != null) { - for (final row in rows.reversed) { - if (row['kind'] == 'assistant' && - (row['messageIds'] as List).contains(streamingId)) { - row['streaming'] = true; - hasStreamingRow = true; - break; - } - } - } - // Display-only response shell: never inserted into stored message history. - // A queued follow-up does not create a second shell beside the active reply. - if (!hasStreamingRow && (running || awaitingResponse)) { - rows.add({ - 'kind': 'assistant', - 'key': 'chat-pending-response', - 'messageIds': [], - 'parts': [], - 'streaming': true, - }); - } - // Consecutive background-task notices are one collapsed group. - return [...groupTaskNotices(rows), ...trailing]; -} - /// The protocol records creation in epoch milliseconds. Invalid values do not /// acquire an invented display time. int? chatMessageCreatedAt(JsonMap message) { @@ -312,6 +161,14 @@ List reconcileChatUser(List messages, JsonMap remote) { return result; } +/// The messages of a decoded `messages` value. A message that already is a +/// [JsonMap] is kept as the same object (messages are never changed in place); +/// any other map is converted. +List chatMessageList(Object? messages) => [ + for (final message in (messages as List? ?? const []).whereType()) + message is JsonMap ? message : Map.from(message), +]; + /// Fold a live chat frame into an immutable thread snapshot. Text and reasoning /// deltas append by part id, tool snapshots merge without dropping prior fields, /// and step frames update turn activity. Frames for another session are ignored. @@ -322,10 +179,9 @@ JsonMap applyChatEvent(JsonMap thread, JsonMap frame) { session != frame['session_id']) { return thread; } - final messages = (thread['messages'] as List? ?? const []) - .whereType() - .map((m) => Map.from(m)) - .toList(); + // Untouched messages keep their identity, so derived rows held for them + // (see ChatTranscriptRows) are reused; a changed message is a new map. + final messages = chatMessageList(thread['messages']); final kind = frame['kind']; if (kind == 'user') { final part = frame['part']; diff --git a/app/lib/features/chat/domain/chat_transcript_rows.dart b/app/lib/features/chat/domain/chat_transcript_rows.dart new file mode 100644 index 00000000..7d5d2914 --- /dev/null +++ b/app/lib/features/chat/domain/chat_transcript_rows.dart @@ -0,0 +1,257 @@ +import 'dart:collection'; + +import '../../../core/api_gateway.dart'; +import 'chat_task_notices.dart'; +import 'chat_transcript.dart'; + +/// Consecutive assistant messages form one answer turn. All part fields (also +/// future/unknown protocol types) are preserved in original order. +/// +/// `key` is the turn's first assistant message id, which is only a display +/// anchor: paging an older assistant message into the turn changes it, so widget +/// identity uses `messageIds`. That list carries every message the folded turn +/// currently holds so the view can keep the same row element while the turn +/// grows at either end. +/// +/// A recorded activity sequence is not an answer bubble. Parts whose `type` is +/// `activity` or `compaction` are lifted out of the turn and collapse into one +/// `activity` row before it, carrying the recorded records, the web wording for +/// the latest record and that record's lifecycle state. Several updates of one +/// compaction are therefore one line, never one line per update. +/// +/// A message the server marks with a `kind` is content the harness delivered, +/// not a turn: an `agent_report` becomes its own `agent_report` row and a +/// `notice` its own `system_notice` row, on the agent's side, never folded into +/// an answer and never drawn as the operator's bubble (see [chatInjectedKind]). +/// Two or more consecutive background-task notices fold into one +/// `task_notices` row (see [groupTaskNotices]). +/// +/// [trailing] rows are conversation cards (permission requests and questions), +/// not messages. They keep the server's order after the transcript, the way the +/// web renders approvals and forms at the end of the conversation. +/// +/// Rows are values: nothing downstream changes a row in place, which is what +/// lets [ChatTranscriptRows] hand out the same rows again. +List transcriptRows( + List messages, { + List trailing = const [], + bool running = false, + bool awaitingResponse = false, + String? liveMessageId, +}) => _finish( + [for (final segment in _segments(messages)) ..._segmentRows(segment)], + messages, + trailing: trailing, + running: running, + awaitingResponse: awaitingResponse, + liveMessageId: liveMessageId, +); + +/// [transcriptRows] for a transcript that changes a little at a time. +/// +/// The rows of a run of consecutive answer messages, or of one other message, +/// depend on those messages alone. Each run's rows are kept while the run holds +/// the identical message objects, so a streamed delta (which replaces only the +/// live message) rebuilds only the live turn, and a prepended history page +/// only the runs it touches. The result equals [transcriptRows] for the same +/// arguments; messages must not be changed in place once passed. +class ChatTranscriptRows { + /// The previous call's runs, by their first message. + Map _held = HashMap.identity(); + + List rows( + List messages, { + List trailing = const [], + bool running = false, + bool awaitingResponse = false, + String? liveMessageId, + }) { + final next = HashMap.identity(); + final rows = []; + for (final messages in _segments(messages)) { + final held = _held[messages.first]; + final segment = held != null && _sameMessages(held.messages, messages) + ? held + : _Segment(messages, _segmentRows(messages)); + next[messages.first] = segment; + rows.addAll(segment.rows); + } + _held = next; + return _finish( + rows, + messages, + trailing: trailing, + running: running, + awaitingResponse: awaitingResponse, + liveMessageId: liveMessageId, + ); + } + + static bool _sameMessages(List a, List b) { + if (a.length != b.length) return false; + for (var index = 0; index < a.length; index++) { + if (!identical(a[index], b[index])) return false; + } + return true; + } +} + +class _Segment { + _Segment(this.messages, this.rows); + final List messages; + final List rows; +} + +/// Whether [message] folds into an answer turn with its assistant neighbours. +bool _answerPart(JsonMap message) => + chatInjectedKind(message) == null && message['role'] == 'assistant'; + +/// [messages] split into runs whose rows depend on the run alone: each maximal +/// run of answer messages, and every other message on its own. +List> _segments(List messages) { + final segments = >[]; + List? answer; + for (final message in messages) { + if (_answerPart(message)) { + if (answer == null) segments.add(answer = []); + answer.add(message); + } else { + answer = null; + segments.add([message]); + } + } + return segments; +} + +/// The rows of one run from [_segments]. +List _segmentRows(List segment) { + final first = segment.first; + if (!_answerPart(first)) { + final injected = chatInjectedKind(first); + if (injected != null) { + return [ + { + 'kind': injected == chatKindAgentReport + ? 'agent_report' + : 'system_notice', + 'key': first['id'], + 'message': first, + }, + ]; + } + return [ + {'kind': 'user', 'key': first['id'], 'message': first}, + ]; + } + final rows = []; + String? key; + String model = ''; + final ids = []; + final parts = []; + final activity = []; + String? activityKey; + int? createdAt; + for (final message in segment) { + final id = '${message['id'] ?? ''}'.trim(); + key ??= id; + createdAt ??= chatMessageCreatedAt(message); + final recorded = '${message['model'] ?? ''}'.trim(); + if (recorded.isNotEmpty) model = recorded; + if (id.isNotEmpty) ids.add(id); + for (final part in (message['parts'] as List? ?? const [])) { + if (part is! Map) continue; + final record = Map.from(part); + if (chatActivityType(record)) { + activityKey ??= id.isEmpty ? null : id; + activity.add(record); + } else { + parts.add(record); + } + } + } + final compactions = activity + .where((p) => chatActivityKind(p) == 'compaction') + .toList(); + final passive = activity + .where((p) => chatActivityKind(p) != 'compaction') + .toList(); + if (compactions.isNotEmpty) { + final latest = compactions.last; + rows.add({ + 'kind': 'activity', + 'key': activityKey == null ? '' : 'chat-activity-$activityKey', + 'records': compactions, + 'label': chatActivityLabel(latest), + 'state': chatActivityState(latest).toLowerCase(), + }); + } + if (parts.isNotEmpty || passive.isNotEmpty) { + rows.add({ + 'kind': 'assistant', + 'key': key, + 'messageIds': ids, + 'parts': parts, + if (passive.isNotEmpty) 'activityRecords': passive, + // The model that answered, as the server recorded it on the reply. + if (model.isNotEmpty) 'model': model, + 'created_at': ?createdAt, + }); + } + return rows; +} + +/// The streaming mark, the response shell, task-notice grouping and the +/// trailing cards over the transcript's own [rows]. [rows] is not changed: a +/// marked row is a copy. +List _finish( + List rows, + List messages, { + required List trailing, + required bool running, + required bool awaitingResponse, + required String? liveMessageId, +}) { + // Prefer the assistant identity that actually streamed: a queued follow-up + // can be later in the transcript while this same reply is still arriving. + // Before a first delta, do not reopen an older reply behind a new user turn. + String? streamingId = liveMessageId; + if (streamingId == null) { + for (final message in messages.reversed) { + if (chatInjectedKind(message) != null) continue; + if (message['role'] == 'assistant') { + streamingId = '${message['id'] ?? ''}'; + } + break; + } + } + var marked = rows; + var hasStreamingRow = false; + if (running && streamingId != null) { + for (var index = rows.length - 1; index >= 0; index--) { + final row = rows[index]; + if (row['kind'] == 'assistant' && + (row['messageIds'] as List).contains(streamingId)) { + marked = List.of(rows); + marked[index] = {...row, 'streaming': true}; + hasStreamingRow = true; + break; + } + } + } + // Display-only response shell: never inserted into stored message history. + // A queued follow-up does not create a second shell beside the active reply. + if (!hasStreamingRow && (running || awaitingResponse)) { + marked = [ + ...marked, + { + 'kind': 'assistant', + 'key': 'chat-pending-response', + 'messageIds': [], + 'parts': [], + 'streaming': true, + }, + ]; + } + // Consecutive background-task notices are one collapsed group. + return [...groupTaskNotices(marked), ...trailing]; +} diff --git a/app/notes/perf-4k-chat.md b/app/notes/perf-4k-chat.md index 207cba2f..6e217bee 100644 --- a/app/notes/perf-4k-chat.md +++ b/app/notes/perf-4k-chat.md @@ -55,6 +55,12 @@ timeline) in the self-time lists are measurement overhead. 16 ms, `sliver_animated_extent` layout 250 ms inclusive over the stream. Every delta re-derives all rows of the transcript; only the live message changed. Memoise the settled rows and recompute the tail. + Change (not yet re-benchmarked): `ChatTranscriptRows` + (`chat_transcript_rows.dart`) keeps each message run's rows while it holds + the identical message objects, `applyChatEvent` keeps untouched messages' + identity, and the shown-item filter, task-notice groups and + `ChatRowKeys` ids are held per row object. Equality with a full derivation + is covered by `test/features/chat/conversation/chat_transcript_rows_memo_test.dart`. 5. **Spinner during streaming**: `CircularProgressIndicator`'s controller getter looks up an inherited widget every frame (89 ms over the stream) on top of the border animation; two 60 fps animations for one running turn. diff --git a/app/test/features/chat/conversation/chat_transcript_rows_memo_test.dart b/app/test/features/chat/conversation/chat_transcript_rows_memo_test.dart new file mode 100644 index 00000000..2523590d --- /dev/null +++ b/app/test/features/chat/conversation/chat_transcript_rows_memo_test.dart @@ -0,0 +1,321 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:pipelines_app/core/api_gateway.dart'; +import 'package:pipelines_app/features/chat/conversation/chat_permission_card_model.dart'; +import 'package:pipelines_app/features/chat/conversation/chat_permission_group.dart'; +import 'package:pipelines_app/features/chat/conversation/chat_question_group.dart'; +import 'package:pipelines_app/features/chat/conversation/chat_question_card_model.dart'; +import 'package:pipelines_app/features/chat/conversation/chat_row_keys.dart'; +import 'package:pipelines_app/features/chat/domain/chat_shown_items.dart'; +import 'package:pipelines_app/features/chat/domain/chat_transcript.dart'; +import 'package:pipelines_app/features/chat/domain/chat_turn_diffs.dart'; + +/// The thread view's inputs to its row derivation. +class _State { + List messages = []; + List cards = []; + List shown = []; + bool hasMore = false; + bool running = false; + String? live; + final diffs = ChatTurnDiffs(); +} + +typedef _Rows = + List Function( + List messages, { + List trailing, + bool running, + bool awaitingResponse, + String? liveMessageId, + }); + +/// The chain the thread view runs, with [rows] as its first stage. +List _derive(_Rows rows, _State state) => attachChatTurnDiffs( + groupSettledQuestions( + groupSettledPermissions( + withChatShownItems( + rows( + state.messages, + trailing: state.cards, + running: state.running, + awaitingResponse: false, + liveMessageId: state.live, + ), + state.shown, + hasMore: state.hasMore, + ), + ), + ), + state.diffs, +); + +/// Comparable form of a row list: permission cards are plain objects. +Object? _plain(Object? value) => switch (value) { + ChatPermissionCardData card => { + 'requestId': card.requestId, + 'action': card.action, + 'state': '${card.state}', + 'decision': card.decision, + 'headline': card.headline, + 'statusLine': card.statusLine, + }, + Map map => {for (final e in map.entries) e.key: _plain(e.value)}, + List list => [for (final item in list) _plain(item)], + _ => value, +}; + +const _base = 1700000000000; + +List _history(int from, int to) => [ + for (var i = from; i < to; i++) ...[ + { + 'id': 'u$i', + 'role': 'user', + 'created_at': _base + i * 1000, + 'parts': [ + {'type': 'text', 'text': 'question $i'}, + ], + }, + { + 'id': 'a$i', + 'role': 'assistant', + 'model': 'm', + 'created_at': _base + i * 1000 + 100, + 'parts': [ + {'type': 'reasoning', 'id': 'r$i', 'text': 'thinking $i'}, + { + 'type': 'tool', + 'id': 'call-$i', + 'name': i % 5 == 0 ? 'mcp__pipelines__show_file' : 'Bash', + 'state': {'status': 'completed'}, + }, + if (i % 7 == 0) + {'type': 'compaction', 'id': 'c$i', 'state': 'completed'}, + ], + }, + // A second message in the same turn. + if (i.isOdd) + { + 'id': 'b$i', + 'role': 'assistant', + 'parts': [ + {'type': 'text', 'id': 't$i', 'text': 'answer $i'}, + ], + }, + if (i % 9 == 0) ...[ + for (final n in [1, 2]) + { + 'id': 'n$i-$n', + 'role': 'assistant', + 'kind': 'notice', + 'created_at': _base + i * 1000 + 500 + n, + 'parts': [ + {'type': 'text', 'text': 'Background task finished: job $n'}, + ], + }, + ], + if (i % 11 == 0) + { + 'id': 'r$i', + 'role': 'assistant', + 'kind': 'agent_report', + 'parts': [ + {'type': 'text', 'text': 'report $i'}, + ], + }, + ], +]; + +JsonMap _thread(_State state) => { + 'sessionId': 's1', + 'messages': state.messages, + 'running': state.running, +}; + +void _apply(_State state, JsonMap frame) { + final next = applyChatEvent(_thread(state), {'session_id': 's1', ...frame}); + state.messages = chatMessageList(next['messages']); + state.running = next['running'] == true; +} + +void _delta(_State state, String messageId, String text) { + _apply(state, { + 'kind': 'text', + 'message_id': messageId, + 'part': {'id': 'live-$messageId'}, + 'delta': text, + }); + state.live = messageId; +} + +void main() { + late _State state; + late ChatTranscriptRows memo; + late ChatRowKeys memoKeys; + late ChatRowKeys fullKeys; + late List previous; + + /// Derives through the memo and from scratch, and checks they agree on rows + /// and on the keys a long-lived [ChatRowKeys] assigns them. + List check() { + final incremental = _derive(memo.rows, state); + final full = _derive(transcriptRows, state); + expect(_plain(incremental), _plain(full)); + expect(memoKeys.forRows(incremental), fullKeys.forRows(full)); + return previous = incremental; + } + + /// Rows of [now] that are the very objects [before] held for the same key. + int reused(List before, List now) { + final held = {for (final row in before) '${row['key']}': row}; + return now.where((row) => identical(held['${row['key']}'], row)).length; + } + + setUp(() { + state = _State()..messages = _history(10, 40); + state.shown = [ + {'id': 'f1', 'kind': 'file', 'created_at': _base + 20 * 1000 + 50}, + {'id': 'f0', 'kind': 'file', 'created_at': _base + 1000}, + ]; + state.hasMore = true; + memo = ChatTranscriptRows(); + memoKeys = ChatRowKeys(); + fullKeys = ChatRowKeys(); + check(); + }); + + test('streaming into the last message re-derives only the live turn', () { + _apply(state, { + 'kind': 'user', + 'message_id': 'u-live', + 'part': {'type': 'text', 'text': 'go'}, + }); + check(); + for (var i = 0; i < 20; i++) { + final before = previous; + _delta(state, 'a-live', 'chunk $i '); + final now = check(); + expect(now.last['streaming'], isTrue); + // Every row but the live turn is the object the last build held. + if (i > 0) expect(reused(before, now), now.length - 1); + } + // A second message joins the live turn. + _delta(state, 'b-live', 'more'); + check(); + _apply(state, { + 'kind': 'step', + 'step': {'status': 'ended'}, + }); + state.live = null; + final now = check(); + expect(now.any((row) => row['streaming'] == true), isFalse); + }); + + test('a new message appended keeps the settled rows', () { + final before = previous; + state.messages = [ + ...state.messages, + { + 'id': 'u-new', + 'role': 'user', + 'created_at': _base + 99000, + 'parts': [ + {'type': 'text', 'text': 'next'}, + ], + }, + ]; + final now = check(); + expect(reused(before, now), greaterThan(before.length - 3)); + }); + + test('a shown item arriving is placed as a full derivation places it', () { + state.shown = mergeChatShownItems(state.shown, [ + {'id': 'f2', 'kind': 'image', 'created_at': _base + 30 * 1000 + 10}, + ]); + final now = check(); + expect(now.where((row) => row['kind'] == chatShownRowKind), hasLength(2)); + // An older one waits for its page while history remains. + state.shown = mergeChatShownItems(state.shown, [ + {'id': 'f3', 'kind': 'notify', 'created_at': _base}, + ]); + check(); + }); + + test('permission cards added, then settled into their turn', () { + state.cards = [ + { + 'kind': 'permission', + 'key': 'chat-permission-p1', + 'frame': { + 'request_id': 'p1', + 'action': 'Bash', + 'source': {'type': 'tool', 'id': 'call-38'}, + }, + 'answer': '', + 'error': null, + }, + { + 'kind': 'question', + 'key': 'chat-question-q1', + 'form': {'id': 'q1', chatQuestionRaisedIn: 'a37', 'resolved': true}, + 'error': null, + }, + ]; + var now = check(); + expect(now.where((row) => row['kind'] == 'permission'), hasLength(1)); + state.cards = [ + {...state.cards.first, 'answer': 'once'}, + state.cards.last, + ]; + now = check(); + expect(now.where((row) => row['kind'] == 'permission'), isEmpty); + expect(now.where((row) => row['permissions'] != null), hasLength(1)); + expect(now.where((row) => row['questions'] != null), hasLength(1)); + _delta(state, 'a-live', 'after the card'); + check(); + }); + + test('turn diffs arriving attach as a full derivation attaches them', () { + state.diffs.apply({ + 'session_id': 's1', + 'turn_diffs': [ + { + 'turn_id': 't1', + 'message_ids': ['a31'], + 'files': 2, + }, + ], + }, 's1'); + final now = check(); + expect(now.where((row) => row['turnDiff'] != null), hasLength(1)); + _delta(state, 'a-live', 'x'); + check(); + }); + + test('a prepended history page re-derives only the runs it touches', () { + final before = previous; + // The older page ends in an assistant message that folds into the + // first loaded turn's neighbour, and the last page drops hasMore. + state.messages = [..._history(0, 10), ...state.messages]; + state.hasMore = false; + final now = check(); + expect(reused(before, now), greaterThan(before.length ~/ 2)); + state.messages = [ + { + 'id': 'older', + 'role': 'assistant', + 'parts': [ + {'type': 'text', 'id': 'o', 'text': 'older'}, + ], + }, + ...state.messages, + ]; + check(); + }); + + test('a cached transcript reused with the same list returns equal rows', () { + final before = previous; + final now = check(); + expect(reused(before, now), now.length); + }); +}