Chat: fold activity-only rows between messages into one summary line

Tool-only turns, thinking-only turns and background-task notices that sit
between two content rows become one collapsible activity_group row, e.g.
"Ran 7 commands · used 9 tools · 8 background tasks finished · 1 failed",
the failure count in the error colour and never ellipsized. Expanding it
shows the folded rows as they render on their own. A run's task notices
fold into one chip at the newest notice; a notice whose id recurs later is
counted only at its latest position.

Grouping is a pure domain step (groupChatActivity) applied last in the
thread view's row pipeline; group rows list every member id so ChatRowKeys
keeps their identity, unchanged runs return the identical group object, and
the transcript anchor accepts a group's agent-turn ids. The scroll fixture
gains ?bgwork=1 for the shape.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-25 10:58:25 +00:00
parent 33874005c4
commit 22866ec0ce
10 changed files with 963 additions and 13 deletions
@@ -0,0 +1,140 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/core/api_gateway.dart';
import 'package:pipelines_app/features/chat/conversation/chat_draft_store.dart';
import 'package:pipelines_app/features/chat/conversation/chat_thread_view.dart';
import 'package:pipelines_app/features/chat/ui/transcript/chat_activity_group_view.dart';
import 'package:pipelines_app/features/chat/ui/transcript/chat_task_notices_group.dart';
import 'package:pipelines_app/features/chat/ui/transcript/chat_turn_activity_view.dart';
import 'chat_thread_fixtures.dart';
/// Tool-only turns and background-task notices between two answers are one
/// summary line that opens to the rows as they render on their own.
JsonMap _notice(String id) => {
'id': id,
'role': 'assistant',
'kind': 'notice',
'parts': [
{'type': 'text', 'text': 'Background task finished: task $id'},
],
};
JsonMap _tool(String id, {bool failed = false}) => {
'type': 'tool',
'id': id,
'name': 'bash',
'state': {'status': failed ? 'error' : 'completed'},
};
JsonMap _text(String text) => {'type': 'text', 'text': text};
final _messages = [
chatMessage('u1', 'user', parts: [_text('Run the checks')]),
chatMessage('a1', 'assistant', parts: [_text('Starting them now.')]),
_notice('n1'),
chatMessage('w1', 'assistant', parts: [_tool('t1')]),
_notice('n2'),
_notice('n3'),
chatMessage(
'w2',
'assistant',
parts: [_tool('t2'), _tool('t3', failed: true)],
),
_notice('n4'),
chatMessage('a2', 'assistant', parts: [_text('All done.')]),
];
Future<void> _host(WidgetTester tester) async {
tester.view.physicalSize = const Size(1000, 1600);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.reset);
final gateway = FakeChatGateway()..history = (_) async => chatPage(_messages);
addTearDown(gateway.close);
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: ChatThreadView(
gateway: gateway,
sessionId: 's',
onBack: () {},
drafts: MemoryChatDraftStore(),
newOperationId: () => 'op',
),
),
),
);
await tester.pump();
await tester.pump(const Duration(milliseconds: 100));
await tester.pump();
}
void main() {
testWidgets('the activity summary expands to its rows and collapses again', (
tester,
) async {
final semantics = tester.ensureSemantics();
await _host(tester);
const label = 'Ran 3 commands · 4 background tasks finished · 1 failed';
final summary = find.byKey(const Key('chat-activity-group'));
expect(find.byType(ChatActivityGroupView), findsOneWidget);
expect(summary, findsOneWidget);
expect(
find.text('Ran 3 commands · 4 background tasks finished'),
findsOneWidget,
);
// The failure is drawn whole, in the error colour.
final failed = tester.widget<Text>(
find.byKey(const Key('chat-activity-group-failed')),
);
expect(failed.data, ' · 1 failed');
final context = tester.element(summary);
expect(failed.style?.color, Theme.of(context).colorScheme.error);
expect(
tester.getSemantics(summary),
matchesSemantics(
label: '$label, show all 3 activity rows',
isButton: true,
hasTapAction: true,
hasExpandedState: true,
isExpanded: false,
),
);
// Collapsed: neither the turns' own lines nor the notices are built; the
// answers on both sides still are.
expect(find.byType(ChatTurnActivityView), findsNothing);
expect(find.byType(ChatTaskNoticesGroup), findsNothing);
expect(find.text('Starting them now.'), findsOneWidget);
expect(find.text('All done.'), findsOneWidget);
await tester.tap(summary);
await tester.pumpAndSettle();
expect(find.text('Ran 1 command'), findsOneWidget);
expect(find.text('Ran 2 commands · 1 failed'), findsOneWidget);
// Every notice of the run is one chip, shown once at the newest notice.
expect(find.byType(ChatTaskNoticesGroup), findsOneWidget);
expect(find.text('4 background tasks'), findsOneWidget);
expect(
tester.getTopLeft(find.byType(ChatTaskNoticesGroup)).dy,
greaterThan(tester.getTopLeft(find.text('Ran 2 commands · 1 failed')).dy),
);
expect(
tester.getSemantics(summary),
matchesSemantics(
label: '$label, hide all 3 activity rows',
isButton: true,
hasTapAction: true,
hasExpandedState: true,
isExpanded: true,
),
);
await tester.tap(summary);
await tester.pumpAndSettle();
expect(find.byType(ChatTurnActivityView), findsNothing);
expect(find.byType(ChatTaskNoticesGroup), findsNothing);
semantics.dispose();
});
}
@@ -191,6 +191,12 @@ void main() {
greaterThan(width * 0.8),
);
// A task notice folds into the activity summary on the agent's side and
// shows as its own line once that is opened.
final summary = find.text('1 background task finished');
expect(summary, findsOneWidget);
await tester.tap(summary);
await _pump(tester);
final notice = find.text(
'Background task finished: Background command "Build" completed',
);
@@ -10,8 +10,8 @@ import 'package:pipelines_app/features/chat/ui/transcript/chat_task_notices_grou
import 'chat_thread_fixtures.dart';
/// Consecutive background-task notices are one collapsed chip on the agent's
/// side; it opens a sheet with one compact row per task. None of them is ever
/// the operator's bubble.
/// side, inside the activity summary they fold into; the chip opens a sheet
/// with one compact row per task. None of them is ever the operator's bubble.
JsonMap _notice(String id, String text) => {
'id': id,
@@ -73,11 +73,18 @@ Future<FakeChatGateway> _host(
return gateway;
}
/// Opens the activity summary the notices fold into.
Future<void> _expand(WidgetTester tester, String label) async {
await tester.tap(find.text(label));
await _pump(tester);
}
void main() {
testWidgets('consecutive task notices are one chip that opens to each task', (
tester,
) async {
await _host(tester, _messages);
await _expand(tester, '3 background tasks ended');
expect(find.byType(ChatTaskNoticesGroup), findsOneWidget);
expect(find.byType(ChatSystemNoticeLine), findsNothing);
@@ -122,6 +129,7 @@ void main() {
testWidgets('a lone notice stays one line and a live second notice joins '
'it into the group', (tester) async {
final gateway = await _host(tester, _messages.take(2).toList());
await _expand(tester, '1 background task finished');
expect(find.byType(ChatSystemNoticeLine), findsOneWidget);
expect(find.byType(ChatTaskNoticesGroup), findsNothing);
@@ -136,6 +144,8 @@ void main() {
'failed (exit code 1)',
});
await _pump(tester);
// The summary keeps its place and open state as the notice joins it.
expect(find.text('2 background tasks ended'), findsOneWidget);
expect(find.byType(ChatSystemNoticeLine), findsNothing);
expect(find.text('2 background tasks · 1 failed'), findsOneWidget);
expect(find.byType(ChatUserBubble), findsOneWidget);
@@ -0,0 +1,313 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/core/api_gateway.dart';
import 'package:pipelines_app/features/chat/conversation/chat_row_keys.dart';
import 'package:pipelines_app/features/chat/conversation/chat_transcript_anchor.dart';
import 'package:pipelines_app/features/chat/domain/chat_activity_groups.dart';
import 'package:pipelines_app/features/chat/domain/chat_transcript_rows.dart';
JsonMap _user(String id) => {
'id': id,
'role': 'user',
'parts': [
{'type': 'text', 'text': 'question $id'},
],
};
JsonMap _answer(String id, {List<JsonMap> tools = const []}) => {
'id': id,
'role': 'assistant',
'parts': [
...tools,
{'type': 'text', 'text': 'answer $id'},
],
};
JsonMap _tool(String id, String name, {bool failed = false}) => {
'type': 'tool',
'id': id,
'name': name,
'state': {'status': failed ? 'error' : 'completed'},
};
JsonMap _work(String id, List<JsonMap> parts) => {
'id': id,
'role': 'assistant',
'parts': parts,
};
JsonMap _notice(String id, {String outcome = 'finished'}) => {
'id': id,
'role': 'assistant',
'kind': 'notice',
'parts': [
{'type': 'text', 'text': 'Background task $outcome: task $id'},
],
};
List<JsonMap> _rows(List<JsonMap> messages) =>
groupChatActivity(transcriptRows(messages));
List<String> _kinds(List<JsonMap> rows) => [
for (final row in rows) '${row['kind']}',
];
/// The transcript the operator reported: two answers with tool-only turns and
/// background-task notices between them.
List<JsonMap> _backgroundSession() => [
_user('u1'),
_answer('a1'),
_notice('n1'),
_work('w1', [for (var i = 0; i < 9; i++) _tool('w1-$i', 'read')]),
_notice('n2'),
_notice('n3'),
_work('w2', [_tool('w2-0', 'bash')]),
_notice('n4'),
_work('w3', [
for (var i = 0; i < 4; i++) _tool('w3-$i', 'bash', failed: i == 1),
]),
_notice('n5'),
_work('w4', [
{'type': 'reasoning', 'text': 'thinking'},
]),
_notice('n6'),
_answer('a2'),
];
void main() {
test('a run of activity rows between two answers is one summary row', () {
final rows = _rows(_backgroundSession());
expect(_kinds(rows), [
'user',
'assistant',
chatActivityGroupKind,
'assistant',
]);
final group = rows[2];
expect(
chatActivityGroupLabel(group),
'Ran 5 commands · used 9 tools · 6 background tasks finished · 1 failed',
);
final segments = chatActivityGroupSegments(group);
expect(segments.last, (text: '1 failed', failed: true));
expect(segments.where((s) => s.failed), hasLength(1));
// The members render as before, with every notice folded into one chip
// at the newest notice's place, so the chip shows once.
final members = chatActivityGroupRows(group);
expect(_kinds(members), [
'assistant',
'assistant',
'assistant',
'assistant',
'task_notices',
]);
expect(members.last['messageIds'], ['n1', 'n2', 'n3', 'n4', 'n5', 'n6']);
expect(group['messageIds'], [
'w1',
'w2',
'w3',
'w4',
'n1',
'n2',
'n3',
'n4',
'n5',
'n6',
]);
});
test('content rows bound runs and a single tool turn stays itself', () {
final rows = _rows([
_user('u1'),
_answer('a1'),
_notice('n1'),
_answer('a2'),
_user('u2'),
_work('w1', [_tool('t', 'bash')]),
_user('u3'),
_work('w2', [_tool('t2', 'bash')]),
_work('w3', [
{'type': 'text', 'text': ' '},
]),
]);
// The lone notice is a summary, not a full-width line; the lone tool turn
// (folded with its blank neighbour into one turn) keeps its own row.
expect(_kinds(rows), [
'user',
'assistant',
chatActivityGroupKind,
'assistant',
'user',
'assistant',
'user',
'assistant',
]);
expect(chatActivityGroupLabel(rows[2]), '1 background task finished');
expect(chatActivityGroupRows(rows[2]).single['kind'], 'system_notice');
});
test('a turn with answer text, a chip or a live stream is content', () {
final work = transcriptRows([
_work('w1', [_tool('t', 'bash')]),
]).single;
expect(chatActivityOnlyRow(work), isTrue);
expect(chatActivityOnlyRow({...work, 'streaming': true}), isFalse);
expect(chatActivityOnlyRow({...work, 'turnDiff': {}}), isFalse);
expect(
chatActivityOnlyRow({
...work,
'shellHistory': ['r1'],
}),
isFalse,
);
expect(
chatActivityOnlyRow({
...work,
'questions': [{}],
}),
isFalse,
);
final answer = transcriptRows([_answer('a1')]).single;
expect(chatActivityOnlyRow(answer), isFalse);
expect(
chatActivityOnlyRow({
...work,
'parts': [
{'type': 'file', 'name': 'a.png', 'uri': 'x'},
],
}),
isFalse,
);
// The live turn stays its own row after the run before it.
final rows = groupChatActivity(
transcriptRows([
_user('u1'),
_answer('a1'),
_notice('n1'),
_notice('n2'),
_work('w1', [_tool('t', 'bash')]),
], running: true),
);
expect(_kinds(rows), [
'user',
'assistant',
chatActivityGroupKind,
'assistant',
]);
expect(rows.last['streaming'], isTrue);
});
test('failed and stopped tasks are carried into the summary', () {
final rows = _rows([
_answer('a1'),
_notice('n1', outcome: 'failed'),
_notice('n2', outcome: 'stopped'),
_user('u1'),
]);
expect(
chatActivityGroupLabel(rows[1]),
'2 background tasks ended · 1 failed',
);
});
test('a repeated task notice counts once, at its latest position', () {
final rows = _rows([
_answer('a1'),
_notice('n1'),
_work('w1', [_tool('t', 'bash')]),
_user('u1'),
_answer('a2'),
_notice('n1'),
_user('u2'),
]);
// The earlier run keeps only its tool turn, which stays its own row; the
// task is counted where it was reported last.
expect(_kinds(rows), [
'assistant',
'assistant',
'user',
'assistant',
chatActivityGroupKind,
'user',
]);
expect(rows[1]['key'], 'w1');
expect(chatActivityGroupLabel(rows[4]), '1 background task finished');
});
test('a repeated notice leaves only the later chip', () {
final rows = _rows([
_answer('a1'),
_notice('n1'),
_notice('n2'),
_user('u1'),
_answer('a2'),
_notice('n1'),
_notice('n2'),
_user('u2'),
]);
expect(_kinds(rows), [
'assistant',
'user',
'assistant',
chatActivityGroupKind,
'user',
]);
expect(chatActivityGroupLabel(rows[3]), '2 background tasks finished');
});
test(
'identity holds across a streamed delta and the key carries forward',
() {
final cache = ChatTranscriptRows();
final history = _backgroundSession();
final live = {
'id': 'live',
'role': 'assistant',
'parts': [
{'type': 'text', 'text': 'word'},
],
};
final before = groupChatActivity(
cache.rows([...history, _user('u9'), live], running: true),
);
final delta = {
...live,
'parts': [
{'type': 'text', 'text': 'word word'},
],
};
final after = groupChatActivity(
cache.rows([...history, _user('u9'), delta], running: true),
);
expect(identical(before[2], after[2]), isTrue);
expect(
identical(
chatActivityGroupRows(before[2]).last,
chatActivityGroupRows(after[2]).last,
),
isTrue,
);
// A tool turn that already had a row keeps its key when it joins a group.
final keys = ChatRowKeys();
final single = _rows([
_user('u1'),
_work('w1', [_tool('t', 'bash')]),
]);
final firstKeys = keys.forRows(single);
final grown = _rows([
_user('u1'),
_work('w1', [_tool('t', 'bash')]),
_notice('n1'),
]);
expect(keys.forRows(grown)[1], firstKeys[1]);
},
);
test('a group can anchor the transcript split by its agent turns', () {
final rows = _rows([..._backgroundSession(), _user('u2'), _answer('a3')]);
final anchor = chatTranscriptAnchor(rows);
expect(anchor, 'u2');
expect(chatTranscriptAnchorIndex(rows, 'w3'), 2);
expect(chatTranscriptAnchorIndex(rows, 'n3'), -1);
});
}