From c320dac131eda6f9507252ee1c11e4539884d74e Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Tue, 3 Nov 2026 18:34:29 +0000 Subject: [PATCH] Chat list: tool calls and notices never mark a chat New Only visible assistant reply text, a pending form or an unresolved permission request is unread. Tool results, user prompts, harness notices/agent reports and blank deltas only drive the working state. --- .../chat/ui/sessions/session_attention.dart | 19 ++-- app/notes/chat-unread-tool-calls.md | 21 ++++ .../sessions/chat_sessions_live_new_test.dart | 100 +++++++++++++----- .../ui/sessions/session_attention_test.dart | 40 +++++++ 4 files changed, 145 insertions(+), 35 deletions(-) create mode 100644 app/notes/chat-unread-tool-calls.md diff --git a/app/lib/features/chat/ui/sessions/session_attention.dart b/app/lib/features/chat/ui/sessions/session_attention.dart index 815a1f30..457407f0 100644 --- a/app/lib/features/chat/ui/sessions/session_attention.dart +++ b/app/lib/features/chat/ui/sessions/session_attention.dart @@ -18,23 +18,20 @@ ChatSessionFrameEffect? chatSessionFrameEffect(JsonMap frame) { if (event is! Map) return null; id = event['session_id'] is String ? event['session_id'] as String : null; final kind = event['kind']; - // A badge means content to read, not transport/activity churn: assistant - // text, another client's user message, a returned tool result, or a pending - // form. Reasoning, starts, steps, shells, compaction and usage are not unread. + // A badge means the agent said something to read or is waiting on the + // operator: visible assistant reply text or a pending form. Tool calls and + // their results, harness notices and agent reports (`message_kind`), the + // operator's own prompts from any device, reasoning, steps, shells, + // compaction and usage are turn activity, never unread. if (kind == 'text') { - attention = '${event['delta'] ?? ''}'.isNotEmpty; - } else if (kind == 'user') { - attention = true; + attention = + '${event['delta'] ?? ''}'.trim().isNotEmpty && + '${event['message_kind'] ?? ''}'.isEmpty; } else if (kind == 'tool') { final part = event['part']; final state = part is Map ? part['state'] : null; if (state is Map) { final status = state['status']; - attention = - (status == 'completed' || status == 'error') && - ((state['content'] is List && - (state['content'] as List).isNotEmpty) || - state['error'] != null); if (status == 'running' || status == 'streaming') running = true; } } else if (kind == 'form') { diff --git a/app/notes/chat-unread-tool-calls.md b/app/notes/chat-unread-tool-calls.md new file mode 100644 index 00000000..a1687606 --- /dev/null +++ b/app/notes/chat-unread-tool-calls.md @@ -0,0 +1,21 @@ +# Chat list: tool calls no longer mark a chat New + +Follow-up to 985cc355 ("Fix chat session unread attention state"). + +Root cause: that fix made unread purely frame-driven +(`lib/features/chat/ui/sessions/session_attention.dart`), but it still listed +a returned tool result (`kind: tool`, status `completed`/`error` with content or +error) and any `kind: user` frame as unread content. Every finished tool call in +a background chat therefore raised the New badge even when the agent had said +nothing yet. Text frames carrying a `message_kind` (harness `notice`, subagent +`agent_report`, often emitted around tool calls) and whitespace-only deltas also +counted. + +Now only visible assistant reply text (`kind: text`, non-blank delta, no +`message_kind`, matching the server's own `finalReplyTextLocked`), a pending +form, or an unresolved permission request marks a chat New. Tool frames only +drive the working indicator. The open chat stays excluded (`activeSessionId`). + +Tests: `test/features/chat/ui/sessions/chat_sessions_live_new_test.dart` +(tool call case, open-chat case), `session_attention_test.dart` (frame table). +Not browser- or device-verified. diff --git a/app/test/features/chat/ui/sessions/chat_sessions_live_new_test.dart b/app/test/features/chat/ui/sessions/chat_sessions_live_new_test.dart index 7a08c57c..aa015e6f 100644 --- a/app/test/features/chat/ui/sessions/chat_sessions_live_new_test.dart +++ b/app/test/features/chat/ui/sessions/chat_sessions_live_new_test.dart @@ -67,6 +67,25 @@ JsonMap _event(String id, {String kind = 'text', String? delta}) => { }, }; +JsonMap _tool(String id, String status) => { + 'type': 'chat_event', + 'chat_event': { + 'session_id': id, + 'kind': 'tool', + 'part': { + 'type': 'tool', + 'state': { + 'status': status, + if (status == 'completed') + 'content': [ + {'type': 'text', 'text': 'command output'}, + ], + if (status == 'error') 'error': 'exit status 1', + }, + }, + }, +}; + void main() { setUp(() => _store = MemoryChatSessionLocalStore()); @@ -176,46 +195,79 @@ void main() { expect(find.byType(CircularProgressIndicator), findsNothing); }); - testWidgets('tool starts are loading; returned tool results are unread', ( + testWidgets('tool calls and their results are loading, never New', ( tester, ) async { final fake = gateway(); await _pump(tester, fake); - fake.pushed.add({ - 'type': 'chat_event', - 'chat_event': { - 'session_id': 's1', - 'kind': 'tool', - 'part': { - 'type': 'tool', - 'state': {'status': 'running'}, - }, - }, - }); + fake.pushed.add(_tool('s1', 'running')); await tester.pump(); await tester.pump(); expect(find.byKey(const Key('chat-row-unread')), findsNothing); expect(find.byType(CircularProgressIndicator), findsOneWidget); + fake.pushed.add(_tool('s1', 'completed')); + fake.pushed.add(_tool('s1', 'error')); + // Harness notices, agent reports, a prompt from another device and + // whitespace-only deltas around a tool call are not a reply either. fake.pushed.add({ 'type': 'chat_event', 'chat_event': { 'session_id': 's1', - 'kind': 'tool', - 'part': { - 'type': 'tool', - 'state': { - 'status': 'completed', - 'content': [ - {'type': 'text', 'text': 'command output'}, - ], - }, - }, + 'kind': 'text', + 'delta': 'Background task finished', + 'message_kind': 'notice', + }, + }); + fake.pushed.add({ + 'type': 'chat_event', + 'chat_event': { + 'session_id': 's1', + 'kind': 'text', + 'delta': 'Subagent report', + 'message_kind': 'agent_report', + }, + }); + fake.pushed.add({ + 'type': 'chat_event', + 'chat_event': {'session_id': 's1', 'kind': 'user', 'delta': 'hi'}, + }); + fake.pushed.add(_event('s1', delta: '\n\n')); + // The turn is still working, so its spinner never settles. + await tester.pump(); + await tester.pump(); + expect(find.byKey(const Key('chat-row-unread')), findsNothing); + expect(find.byType(CircularProgressIndicator), findsOneWidget); + + // The assistant's reply after the tool call is what is unread. + fake.pushed.add(_event('s1', delta: 'Done: tests pass.')); + await tester.pump(); + await tester.pump(); + expect(find.byKey(const Key('chat-row-unread')), findsOneWidget); + }); + + testWidgets('a tool call and reply in the open chat stay read on leaving', ( + tester, + ) async { + final fake = gateway(); + await _pump(tester, fake, active: 's1'); + fake.pushed.add(_tool('s1', 'running')); + fake.pushed.add(_tool('s1', 'completed')); + fake.pushed.add(_event('s1', delta: 'Here is the answer.')); + fake.pushed.add({ + 'type': 'chat_event', + 'chat_event': { + 'session_id': 's1', + 'kind': 'step', + 'step': {'status': 'ended'}, }, }); await tester.pumpAndSettle(); - expect(find.byKey(const Key('chat-row-unread')), findsOneWidget); - expect(find.byType(CircularProgressIndicator), findsNothing); + expect(find.byKey(const Key('chat-row-unread')), findsNothing); + + await _pump(tester, fake); + expect(find.byKey(const Key('chat-row-unread')), findsNothing); + expect(_store.state?.isUnseen('s1') ?? false, isFalse); }); testWidgets('only unresolved permission requests mark New', (tester) async { diff --git a/app/test/features/chat/ui/sessions/session_attention_test.dart b/app/test/features/chat/ui/sessions/session_attention_test.dart index 76bde0c3..ce79e2fb 100644 --- a/app/test/features/chat/ui/sessions/session_attention_test.dart +++ b/app/test/features/chat/ui/sessions/session_attention_test.dart @@ -37,4 +37,44 @@ void main() { }); expect(intermediate!.running, isNull); }); + + test('only visible assistant reply text is unread', () { + bool attention(Map event) => + chatSessionFrameEffect({ + 'type': 'chat_event', + 'chat_event': {'session_id': 's', ...event}, + })!.attention; + for (final status in ['running', 'completed', 'error']) { + expect( + attention({ + 'kind': 'tool', + 'part': { + 'type': 'tool', + 'state': { + 'status': status, + 'content': [ + {'type': 'text', 'text': 'out'}, + ], + 'error': status == 'error' ? 'failed' : null, + }, + }, + }), + isFalse, + reason: 'tool $status', + ); + } + expect(attention({'kind': 'user', 'delta': 'prompt'}), isFalse); + expect(attention({'kind': 'text', 'delta': ' \n'}), isFalse); + for (final messageKind in ['notice', 'agent_report']) { + expect( + attention({ + 'kind': 'text', + 'delta': 'x', + 'message_kind': messageKind, + }), + isFalse, + ); + } + expect(attention({'kind': 'text', 'delta': 'answer'}), isTrue); + }); }