diff --git a/app/lib/features/chat/domain/chat_transcript.dart b/app/lib/features/chat/domain/chat_transcript.dart index 678d73fc..47bdb37c 100644 --- a/app/lib/features/chat/domain/chat_transcript.dart +++ b/app/lib/features/chat/domain/chat_transcript.dart @@ -95,6 +95,7 @@ List transcriptRows( final parts = []; final activity = []; String? activityKey; + int? createdAt; void flush() { final compactions = activity .where((p) => chatActivityKind(p) == 'compaction') @@ -121,6 +122,7 @@ List transcriptRows( 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; @@ -129,6 +131,7 @@ List transcriptRows( parts.clear(); activity.clear(); activityKey = null; + createdAt = null; } for (final message in messages) { @@ -147,6 +150,7 @@ List transcriptRows( 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); @@ -204,6 +208,20 @@ List transcriptRows( return [...groupTaskNotices(rows), ...trailing]; } +/// The protocol records creation in epoch milliseconds. Invalid values do not +/// acquire an invented display time. +int? chatMessageCreatedAt(JsonMap message) { + final value = message['created_at']; + if (value is! num || + !value.isFinite || + value <= 0 || + value > 8640000000000000 || + value != value.roundToDouble()) { + return null; + } + return value.toInt(); +} + /// `ChatMessage.Kind` of a report another agent delivered into the session. const String chatKindAgentReport = 'agent_report'; diff --git a/app/lib/features/chat/ui/transcript/chat_message_timestamp.dart b/app/lib/features/chat/ui/transcript/chat_message_timestamp.dart new file mode 100644 index 00000000..fec7d466 --- /dev/null +++ b/app/lib/features/chat/ui/transcript/chat_message_timestamp.dart @@ -0,0 +1,77 @@ +import 'package:flutter/cupertino.dart'; +import 'package:flutter/material.dart'; + +import '../sessions/chat_adaptive.dart'; + +/// The small, non-selectable metadata line for a recorded chat message time. +/// The visible value includes a local date so messages from different days are +/// not ambiguous; the tooltip and semantics expose the full localized value. +class ChatMessageTimestamp extends StatelessWidget { + const ChatMessageTimestamp({super.key, required this.createdAt}); + + final int? createdAt; + + @override + Widget build(BuildContext context) { + final instant = createdAt == null + ? null + : DateTime.fromMillisecondsSinceEpoch( + createdAt!, + isUtc: true, + ).toLocal(); + if (instant == null) return const SizedBox.shrink(); + + final cupertino = + chatControlsStyleOf(context) == ChatControlsStyle.cupertino; + final material = Localizations.of( + context, + MaterialLocalizations, + ); + final String date, time, full; + if (material != null) { + time = material.formatTimeOfDay( + TimeOfDay.fromDateTime(instant), + alwaysUse24HourFormat: MediaQuery.alwaysUse24HourFormatOf(context), + ); + date = material.formatCompactDate(instant); + full = '${material.formatFullDate(instant)}, $time'; + } else { + final local = CupertinoLocalizations.of(context); + final use24 = MediaQuery.alwaysUse24HourFormatOf(context); + final hour = use24 + ? instant.hour + : (instant.hour % 12 == 0 ? 12 : instant.hour % 12); + final period = use24 + ? '' + : ' ${instant.hour < 12 ? local.anteMeridiemAbbreviation : local.postMeridiemAbbreviation}'; + time = + '${local.datePickerHour(hour)}:${local.datePickerMinute(instant.minute)}$period'; + date = + '${local.datePickerMediumDate(instant)} ${local.datePickerYear(instant.year)}'; + full = '$date, $time'; + } + final style = + (cupertino + ? CupertinoTheme.of(context).textTheme.textStyle + : (Theme.of(context).textTheme.bodySmall ?? const TextStyle())) + .copyWith( + color: cupertino + ? CupertinoColors.secondaryLabel.resolveFrom(context) + : Theme.of(context).colorScheme.onSurfaceVariant, + fontSize: 12, + ); + final label = 'Sent $full'; + final text = Text('$date · $time', style: style); + return SelectionContainer.disabled( + child: Tooltip( + message: full, + child: Semantics( + container: true, + label: label, + excludeSemantics: true, + child: text, + ), + ), + ); + } +} diff --git a/app/lib/features/chat/ui/transcript/chat_message_view.dart b/app/lib/features/chat/ui/transcript/chat_message_view.dart index 8f4fd51e..060a3699 100644 --- a/app/lib/features/chat/ui/transcript/chat_message_view.dart +++ b/app/lib/features/chat/ui/transcript/chat_message_view.dart @@ -8,11 +8,13 @@ import '../../data/chat_attachment_images.dart'; import '../sessions/chat_adaptive.dart'; import 'chat_attachment_gallery.dart'; import '../../domain/chat_tool_calls.dart'; +import '../../domain/chat_transcript.dart'; import 'chat_copy_button.dart'; import 'chat_link_text.dart'; import 'chat_message_bubble.dart'; import 'chat_turn_activity_view.dart'; import 'native_disclosure.dart'; +import 'chat_message_timestamp.dart'; /// Builds an assistant text part; the default keeps the server's text exactly. typedef ChatMessageTextBuilder = Widget Function(BuildContext, String); @@ -112,7 +114,11 @@ class ChatMessageView extends StatelessWidget { for (final part in _partsOf(_userMessage)) if (_isFile(part) && chatPartIsThumbnail(part)) part, ]; - if (thumbnails.isEmpty && footer.isEmpty) { + final timestamp = ChatMessageTimestamp( + createdAt: chatMessageCreatedAt(_userMessage), + ); + final hasTimestamp = chatMessageCreatedAt(_userMessage) != null; + if (thumbnails.isEmpty && footer.isEmpty && !hasTimestamp) { return ChatUserBubble(cupertino: cupertino, child: body); } return Column( @@ -139,6 +145,7 @@ class ChatMessageView extends StatelessWidget { children: footer, ), ], + if (hasTimestamp) ...[const SizedBox(height: 2), timestamp], ], ); } @@ -196,6 +203,8 @@ class ChatMessageView extends StatelessWidget { color: _secondaryColor(context, _isCupertino(context)), ), ), + if (chatMessageCreatedAt(row) case final time?) + ChatMessageTimestamp(createdAt: time), ]; return Column( mainAxisSize: MainAxisSize.min, diff --git a/app/notes/chat-message-timestamps.md b/app/notes/chat-message-timestamps.md new file mode 100644 index 00000000..58ab62ec --- /dev/null +++ b/app/notes/chat-message-timestamps.md @@ -0,0 +1,11 @@ +# Chat message timestamps + +User messages and folded assistant replies show a small local date and time using recorded epoch-millisecond `created_at` values. Missing, invalid and out-of-range values stay absent. Folded replies use the first valid creation time, not render time. Native localization and secondary theme colors supply the presentation; full dates are exposed through tooltip and accessibility semantics. Material and Cupertino hosts are supported. + +Timestamp metadata is inside `SelectionContainer.disabled`, so cross-message drag selection and the reply Copy action exclude it. Existing delivery/model/copy controls remain. + +Manager recovered the incomplete Mobile v15 run 254 directly after detecting an ISO-string assumption that contradicted `protocol.ChatMessage.CreatedAt`. The run was canceled and awaited before editing; its worktree and changes were preserved. No workflow definitions changed. + +Checks: bounded analysis clean; 586 transcript/conversation tests passed through the memory watchdog (peak 1955 MB, 43s). 63 domain tests also passed (peak 954 MB, 4s). After strengthening local-format assertions, 25 focused message/selection tests passed (peak 1028 MB, 5s). Tests cover actual protocol values, invalid/missing values, folded replies and streaming, local formatting, Cupertino 2x text, copy buttons and forward/backward selection exclusion. + +Real Chrome full-app fixture screenshots inspected: `/private/tmp/timestamps-wide-final/01-timestamp.png` (1100x1000 light), `/private/tmp/timestamps-narrow-final/01-timestamp.png` (560x1000 dark), and `/private/tmp/timestamps-cupertino-final/01-timestamp.png` (560x1000 Cupertino light). Fixture uses opt-in `timestamps=1`; no production sessions touched. Capture tools exited normally. No physical device or Safari verification. diff --git a/app/test/features/chat/domain/chat_transcript_timestamp_test.dart b/app/test/features/chat/domain/chat_transcript_timestamp_test.dart new file mode 100644 index 00000000..1259b9a2 --- /dev/null +++ b/app/test/features/chat/domain/chat_transcript_timestamp_test.dart @@ -0,0 +1,34 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:pipelines_app/core/api_gateway.dart'; +import 'package:pipelines_app/features/chat/domain/chat_transcript.dart'; + +void main() { + test('folded assistant keeps its first recorded creation time', () { + final rows = transcriptRows([ + { + 'id': 'a1', + 'role': 'assistant', + 'created_at': 1792483920000, + 'parts': [ + {'type': 'text', 'text': 'first'}, + ], + }, + { + 'id': 'a2', + 'role': 'assistant', + 'created_at': 1792483921000, + 'parts': [ + {'type': 'text', 'text': 'streamed'}, + ], + }, + ]); + expect(rows.single['created_at'], 1792483920000); + }); + + test('invalid and missing creation values are unavailable', () { + expect(chatMessageCreatedAt({}), isNull); + expect(chatMessageCreatedAt({'created_at': ''}), isNull); + expect(chatMessageCreatedAt({'created_at': 'yesterday'}), isNull); + expect(chatMessageCreatedAt({'created_at': 1792483920000}), 1792483920000); + }); +} diff --git a/app/test/features/chat/ui/transcript/chat_message_view_test.dart b/app/test/features/chat/ui/transcript/chat_message_view_test.dart index e0babb07..70d459ed 100644 --- a/app/test/features/chat/ui/transcript/chat_message_view_test.dart +++ b/app/test/features/chat/ui/transcript/chat_message_view_test.dart @@ -6,6 +6,8 @@ import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/core/api_gateway.dart'; import 'package:pipelines_app/features/chat/domain/chat_transcript.dart'; import 'package:pipelines_app/features/chat/ui/transcript/chat_message_view.dart'; +import 'package:pipelines_app/features/chat/ui/transcript/chat_copy_button.dart'; +import 'package:pipelines_app/features/chat/ui/transcript/chat_message_timestamp.dart'; JsonMap _row(List messages) => transcriptRows(messages).single; @@ -59,6 +61,150 @@ void _phone(WidgetTester tester) { } void main() { + test('folded replies retain the first valid recorded epoch time', () { + JsonMap message(String id, Object? time) => { + 'id': id, + 'role': 'assistant', + 'created_at': time, + 'parts': [ + {'type': 'text', 'text': id}, + ], + }; + final first = DateTime.utc(2026, 10, 20, 8, 12).millisecondsSinceEpoch; + final messages = [message('a1', first), message('a2', first + 60000)]; + expect(transcriptRows(messages).single['created_at'], first); + expect(transcriptRows(messages, running: true).single['created_at'], first); + expect( + transcriptRows([message('bad', 0), ...messages]).single['created_at'], + first, + ); + for (final invalid in [ + null, + 0, + -1, + double.nan, + double.infinity, + 1.5, + 8640000000000001, + '2026-10-20', + ]) { + expect(chatMessageCreatedAt({'created_at': invalid}), isNull); + } + }); + + testWidgets( + 'Cupertino timestamp supports large text without Material localizations', + (tester) async { + _phone(tester); + await tester.pumpWidget( + _host( + _row([ + { + 'id': 'u1', + 'role': 'user', + 'created_at': DateTime.utc( + 2026, + 10, + 20, + 8, + 12, + ).millisecondsSinceEpoch, + 'parts': [ + {'type': 'text', 'text': 'Question'}, + ], + }, + ]), + cupertino: true, + scale: 2, + ), + ); + await tester.pumpAndSettle(); + expect(find.byType(ChatMessageTimestamp), findsOneWidget); + expect(tester.takeException(), isNull); + }, + ); + + testWidgets('shows a local dated timestamp but keeps it out of copy', ( + tester, + ) async { + _phone(tester); + final rows = transcriptRows([ + { + 'id': 'u1', + 'role': 'user', + 'created_at': DateTime.utc(2026, 10, 20, 8, 12).millisecondsSinceEpoch, + 'parts': [ + {'type': 'text', 'text': 'Question'}, + ], + }, + { + 'id': 'a1', + 'role': 'assistant', + 'created_at': DateTime.utc(2026, 10, 20, 8, 13).millisecondsSinceEpoch, + 'parts': [ + {'type': 'text', 'text': 'Answer'}, + ], + }, + ]); + // Use the actual localized formatter rather than asserting a machine's + // timezone or 12/24-hour preference. + await tester.pumpWidget( + MaterialApp( + theme: ThemeData(brightness: Brightness.dark), + home: Scaffold( + body: ListView( + children: [for (final row in rows) ChatMessageView(row: row)], + ), + ), + ), + ); + await tester.pumpAndSettle(); + + expect(find.byType(ChatMessageTimestamp), findsNWidgets(2)); + final context = tester.element(find.byType(ChatMessageTimestamp).first); + final local = DateTime.utc(2026, 10, 20, 8, 12).toLocal(); + final l10n = MaterialLocalizations.of(context); + final expected = + '${l10n.formatCompactDate(local)} · ${l10n.formatTimeOfDay(TimeOfDay.fromDateTime(local), alwaysUse24HourFormat: MediaQuery.alwaysUse24HourFormatOf(context))}'; + expect(find.text(expected), findsOneWidget); + + final copy = tester.widget(find.byType(ChatCopyButton)); + expect(copy.text, 'Answer'); + expect(copy.text, isNot(contains('2026'))); + }); + + testWidgets('omits absent and invalid server times', (tester) async { + _phone(tester); + final rows = transcriptRows([ + { + 'id': 'u1', + 'role': 'user', + 'created_at': 'not a timestamp', + 'parts': [ + {'type': 'text', 'text': 'Question'}, + ], + }, + { + 'id': 'a1', + 'role': 'assistant', + 'parts': [ + {'type': 'text', 'text': 'Answer'}, + ], + }, + ]); + await tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: ListView( + children: [for (final row in rows) ChatMessageView(row: row)], + ), + ), + ), + ); + await tester.pumpAndSettle(); + expect(find.byType(ChatMessageTimestamp), findsNothing); + }); + testWidgets('Material dark user row keeps exact text and attachments at 2x', ( tester, ) async { diff --git a/app/test/features/chat/ui/transcript/chat_transcript_selection_test.dart b/app/test/features/chat/ui/transcript/chat_transcript_selection_test.dart index 952373a5..9693ae36 100644 --- a/app/test/features/chat/ui/transcript/chat_transcript_selection_test.dart +++ b/app/test/features/chat/ui/transcript/chat_transcript_selection_test.dart @@ -2,6 +2,7 @@ import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/features/chat/ui/transcript/chat_link_text.dart'; +import 'package:pipelines_app/features/chat/ui/transcript/chat_message_timestamp.dart'; import 'package:pipelines_app/features/chat/ui/transcript/chat_markdown_view.dart'; import 'package:pipelines_app/features/chat/ui/transcript/chat_transcript_selection.dart'; @@ -30,6 +31,7 @@ void main() { crossAxisAlignment: CrossAxisAlignment.start, children: [ ChatLinkText('User question'), + ChatMessageTimestamp(createdAt: 1792483920000), SizedBox(height: 24), ChatMarkdownView(text: 'Assistant answer.'), ], @@ -49,6 +51,9 @@ void main() { ); await copyTranscriptSelection(tester); expect(copied, hasLength(1)); + expect(copied.single, isNot(contains('2026'))); + expect(copied.single, isNot(contains('AM'))); + expect(copied.single, isNot(contains('PM'))); expect(copied.single, contains('User question')); expect(copied.single, contains('Assistant answer.')); expect( @@ -121,6 +126,7 @@ void main() { crossAxisAlignment: CrossAxisAlignment.start, children: [ ChatLinkText('User question'), + ChatMessageTimestamp(createdAt: 1792483920000), ChatMarkdownView(text: 'Assistant answer.'), ], ), @@ -166,6 +172,7 @@ void main() { crossAxisAlignment: CrossAxisAlignment.start, children: [ const ChatLinkText('User question'), + ChatMessageTimestamp(createdAt: 1792483920000), const ChatMarkdownView(text: 'Assistant answer.'), Text('Activity revision $revision'), ], diff --git a/app/tool/perf/perf_fixture.dart b/app/tool/perf/perf_fixture.dart index 44db6ec0..fb2e5a01 100644 --- a/app/tool/perf/perf_fixture.dart +++ b/app/tool/perf/perf_fixture.dart @@ -291,6 +291,14 @@ JsonMap? perfChatReply(JsonMap request) { { 'id': 'perf-m$i', 'role': i.isEven ? 'user' : 'assistant', + if (_params['timestamps'] == '1') + 'created_at': DateTime.utc( + 2026, + 11, + 5, + 9, + i, + ).millisecondsSinceEpoch, 'parts': [ { 'type': 'text',