diff --git a/app/lib/features/chat/ui/transcript/chat_message_view.dart b/app/lib/features/chat/ui/transcript/chat_message_view.dart new file mode 100644 index 00000000..1c318e12 --- /dev/null +++ b/app/lib/features/chat/ui/transcript/chat_message_view.dart @@ -0,0 +1,195 @@ +import 'dart:convert'; + +import 'package:flutter/cupertino.dart'; +import 'package:flutter/material.dart'; + +import '../../../../core/api_gateway.dart'; +import '../sessions/chat_adaptive.dart'; +import 'native_disclosure.dart'; + +/// Builds an assistant text part; the default keeps the server's text exactly. +typedef ChatMessageTextBuilder = Widget Function(BuildContext, String); + +/// One `transcriptRows` row: a user message or one folded agent turn. Parts keep +/// order; reasoning/tool bodies are native disclosures and unknown parts keep raw JSON. +class ChatMessageView extends StatelessWidget { + const ChatMessageView({super.key, required this.row, this.textBuilder}); + + final JsonMap row; + final ChatMessageTextBuilder? textBuilder; + + @override + Widget build(BuildContext context) { + final user = row['kind'] == 'user'; + final role = user ? 'User' : 'Assistant'; + final cupertino = _isCupertino(context); + final content = user ? _userParts(context) : _agentParts(context); + final body = Column( + mainAxisSize: MainAxisSize.min, + crossAxisAlignment: CrossAxisAlignment.stretch, + children: [ + // The row's semantics label already says the role; announcing the + // visible label again would read it twice. + ExcludeSemantics( + child: Text(role, style: _roleStyle(context, cupertino)), + ), + ...content, + ], + ); + return Semantics( + container: true, + explicitChildNodes: true, + label: '$role message', + child: Padding( + padding: const EdgeInsets.symmetric(vertical: 8), + child: cupertino + ? DefaultTextStyle.merge( + style: CupertinoTheme.of(context).textTheme.textStyle, + child: body, + ) + : body, + ), + ); + } + + TextStyle? _roleStyle(BuildContext context, bool cupertino) => + (cupertino + ? CupertinoTheme.of(context).textTheme.textStyle + : Theme.of(context).textTheme.labelLarge) + ?.copyWith( + fontWeight: FontWeight.w600, + color: _secondaryColor(context, cupertino), + ); + + Color _secondaryColor(BuildContext context, bool cupertino) => cupertino + ? CupertinoColors.secondaryLabel.resolveFrom(context) + : Theme.of(context).colorScheme.onSurfaceVariant; + + bool _isCupertino(BuildContext context) => + chatControlsStyleOf(context) == ChatControlsStyle.cupertino; + + List _userParts(BuildContext context) { + final message = row['message'] is Map + ? Map.from(row['message'] as Map) + : row; + final parts = _partsOf(message); + final text = parts + .where((part) => part['type'] == null || part['type'] == 'text') + .map((part) => '${part['text'] ?? ''}') + .join('\n'); + final widgets = [ + if (text.trim().isNotEmpty) SelectableText(text), + for (final part in parts.where(_isFile)) _attachment(context, part), + ]; + final client = '${message['client'] ?? ''}'.trim(); + if (client.isNotEmpty) { + widgets.insert(0, Text(client, style: _clientStyle(context))); + } + return widgets; + } + + List _agentParts(BuildContext context) { + final widgets = []; + for (final part in _partsOf(row)) { + final widget = _agentPart(context, part); + if (widget != null) widgets.add(widget); + } + return widgets; + } + + /// One agent part, or null when a known part has nothing to draw. The web's + /// `partRendering` hides blank text/reasoning too, so a blank known part must + /// not fall through to the unknown-part disclosure. + Widget? _agentPart(BuildContext context, JsonMap part) { + final type = '${part['type'] ?? ''}'; + final text = part['text'] is String ? part['text'] as String : ''; + final hasText = text.trim().isNotEmpty; + if (type.isEmpty || type == 'text') { + if (!hasText) return type.isEmpty ? _unrecognised(part) : null; + return textBuilder?.call(context, text) ?? SelectableText(text); + } + if (type == 'reasoning') { + if (!hasText) return null; + return NativeDisclosure( + label: 'Reasoning', + childBuilder: (context) => SelectableText(text), + ); + } + if (type == 'tool') { + return NativeDisclosure( + label: _toolLabel(part), + childBuilder: (context) => _raw(part), + ); + } + if (type == 'file') { + if (_isFile(part)) return _attachment(context, part); + return hasText ? SelectableText(text) : null; + } + if (hasText) return SelectableText(text); + return _unrecognised(part); + } + + Widget _unrecognised(JsonMap part) { + final type = '${part['type'] ?? ''}'; + return NativeDisclosure( + label: 'Unrecognised part${type.isEmpty ? '' : ': $type'}', + childBuilder: (context) => _raw(part), + ); + } + + TextStyle? _clientStyle(BuildContext context) { + final cupertino = _isCupertino(context); + return (cupertino + ? CupertinoTheme.of(context).textTheme.textStyle + : Theme.of(context).textTheme.bodySmall) + ?.copyWith(color: _secondaryColor(context, cupertino)); + } + + Widget _attachment(BuildContext context, JsonMap part) { + final cupertino = _isCupertino(context); + return Row( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Padding( + padding: const EdgeInsets.only(right: 8), + child: Icon(cupertino ? CupertinoIcons.paperclip : Icons.attach_file), + ), + Expanded( + child: SelectableText( + _attachmentDisplayName(part['name'], part['uri']), + ), + ), + ], + ); + } + + /// Pretty-printed on demand: a closed disclosure never pays for it. + Widget _raw(JsonMap part) => + SelectableText(const JsonEncoder.withIndent(' ').convert(part)); +} + +List _partsOf(Map source) { + final parts = source['parts']; + if (parts is! List) return const []; + return parts + .whereType() + .map((part) => Map.from(part)) + .toList(); +} + +bool _isFile(JsonMap part) => + part['type'] == 'file' && + '${part['name'] ?? ''}${part['uri'] ?? ''}'.trim().isNotEmpty; + +String _toolLabel(JsonMap part) { + final name = '${part['name'] ?? ''}'.trim(); + return name.isEmpty ? 'tool' : name; +} + +/// The web's attachment name rule: its own name, else the URI basename, else +/// `file`, so an attachment never shows an empty label. +String _attachmentDisplayName(Object? name, Object? uri) { + final shown = '${name ?? ''}'.trim(); + final basename = '${uri ?? ''}'.split('/').last.trim(); + return shown.isNotEmpty ? shown : (basename.isEmpty ? 'file' : basename); +} diff --git a/app/lib/features/chat/ui/transcript/native_disclosure.dart b/app/lib/features/chat/ui/transcript/native_disclosure.dart new file mode 100644 index 00000000..29ab9436 --- /dev/null +++ b/app/lib/features/chat/ui/transcript/native_disclosure.dart @@ -0,0 +1,79 @@ +import 'package:flutter/cupertino.dart'; +import 'package:flutter/material.dart'; + +import '../sessions/chat_adaptive.dart'; + +/// A collapsed block opened with the host's own disclosure control. +/// +/// [childBuilder] runs only while the block is open, so a closed disclosure +/// never encodes or lays out its body. +class NativeDisclosure extends StatefulWidget { + const NativeDisclosure({ + super.key, + required this.label, + required this.childBuilder, + }); + + final String label; + final WidgetBuilder childBuilder; + + @override + State createState() => _NativeDisclosureState(); +} + +class _NativeDisclosureState extends State { + bool _expanded = false; + + void _toggle() => setState(() => _expanded = !_expanded); + + @override + Widget build(BuildContext context) { + if (chatControlsStyleOf(context) != ChatControlsStyle.cupertino) { + return ExpansionTile( + title: Text(widget.label), + maintainState: false, + // A transcript block starts at the row edge, like its answer text; the + // tile's own padding would indent it, and its body defaults to centred, + // which floats a short body in the middle of the row. + tilePadding: EdgeInsets.zero, + expandedAlignment: AlignmentDirectional.centerStart, + expandedCrossAxisAlignment: CrossAxisAlignment.stretch, + children: [Builder(builder: widget.childBuilder)], + ); + } + return Column( + mainAxisSize: MainAxisSize.min, + crossAxisAlignment: CrossAxisAlignment.stretch, + children: [ + // One node carries the label, the button action and the state; the + // Cupertino button's own node would otherwise drop the state. + Semantics( + container: true, + button: true, + expanded: _expanded, + label: widget.label, + onTap: _toggle, + child: ExcludeSemantics( + child: CupertinoButton( + padding: EdgeInsets.zero, + onPressed: _toggle, + child: Row( + children: [ + Expanded( + child: Text(widget.label, textAlign: TextAlign.start), + ), + Icon( + _expanded + ? CupertinoIcons.chevron_up + : CupertinoIcons.chevron_down, + ), + ], + ), + ), + ), + ), + if (_expanded) widget.childBuilder(context), + ], + ); + } +} diff --git a/app/notes/C2-message.md b/app/notes/C2-message.md new file mode 100644 index 00000000..18f7d3f9 --- /dev/null +++ b/app/notes/C2-message.md @@ -0,0 +1,163 @@ +# C2 message — one native transcript row + +The first transcript slice renders one row already produced by +`transcriptRows` (`lib/features/chat/domain/chat_transcript.dart`). It is a pure +widget with no gateway, history, network, composer, shell or chat-screen wiring. + +This visit fixed the two blocking review findings and the actionable advisories: +Material disclosure bodies are no longer centred, blank known-type parts no +longer read as `Unrecognised`, closed bodies are no longer encoded, and the +Cupertino disclosure now reports its expanded state. + +## Behavior + +- The visible role (`User`/`Assistant`) identifies the row and a single + `User message`/`Assistant message` semantics group carries it. The visible role + label is excluded from semantics so a screen reader hears the role once. Role + text uses the host's semantic secondary colour, not a hardcoded colour or font + family. +- A user row joins its text parts with the web's newline rule and keeps the exact + string in a `SelectableText`, including leading/trailing spaces. Its optional + `client` label is shown. `file` parts show the web's + `attachmentDisplayName` fallback (name, else URI basename, else `file`), also + selectable. +- An agent turn draws every part in its original order. Text parts are selectable; + `reasoning` and `tool` parts are collapsed native disclosures. A non-text part + of an unknown type gets `Unrecognised part: ` (or `Unrecognised part`) + with the full pretty-printed raw JSON behind the disclosure, so no field is + silently dropped. +- Blank known parts draw nothing, as the web's `partRendering` does: empty + `text`/`reasoning`, and `file` parts with neither a name nor a URI. The + `Unrecognised part` fallback is reserved for unknown types (and for a part with + no type at all, which keeps its raw JSON). +- `textBuilder` is the seam for the forthcoming markdown renderer. It receives + each assistant text part; when absent, the default is the untouched + `SelectableText`. +- Material uses `ExpansionTile`; Cupertino uses a `CupertinoButton` with the + standard chevron. No custom-painted control, fixed component size, custom font + or palette was added. +- Material disclosures set `tilePadding: EdgeInsets.zero` and + `expandedAlignment: AlignmentDirectional.centerStart` with a stretched body: + the label and a short body both start at the row edge (the tile's default + padding indented the label and its body defaulted to centred). Cupertino + already started both at the row edge. +- `NativeDisclosure` takes a `childBuilder` and only calls it while open, so a + collapsed tool/unknown body never runs `JsonEncoder.withIndent`. +- The Cupertino disclosure wraps `CupertinoButton` in one `Semantics` node with + the label, button action and `expanded` state; `ExcludeSemantics` stops the + child button from replacing that node. The browser exposes + `aria-expanded=false` → `true`. + +## Files + +| Path | Lines | Role | +| --- | ---: | --- | +| `lib/features/chat/ui/transcript/chat_message_view.dart` | 195 | Row composition, part rules, attachment name | +| `lib/features/chat/ui/transcript/native_disclosure.dart` | 79 | Native Material/Cupertino disclosure | +| `test/features/chat/ui/transcript/chat_message_view_test.dart` | 321 | Ten row tests | +| `test/features/chat/ui/transcript/native_disclosure_test.dart` | 64 | Two lazy-body tests | +| `tool/chat_message_probe.dart` | 82 | Material/Cupertino dark fixture | +| `tool/chat_message_probe.mjs` | 386 | Reproducible Chrome + OCR probe | + +Production total is 274 lines. No tracked file, domain file, shell file, pubspec +or `main.dart` changed. `git status --short` shows only new paths, including this +note and the probe driver. + +## Checks (actual) + +- `flutter test test/features/chat/ui/transcript`: 12 tests passed (10 view, 2 + disclosure). +- `flutter test test/features/chat`: 45 tests passed. +- `flutter analyze`: no issues. +- `flutter build web` for both probe hosts (Material and Cupertino defines): + built successfully. +- `node tool/chat_message_probe.mjs` for both hosts: real headless Chrome, + loopback OS-assigned port, dark appearance, 390x844 at DPR 2. It asserted the + role groups, client, all three disclosures, the collapsed bodies' absence + before tapping, the expanded reasoning/tool/unknown text after tapping, and + each body's leading edge within 2 device px of its label (Material 35/33, + 34/32, 34/32; Cupertino 34/32, 33/32, 34/32). Cupertino also reported + `aria-expanded=false` before and `true` after. Captures were inspected. +- The review repro `review_findings_test.dart` now passes F1 and F2 and fails only + F4, whose assertion expects the raw boolean `true`; the semantics data reports + `Tristate.isTrue`, and the candidate's own test asserts + `Tristate.isFalse` → `Tristate.isTrue`. F3 printed 112 ms before the fix and + a warm 44 ms for the 1 MiB collapsed body after it (small 53 ms), i.e. no + per-byte cost remains. + +Widget-test conditions: the two user-row tests pump a dark `MaterialApp` and a +dark `CupertinoApp` at 2.0x text on 390x844 and assert no exception. The agent +order test uses Material at 2.0x on 390x844. The alignment test runs Material at +1x and 2x on 390x844 (resetting the tree between scales). The remaining tests use +the default 800x600 surface at 1x, with the Cupertino disclosure tests in a +Cupertino app. The builder-seam test uses a plain light `MaterialApp`. No test +asserts a fixed row width or height. + +## Browser evidence + +Captures under `/Users/alan/.local/share/pipelines/data/artifacts/70/public/c2-message/`: + +- `material-top.png`, `material-reasoning-expanded.png`, + `material-tool-expanded.png`, `material-unknown-expanded.png` +- `cupertino-top.png`, `cupertino-reasoning-expanded.png`, + `cupertino-tool-expanded.png`, `cupertino-unknown-expanded.png` +- `material-evidence.json`, `cupertino-evidence.json` + +These replace the pre-fix captures: the old Material expanded shots showed the +centred JSON body and are no longer current. The six earlier files with the same +names are overwritten; the two `*-tool-expanded.png` files and both evidence +files are new. + +OCR is imperfect on the small Cupertino paperclip glyph (`‘eport.csv`); the image +shows `report.csv`, the probe asserts the stable `eport.csv` suffix, and exact +names are covered by the widget test. Flutter web paints `SelectableText` on the +canvas, so browser assertions use OCR for body text and the semantics DOM for +controls, as the existing Runs probe does. + +## Review findings this visit + +- Fixed: Material disclosure body centring (blocking). Regression asserts the + body's `dx` equals the label's at 1x/2x and for the unknown JSON body, plus a + browser leading-edge measurement per host. +- Fixed: blank `text`/`reasoning` parts (blocking). The fallback now applies only + to unknown types. Regression covers blank text, whitespace text, blank + reasoning, a nameless file part, and the still-visible unknown part. +- Addressed: eager JSON encoding of collapsed bodies (advisory). `childBuilder` + is lazy; two tests count body builds while closed and open. +- Addressed: Cupertino expanded state (advisory). One semantics node now reports + `expanded`, and a widget test plus the browser `aria-expanded` check cover it. +- Addressed: inaccurate notes (advisory) — the test conditions above are the + actual ones. +- Addressed: browser evidence not reproducible (advisory) — the CDP+OCR driver is + now `tool/chat_message_probe.mjs`, matching the existing `*_probe.dart` + + `*_probe.mjs` pairs. +- Addressed: duplicate role announcement (advisory) — the visible role label is + excluded from semantics; a test asserts the group label and the absence of a + second `Assistant` label. +- Hardened: a non-list `parts` value is ignored instead of throwing. +- Not changed (recorded for integration): the transcript still imports + `../sessions/chat_adaptive.dart`; promoting the chat-wide helper is an + integration decision outside this lane. Expanded tool/unknown bodies remain + unbounded single selectables (rich tool cards are later work). Agent `file` + parts still render as attachments (web draws only answer/reasoning/tools). + User text remains verbatim rather than web-trimmed (declared acceptance). + `transcriptRows` still emits an assistant row for an all-blank turn, unlike the + web's visible-turn rule; the transcript-list wiring must account for that. + +## Not implemented in this slice + +- Markdown rendering is not implemented. Assistant text is shown verbatim; the + `textBuilder` seam is the handoff for the later renderer. +- Rich tool cards are not implemented. Tool bodies are raw JSON, collapsed, as + agreed for this slice. Large outputs are not bounded yet. +- The reasoning label is always `Reasoning`; the web's streaming `Thinking…` and + `· N steps` wording, elapsed times and tool duration/status cards remain later + work. +- No transcript list, history loading, live frames, composer, approvals, forms, + attachments opening/downloading, shell registration or chat screen is included. +- Mobile was not built, run or installed; only web was exercised. Source uses + standard portable widgets. +- No commit, merge, push or deployment was performed; the new paths are the + uncommitted candidate. + +Full unified diff: `/Users/alan/.local/share/pipelines/data/artifacts/70/public/c2-message/c2-message.diff`. 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 new file mode 100644 index 00000000..d644229d --- /dev/null +++ b/app/test/features/chat/ui/transcript/chat_message_view_test.dart @@ -0,0 +1,321 @@ +import 'dart:ui' show Tristate; + +import 'package:flutter/cupertino.dart'; +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/domain/chat_transcript.dart'; +import 'package:pipelines_app/features/chat/ui/transcript/chat_message_view.dart'; + +JsonMap _row(List messages) => transcriptRows(messages).single; + +JsonMap _assistant(List parts) => _row([ + {'id': 'a1', 'role': 'assistant', 'parts': parts}, +]); + +JsonMap _userRow() => _row([ + { + 'id': 'u1', + 'role': 'user', + 'client': 'Ada phone', + 'parts': [ + {'type': 'text', 'text': ' Keep this exact\nline two '}, + {'type': 'file', 'name': 'plan.md', 'uri': '/tmp/plan.md'}, + {'type': 'file', 'uri': '/tmp/report.csv'}, + ], + }, +]); + +Widget _host(JsonMap row, {bool cupertino = false, double scale = 1}) { + final body = SafeArea( + child: ListView(children: [ChatMessageView(row: row)]), + ); + Widget builder(BuildContext context, Widget? child) => MediaQuery( + data: MediaQuery.of(context).copyWith(textScaler: TextScaler.linear(scale)), + child: child!, + ); + if (cupertino) { + return CupertinoApp( + theme: const CupertinoThemeData(brightness: Brightness.dark), + builder: builder, + home: CupertinoPageScaffold(child: body), + ); + } + return MaterialApp( + theme: ThemeData( + brightness: Brightness.dark, + platform: TargetPlatform.android, + ), + builder: builder, + home: Scaffold(body: body), + ); +} + +void _phone(WidgetTester tester) { + tester.view.physicalSize = const Size(390, 844); + tester.view.devicePixelRatio = 1; + addTearDown(tester.view.resetPhysicalSize); + addTearDown(tester.view.resetDevicePixelRatio); +} + +void main() { + testWidgets('Material dark user row keeps exact text and attachments at 2x', ( + tester, + ) async { + _phone(tester); + await tester.pumpWidget(_host(_userRow(), scale: 2)); + await tester.pumpAndSettle(); + + expect(find.text('User'), findsOneWidget); + expect(find.text('Ada phone'), findsOneWidget); + expect(find.text(' Keep this exact\nline two '), findsOneWidget); + expect(find.text('plan.md'), findsOneWidget); + expect(find.text('report.csv'), findsOneWidget); + final role = tester.widget(find.text('User')); + final theme = Theme.of(tester.element(find.byType(ChatMessageView))); + expect(theme.brightness, Brightness.dark); + expect(role.style?.color, theme.colorScheme.onSurfaceVariant); + expect(role.style?.color?.computeLuminance(), greaterThan(0.4)); + expect(tester.takeException(), isNull); + }); + + testWidgets( + 'Cupertino dark user row keeps exact text and attachments at 2x', + (tester) async { + _phone(tester); + await tester.pumpWidget(_host(_userRow(), cupertino: true, scale: 2)); + await tester.pumpAndSettle(); + + expect(find.text('User'), findsOneWidget); + expect(find.text(' Keep this exact\nline two '), findsOneWidget); + expect(find.text('plan.md'), findsOneWidget); + expect(find.text('report.csv'), findsOneWidget); + final context = tester.element(find.byType(ChatMessageView)); + expect(CupertinoTheme.of(context).brightness, Brightness.dark); + final role = tester.widget(find.text('User')); + expect(role.style?.color?.computeLuminance(), greaterThan(0.4)); + expect(tester.takeException(), isNull); + }, + ); + + testWidgets('agent parts retain order and disclosures reveal native bodies', ( + tester, + ) async { + _phone(tester); + final row = _assistant([ + {'type': 'text', 'text': 'First answer'}, + {'type': 'reasoning', 'text': 'private reasoning'}, + { + 'type': 'tool', + 'name': 'shell', + 'state': { + 'status': 'completed', + 'input': {'command': 'ls'}, + }, + }, + {'type': 'text', 'text': 'Second answer'}, + {'type': 'future_text', 'text': 'plain future text'}, + {'type': 'future_part', 'payload': 7}, + ]); + await tester.pumpWidget(_host(row, scale: 2)); + await tester.pumpAndSettle(); + + expect(find.text('Assistant'), findsOneWidget); + expect(find.text('private reasoning'), findsNothing); + expect(find.textContaining('"command": "ls"'), findsNothing); + expect(find.textContaining('"payload": 7'), findsNothing); + expect(find.text('plain future text'), findsOneWidget); + + double top(String text) => tester.getTopLeft(find.text(text)).dy; + expect(top('First answer'), lessThan(top('Reasoning'))); + expect(top('Reasoning'), lessThan(top('shell'))); + expect(top('shell'), lessThan(top('Second answer'))); + expect( + top('Second answer'), + lessThan(top('Unrecognised part: future_part')), + ); + + await tester.ensureVisible(find.text('Reasoning')); + await tester.pumpAndSettle(); + await tester.tap(find.text('Reasoning')); + await tester.pumpAndSettle(); + expect(find.text('private reasoning'), findsOneWidget); + + await tester.ensureVisible(find.text('shell')); + await tester.pumpAndSettle(); + await tester.tap(find.text('shell')); + await tester.pumpAndSettle(); + expect(find.textContaining('"command": "ls"'), findsOneWidget); + + await tester.ensureVisible(find.text('Unrecognised part: future_part')); + await tester.pumpAndSettle(); + await tester.tap(find.text('Unrecognised part: future_part')); + await tester.pumpAndSettle(); + expect(find.textContaining('"payload": 7'), findsOneWidget); + }); + + testWidgets( + 'Material disclosure bodies start at the label edge at 1x and 2x', + (tester) async { + for (final scale in [1.0, 2.0]) { + _phone(tester); + // A fresh tree per scale so the disclosure starts collapsed again. + await tester.pumpWidget(const SizedBox.shrink()); + await tester.pumpWidget( + _host( + _assistant([ + {'type': 'reasoning', 'text': 'short thought'}, + ]), + scale: scale, + ), + ); + await tester.pumpAndSettle(); + await tester.tap(find.text('Reasoning')); + await tester.pumpAndSettle(); + + final label = tester.getTopLeft(find.text('Reasoning')); + final body = tester.getTopLeft(find.text('short thought')); + expect(body.dx, label.dx, reason: 'short reasoning body at ${scale}x'); + expect(find.text('short thought'), findsOneWidget); + } + }, + ); + + testWidgets('unknown disclosure bodies also start at the label edge', ( + tester, + ) async { + await tester.pumpWidget( + _host( + _assistant([ + {'type': 'future_part', 'payload': 7}, + ]), + ), + ); + await tester.pumpAndSettle(); + final label = find.text('Unrecognised part: future_part'); + await tester.tap(label); + await tester.pumpAndSettle(); + + expect( + tester.getTopLeft(find.textContaining('"payload": 7')).dx, + tester.getTopLeft(label).dx, + ); + }); + + testWidgets('blank known-type parts draw nothing but unknown parts stay', ( + tester, + ) async { + await tester.pumpWidget( + _host( + _assistant([ + {'type': 'text', 'text': ''}, + {'type': 'text', 'text': ' '}, + {'type': 'reasoning', 'text': ''}, + {'type': 'file'}, + {'type': 'future_part', 'payload': 7}, + ]), + ), + ); + await tester.pumpAndSettle(); + + for (final label in [ + 'Unrecognised part: text', + 'Unrecognised part: reasoning', + 'Unrecognised part: file', + ]) { + expect(find.text(label), findsNothing, reason: label); + } + expect(find.text('Unrecognised part: future_part'), findsOneWidget); + // The blank parts leave only the visible role label and that disclosure. + expect(find.byType(SelectableText), findsNothing); + }); + + testWidgets('Cupertino disclosure expands with the native control', ( + tester, + ) async { + final row = _assistant([ + {'type': 'reasoning', 'text': 'thought'}, + { + 'type': 'tool', + 'name': 'read', + 'state': {'status': 'error'}, + }, + ]); + await tester.pumpWidget(_host(row, cupertino: true)); + await tester.pumpAndSettle(); + + expect(find.text('thought'), findsNothing); + await tester.tap(find.text('Reasoning')); + await tester.pumpAndSettle(); + expect(find.text('thought'), findsOneWidget); + + await tester.tap(find.text('read')); + await tester.pumpAndSettle(); + expect(find.textContaining('"status": "error"'), findsOneWidget); + }); + + testWidgets('Cupertino disclosure announces its expanded state', ( + tester, + ) async { + final handle = tester.ensureSemantics(); + await tester.pumpWidget( + _host( + _assistant([ + {'type': 'reasoning', 'text': 'thought'}, + ]), + cupertino: true, + ), + ); + await tester.pumpAndSettle(); + + Tristate state() => tester + .getSemantics(find.bySemanticsLabel('Reasoning')) + .getSemanticsData() + .flagsCollection + .isExpanded; + expect(state(), Tristate.isFalse); + + await tester.tap(find.text('Reasoning')); + await tester.pumpAndSettle(); + expect(state(), Tristate.isTrue); + handle.dispose(); + }); + + testWidgets('row semantics announce the role once', (tester) async { + final handle = tester.ensureSemantics(); + await tester.pumpWidget( + _host( + _assistant([ + {'type': 'text', 'text': 'Answer'}, + ]), + ), + ); + await tester.pumpAndSettle(); + + expect(find.bySemanticsLabel('Assistant message'), findsOneWidget); + expect(find.bySemanticsLabel('Assistant'), findsNothing); + final answer = tester.getSemantics(find.text('Answer')).getSemanticsData(); + expect(answer.value, 'Answer'); + handle.dispose(); + }); + + testWidgets('the text builder seam replaces only assistant text', ( + tester, + ) async { + final row = _assistant([ + {'type': 'text', 'text': '**markdown**'}, + ]); + await tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: ChatMessageView( + row: row, + textBuilder: (context, text) => Text('Rendered $text'), + ), + ), + ), + ); + expect(find.text('Rendered **markdown**'), findsOneWidget); + expect(find.text('**markdown**'), findsNothing); + }); +} diff --git a/app/test/features/chat/ui/transcript/native_disclosure_test.dart b/app/test/features/chat/ui/transcript/native_disclosure_test.dart new file mode 100644 index 00000000..411023fd --- /dev/null +++ b/app/test/features/chat/ui/transcript/native_disclosure_test.dart @@ -0,0 +1,64 @@ +import 'package:flutter/cupertino.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:pipelines_app/features/chat/ui/transcript/native_disclosure.dart'; + +Widget _material(WidgetBuilder body) => MaterialApp( + theme: ThemeData(brightness: Brightness.dark), + home: Scaffold( + body: NativeDisclosure(label: 'Body', childBuilder: body), + ), +); + +Widget _cupertino(WidgetBuilder body) => CupertinoApp( + theme: const CupertinoThemeData(brightness: Brightness.dark), + home: CupertinoPageScaffold( + child: NativeDisclosure(label: 'Body', childBuilder: body), + ), +); + +void main() { + testWidgets('closed Material disclosure never builds its body', ( + tester, + ) async { + var builds = 0; + await tester.pumpWidget( + _material((context) { + builds++; + return const Text('body'); + }), + ); + await tester.pumpAndSettle(); + expect(builds, 0); + expect(find.text('body'), findsNothing); + + await tester.tap(find.text('Body')); + await tester.pumpAndSettle(); + expect(builds, greaterThan(0)); + expect(find.text('body'), findsOneWidget); + }); + + testWidgets('closed Cupertino disclosure never builds its body', ( + tester, + ) async { + var builds = 0; + await tester.pumpWidget( + _cupertino((context) { + builds++; + return const Text('body'); + }), + ); + await tester.pumpAndSettle(); + expect(builds, 0); + expect(find.text('body'), findsNothing); + + await tester.tap(find.text('Body')); + await tester.pumpAndSettle(); + expect(builds, 1); + expect(find.text('body'), findsOneWidget); + + await tester.tap(find.text('Body')); + await tester.pumpAndSettle(); + expect(find.text('body'), findsNothing); + }); +} diff --git a/app/tool/chat_message_probe.dart b/app/tool/chat_message_probe.dart new file mode 100644 index 00000000..89150fbb --- /dev/null +++ b/app/tool/chat_message_probe.dart @@ -0,0 +1,82 @@ +import 'package:flutter/cupertino.dart'; +import 'package:flutter/material.dart'; + +import 'package:pipelines_app/features/chat/domain/chat_transcript.dart'; +import 'package:pipelines_app/features/chat/ui/transcript/chat_message_view.dart'; + +const _style = String.fromEnvironment( + 'CHAT_MESSAGE_PROBE_STYLE', + defaultValue: 'material', +); + +/// Visible fixture for one user row and one folded assistant turn. The unknown +/// part and the tool body start collapsed so the browser probe has to open them. +void main() { + final rows = transcriptRows([ + { + 'id': 'probe-user', + 'role': 'user', + 'client': 'Ada phone', + 'parts': [ + { + 'type': 'text', + 'text': + 'Please keep every word of this prompt, including the trailing ' + 'detail that wraps onto another line at a narrow width.', + }, + {'type': 'file', 'name': 'plan.md', 'uri': '/workspace/plan.md'}, + {'type': 'file', 'uri': '/workspace/results/report.csv'}, + ], + }, + { + 'id': 'probe-assistant', + 'role': 'assistant', + 'parts': [ + { + 'type': 'text', + 'text': 'I checked the transcript and kept the original order.', + }, + { + 'type': 'reasoning', + 'text': + 'The user asked for exact text, so the raw string stays as sent.', + }, + { + 'type': 'tool', + 'name': 'shell', + 'state': { + 'status': 'completed', + 'input': {'command': 'ls -la'}, + }, + }, + {'type': 'text', 'text': 'Second answer line after the tool call.'}, + { + 'type': 'future_part', + 'payload': {'kept': true, 'count': 2}, + }, + ], + }, + ]); + final content = ListView( + padding: const EdgeInsets.all(16), + children: [for (final row in rows) ChatMessageView(row: row)], + ); + if (_style == 'cupertino') { + runApp( + CupertinoApp( + theme: const CupertinoThemeData(brightness: Brightness.dark), + home: CupertinoPageScaffold(child: SafeArea(child: content)), + ), + ); + return; + } + runApp( + MaterialApp( + theme: ThemeData( + brightness: Brightness.dark, + platform: TargetPlatform.android, + ), + home: Scaffold(body: SafeArea(child: content)), + ), + ); +} diff --git a/app/tool/chat_message_probe.mjs b/app/tool/chat_message_probe.mjs new file mode 100644 index 00000000..ccc77d2f --- /dev/null +++ b/app/tool/chat_message_probe.mjs @@ -0,0 +1,386 @@ +// Real-browser evidence for the C2 message-view fixture. +// +// Build one host, then run from app/: +// flutter build web -t tool/chat_message_probe.dart \ +// --dart-define=CHAT_MESSAGE_PROBE_STYLE=material \ +// --output build/probe-chat-message-material +// node tool/chat_message_probe.mjs --root build/probe-chat-message-material \ +// --out DIR --style material +// +// The Cupertino host is the same build with CHAT_MESSAGE_PROBE_STYLE=cupertino. +// Requires Google Chrome and Tesseract: Flutter web paints selectable text on +// the canvas, so OCR checks the rendered body and its leading edge while the +// semantics tree checks the controls and the expanded state. +import { createServer } from 'node:http'; +import { mkdir, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises'; +import { spawn, execFile } from 'node:child_process'; +import { once } from 'node:events'; +import { promisify } from 'node:util'; +import { setTimeout as delay } from 'node:timers/promises'; +import { extname, join, resolve, sep } from 'node:path'; +import { tmpdir } from 'node:os'; + +const args = new Map(); +for (let i = 2; i < process.argv.length; i++) { + const value = process.argv[i]; + if (value.startsWith('--')) args.set(value.slice(2), process.argv[++i]); +} +const root = resolve(args.get('root') ?? 'build/probe-chat-message-material'); +const out = resolve(args.get('out') ?? 'build/chat-message-probe'); +const style = args.get('style') ?? 'material'; +const chromePath = + process.env.CHROME_PATH ?? + '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'; +await mkdir(out, { recursive: true }); + +const mime = { + '.html': 'text/html; charset=utf-8', + '.js': 'text/javascript; charset=utf-8', + '.mjs': 'text/javascript; charset=utf-8', + '.json': 'application/json; charset=utf-8', + '.wasm': 'application/wasm', + '.png': 'image/png', + '.svg': 'image/svg+xml', + '.ico': 'image/x-icon', + '.ttf': 'font/ttf', + '.otf': 'font/otf', + '.woff': 'font/woff', + '.woff2': 'font/woff2', + '.bin': 'application/octet-stream', +}; + +const server = createServer(async (req, res) => { + try { + const pathname = decodeURIComponent( + new URL(req.url, 'http://localhost').pathname, + ); + let filePath = resolve(root, '.' + pathname); + if (filePath !== root && !filePath.startsWith(root + sep)) { + res.writeHead(403).end(); + return; + } + if (pathname === '/' || pathname.endsWith('/')) { + filePath = join(filePath, 'index.html'); + } + const file = await readFile(filePath); + const type = mime[extname(filePath)] ?? 'application/octet-stream'; + res.writeHead(200, { 'content-type': type }).end(file); + } catch { + res.writeHead(404).end('not found'); + } +}); +server.listen(0, '127.0.0.1'); +await once(server, 'listening'); +const origin = `http://127.0.0.1:${server.address().port}`; + +const profile = await mkdtemp(join(tmpdir(), 'c2-message-chrome-')); +const chrome = spawn( + chromePath, + [ + '--headless=new', + '--no-first-run', + '--no-default-browser-check', + '--disable-gpu', + '--remote-debugging-port=0', + `--user-data-dir=${profile}`, + 'about:blank', + ], + { stdio: ['ignore', 'ignore', 'pipe'] }, +); +let stderr = ''; +chrome.stderr.on('data', (data) => { + stderr += data; +}); +const waitFor = async (fn, what) => { + for (let i = 0; i < 150; i++) { + const value = await fn(); + if (value) return value; + await delay(100); + } + throw new Error(`Timed out waiting for ${what}: ${stderr.slice(-1500)}`); +}; + +let ws; +const evidence = { origin, style, captures: [], checks: [] }; +try { + const endpoint = await waitFor( + () => stderr.match(/DevTools listening on (ws:\/\/[^\s]+)/)?.[1], + 'Chrome DevTools', + ); + ws = new WebSocket(endpoint); + await once(ws, 'open'); + let id = 0; + const pending = new Map(); + ws.addEventListener('message', (event) => { + const message = JSON.parse(event.data); + if (message.id && pending.has(message.id)) { + pending.get(message.id)(message); + pending.delete(message.id); + } + }); + const send = (method, params = {}, sessionId) => + new Promise((resolveCall) => { + const callId = ++id; + pending.set(callId, resolveCall); + ws.send( + JSON.stringify({ + id: callId, + method, + params, + ...(sessionId ? { sessionId } : {}), + }), + ); + }); + + const created = await send('Target.createTarget', { url: 'about:blank' }); + if (!created.result) { + throw new Error(`Target creation failed: ${JSON.stringify(created)}`); + } + const target = created.result.targetId; + const session = ( + await send('Target.attachToTarget', { targetId: target, flatten: true }) + ).result.sessionId; + await send('Page.enable', {}, session); + await send('Runtime.enable', {}, session); + await send( + 'Emulation.setDeviceMetricsOverride', + { width: 390, height: 844, deviceScaleFactor: 2, mobile: true }, + session, + ); + await send( + 'Emulation.setEmulatedMedia', + { features: [{ name: 'prefers-color-scheme', value: 'dark' }] }, + session, + ); + + const evaluate = async (expression) => { + const response = await send( + 'Runtime.evaluate', + { expression, returnByValue: true, awaitPromise: true }, + session, + ); + if (response.error) { + throw new Error(`Runtime.evaluate failed: ${JSON.stringify(response)}`); + } + return response.result?.result?.value; + }; + const nodes = () => + evaluate(`[...document.querySelectorAll('flt-semantics')].map(x => { + const r = x.getBoundingClientRect(); + return { + label: x.getAttribute('aria-label'), + text: (x.innerText || '').trim(), + expanded: x.getAttribute('aria-expanded'), + role: x.getAttribute('role'), + x: r.x, y: r.y, w: r.width, h: r.height, + }; + })`); + const findNode = async (name) => { + const all = await nodes(); + return all.find( + (node) => node.label === name || node.text === name, + ); + }; + const requireNode = async (name) => { + const node = await findNode(name); + if (!node) { + const shown = (await nodes()) + .map((entry) => entry.label ?? entry.text) + .filter(Boolean); + throw new Error(`Missing semantics ${JSON.stringify(name)}: ${JSON.stringify(shown)}`); + } + return node; + }; + const click = async (name) => { + const clicked = await evaluate(`(() => { + const wanted = ${JSON.stringify(name)}; + const all = [...document.querySelectorAll('flt-semantics')]; + const node = all.find(x => x.getAttribute('aria-label') === wanted) + || all.find(x => (x.innerText || '').trim() === wanted); + if (!node) return false; + node.click(); + return true; + })()`); + if (!clicked) { + const shown = (await nodes()) + .map((entry) => entry.label ?? entry.text) + .filter(Boolean); + throw new Error(`Could not click ${JSON.stringify(name)}: ${JSON.stringify(shown)}`); + } + await delay(450); + }; + const capture = async (name) => { + const data = ( + await send('Page.captureScreenshot', { format: 'png' }, session) + ).result.data; + const file = join(out, `${name}.png`); + await writeFile(file, Buffer.from(data, 'base64')); + evidence.captures.push(file); + return file; + }; + const ocr = async (path) => { + const { stdout } = await promisify(execFile)('tesseract', [path, 'stdout']); + return stdout.replace(/\s+/g, ' '); + }; + const assertOcr = async (path, text) => { + const read = await ocr(path); + if (!read.toLowerCase().includes(text.toLowerCase())) { + throw new Error( + `OCR of ${path} lacks ${JSON.stringify(text)}:\n${read.slice(0, 2000)}`, + ); + } + }; + const words = async (path) => { + const { stdout } = await promisify(execFile)('tesseract', [ + path, + 'stdout', + 'tsv', + ]); + return stdout + .split('\n') + .slice(1) + .map((line) => line.split('\t')) + .filter((cells) => cells.length > 11 && cells[11].trim()) + .map((cells) => ({ + text: cells[11].trim(), + x: Number(cells[6]), + y: Number(cells[7]), + w: Number(cells[8]), + h: Number(cells[9]), + })); + }; + // The leading edge of the first word of a body that starts below `label` and + // above `next`, against the label's own word. A centred body lands near the + // middle of a 390-wide phone, so the tolerance is a few glyph bearings. + const assertBodyEdge = async (path, labelText, nextText) => { + const all = await words(path); + const label = all.find((word) => word.text === labelText); + if (!label) throw new Error(`OCR found no ${labelText} label`); + const next = nextText + ? all.find((word) => word.text === nextText && word.y > label.y) + : null; + const body = all.filter( + (word) => + word.y > label.y + label.h && + (!next || word.y < next.y - 4) && + word.w < 700, + ); + if (body.length === 0) { + throw new Error(`OCR found no ${labelText} body words`); + } + const left = Math.min(...body.map((word) => word.x)); + evidence.checks.push({ + check: `${labelText} body starts at its label edge`, + labelX: label.x, + bodyX: left, + delta: left - label.x, + }); + if (Math.abs(left - label.x) > 12) { + throw new Error( + `${labelText} body starts at ${left} but its label at ${label.x}`, + ); + } + }; + + await send('Page.navigate', { url: `${origin}/` }, session); + await delay(3500); + const contentType = await evaluate( + "fetch('/').then(r => r.headers.get('content-type'))", + ); + if (!String(contentType).toLowerCase().startsWith('text/html')) { + throw new Error(`Root index is not text/html: ${contentType}`); + } + evidence.checks.push({ + check: 'fixture index is served as text/html', + value: contentType, + }); + await evaluate("document.querySelector('flt-semantics-placeholder')?.click()"); + await delay(1200); + await waitFor(async () => Boolean(await findNode('Assistant message')), 'semantics'); + + for (const name of [ + 'User message', + 'Assistant message', + 'Reasoning', + 'shell', + 'Unrecognised part: future_part', + 'Ada phone', + ]) { + await requireNode(name); + } + const userGroup = await requireNode('User message'); + const agentGroup = await requireNode('Assistant message'); + if (userGroup.text.trim() === 'User' || agentGroup.text.trim() === 'Assistant') { + throw new Error('Row semantics repeat the visible role label'); + } + evidence.checks.push({ + check: 'roles, client, and collapsed disclosures are present once', + userText: userGroup.text, + agentText: agentGroup.text, + }); + + const top = await capture(`${style}-top`); + for (const text of [ + 'Please keep every word of this prompt', + 'plan.md', + // `report.csv` covers both hosts; Cupertino's small paperclip glyph makes + // OCR drop the leading `r`, so the suffix is the stable part. + 'eport.csv', + 'checked the transcript and kept the original order.', + ]) { + await assertOcr(top, text); + } + const topText = await ocr(top); + for (const hidden of ['The user asked for exact', 'command', 'payload']) { + if (topText.toLowerCase().includes(hidden.toLowerCase())) { + throw new Error(`Closed disclosure leaked ${JSON.stringify(hidden)}`); + } + } + const reasoning = await requireNode('Reasoning'); + evidence.checks.push({ + check: 'closed disclosures expose their collapsed state', + host: style, + ariaExpanded: reasoning.expanded, + }); + + await click('Reasoning'); + const openReasoning = await capture(`${style}-reasoning-expanded`); + await assertOcr(openReasoning, 'The user asked for exact text'); + const openReasoningNode = await requireNode('Reasoning'); + if (style === 'cupertino' && openReasoningNode.expanded !== 'true') { + throw new Error( + `Cupertino reasoning did not announce aria-expanded=true: ${JSON.stringify(openReasoningNode)}`, + ); + } + await assertBodyEdge(openReasoning, 'Reasoning', 'shell'); + + await click('shell'); + const openTool = await capture(`${style}-tool-expanded`); + for (const text of ['command', '-la', 'completed']) { + await assertOcr(openTool, text); + } + await assertBodyEdge(openTool, 'shell', 'Second'); + + await click('Unrecognised part: future_part'); + const openUnknown = await capture(`${style}-unknown-expanded`); + for (const text of ['future_part', 'payload', 'kept', 'count']) { + await assertOcr(openUnknown, text); + } + await assertBodyEdge(openUnknown, 'Unrecognised', null); + + const report = JSON.stringify(evidence, null, 2); + await writeFile(join(out, `${style}-evidence.json`), report); + console.log(report); +} finally { + ws?.close(); + chrome.kill(); + server.close(); + for (let attempt = 0; attempt < 10; attempt++) { + try { + await rm(profile, { recursive: true, force: true }); + break; + } catch { + await delay(200); + } + } +}