feat(app): render markdown in chat messages and artifact previews

This commit is contained in:
2026-10-22 00:26:36 +01:00
parent 0e323dde10
commit dc6b963170
8 changed files with 926 additions and 12 deletions
@@ -4,6 +4,7 @@ import 'package:flutter/services.dart';
import '../../../core/api_gateway.dart';
import '../../../ui/adaptive/host.dart';
import '../../chat/ui/transcript/chat_markdown_view.dart';
import '../domain/artifact_groups.dart';
import '../domain/artifact_kind.dart';
import '../domain/artifact_record.dart';
@@ -27,7 +28,8 @@ const String artifactGalleryTitle = 'Artifacts';
/// never fetched); any other selection opens a native dialog with the recorded
/// name, MIME and size, the visit's capture facts and, only when the recorded
/// url resolves through [resolveArtifactUrl], native `Open` and `Copy link`
/// actions. It never downloads, shares or filters.
/// actions. Markdown previews use the same chat markdown renderer by default.
/// It never downloads, shares or filters.
class ArtifactGalleryPage extends StatefulWidget {
const ArtifactGalleryPage({
super.key,
@@ -36,7 +38,7 @@ class ArtifactGalleryPage extends StatefulWidget {
this.configuredEndpoint = '',
this.openExternal = openArtifactExternally,
this.fetch = fetchArtifact,
this.markdownBuilder = defaultArtifactMarkdown,
this.markdownBuilder = chatMarkdownText,
});
final List<JsonMap> steps;
@@ -4,12 +4,14 @@ import 'package:flutter/material.dart';
import '../domain/artifact_preview.dart';
/// Renders fetched markdown text for the viewer. The gallery's default builder
/// returns plain selectable text, so the viewer is honest before the markdown
/// lane supplies a real renderer.
typedef ArtifactMarkdownBuilder = Widget Function(String text);
/// Renders fetched markdown text for the viewer. It takes the build context so
/// a themed markdown renderer can be supplied; the viewer's own default stays
/// plain selectable text for a standalone preview.
typedef ArtifactMarkdownBuilder =
Widget Function(BuildContext context, String text);
Widget defaultArtifactMarkdown(String text) => SelectableText(text);
Widget defaultArtifactMarkdown(BuildContext context, String text) =>
SelectableText(text);
/// The viewer's preview surface for one page.
///
@@ -62,7 +64,7 @@ class ArtifactPreviewBody extends StatelessWidget {
case ArtifactPreviewKind.text:
return _ScrollingText(child: SelectableText(text ?? ''));
case ArtifactPreviewKind.markdown:
return _ScrollingText(child: markdownBuilder(text ?? ''));
return _ScrollingText(child: markdownBuilder(context, text ?? ''));
case ArtifactPreviewKind.unsupported:
case ArtifactPreviewKind.unavailable:
return const SizedBox.shrink();
@@ -6,6 +6,7 @@ import 'package:flutter/material.dart';
import '../../../core/api_gateway.dart';
import '../domain/chat_transcript.dart';
import '../ui/sessions/chat_adaptive.dart';
import '../ui/transcript/chat_markdown_view.dart';
import '../ui/transcript/chat_message_view.dart';
import 'chat_composer_bar.dart';
import 'chat_composer_controller.dart';
@@ -16,6 +17,10 @@ import 'chat_thread_controller.dart';
/// One read-only conversation: the controller's transcript in the order the
/// server reports it, rendered by the existing [ChatMessageView].
///
/// Assistant text goes through [ChatMarkdownView]'s builder, so stored and live
/// prose share the supported markdown subset. User text keeps the server's exact
/// characters, and reasoning/tool disclosures stay plain text.
///
/// History and live-subscription failures are separate notices: readable stored
/// history must never hide a refused live watch. The shell owns the navigation
/// bar, so this view adds only its own Back control and content.
@@ -237,7 +242,10 @@ class _ChatThreadViewState extends State<ChatThreadView> {
Padding(
key: ValueKey<String>(keys[index]),
padding: const EdgeInsets.symmetric(horizontal: 16),
child: ChatMessageView(row: rows[index]),
child: ChatMessageView(
row: rows[index],
textBuilder: chatMarkdownText,
),
),
if (_controller.running)
Padding(
+140
View File
@@ -0,0 +1,140 @@
# C2-markdown-wire — assistant markdown in the thread and artifact viewer
Wires the landed chat markdown renderer into the two call sites it was built for.
No parser, inline/block model, chat domain, application route/shell, run,
pipeline, graph or schedule file changed. The candidate is uncommitted: no
commit, push, merge or deployment was performed.
## Behavior
- `ChatThreadView` passes `chatMarkdownText` as `ChatMessageView.textBuilder`.
Stored history and live `chat_event` text frames therefore render the supported
subset: headings, paragraphs, bullet/ordered lists, tables, fenced code with
its language label, inline code, bold/italic, safe/unsafe links and literal
HTML. Assistant prose is parsed into native blocks; the raw markers do not
reach the screen.
- User rows are untouched: they remain the server's exact plain text, including
any `**`, `[label](target)`, `<tag>` or other markdown-looking characters. The
transcript test asserts the exact string and the absence of the parsed form.
- Reasoning and tool disclosures are untouched and stay plain text.
- `ArtifactGalleryPage` now defaults its `markdownBuilder` to the same
`chatMarkdownText`. The public `ArtifactMarkdownBuilder` seam gained the build
context (`Widget Function(BuildContext, String)`) so the themed renderer can be
injected without a `Builder` shim; `ArtifactPreviewBody` passes its own context
and `ArtifactViewerPage`'s standalone default remains plain selectable text.
- `.md`/`.markdown` names and `text/markdown` records already classify as the
viewer's markdown kind; both now render through the chat renderer while the
recorded name/MIME/size, visit facts, paging and external Open/Copy actions
stay unchanged.
- Unsafe links (`javascript:`, etc.) render as their label with no recognizer or
underline. No host link opener was added, so a safe link is also inert: the
renderer only underlines links when a caller supplies `onOpenLink`, and this
slice did not add an external-open capability to chat.
## Files
| Path | Change | Role |
| --- | ---: | --- |
| `lib/features/chat/conversation/chat_thread_view.dart` | +9/−1 | Pass the markdown builder into each `ChatMessageView` |
| `lib/features/artifacts/ui/artifact_gallery_page.dart` | +4/−2 | Default markdown builder is `chatMarkdownText`; docs |
| `lib/features/artifacts/ui/artifact_viewer_content.dart` | +8/−6 | Context-aware builder seam; pass context at the markdown kind |
| `test/features/chat/conversation/chat_thread_view_test.dart` | +174 | History/live/exact-user/labels/Cupertino 320px tests |
| `test/features/artifacts/ui/artifact_viewer_page_test.dart` | +115/−3 | Gallery→viewer markdown wiring, extension-only MIME, 320px Cupertino |
| `tool/chat_markdown_wire_probe.dart` | new, 123 | Material/Cupertino real-app fixture (ChatScreen) |
| `tool/chat_markdown_wire_probe.mjs` | new, 353 | Chrome CDP capture, OCR and semantics assertions |
| `notes/C2-markdown-wire.md` | new | This note |
## Regression coverage
Chat thread (`test/features/chat/conversation/chat_thread_view_test.dart`):
- Assistant history with `##` heading, bullet list, a `dart` fence and an
inline-code/HTML/unsafe-link paragraph renders as blocks on Material and
Cupertino; `dart` and the verbatim code text are present; raw `##`, fences and
the link target are absent; the HTML stays literal; only the user row keeps
`javascript:alert`.
- The user row keeps `Keep **these** characters [as-is](javascript:alert)
<b>literal</b>.` exactly; the parsed equivalent is absent.
- A live text frame (`### Live answer`, ordered list, inline code) renders
formatted markdown and keeps the running line.
- No `Material` widget is introduced under the Cupertino host.
- A formatted answer with heading, table and code renders at 320 logical px,
DPR 2, 2x text, dark Cupertino with no exception and native controls only.
Artifact viewer (`test/features/artifacts/ui/artifact_viewer_page_test.dart`):
- Tapping a `text/markdown` row in the real `ArtifactGalleryPage` opens
`ArtifactViewerPage`, which draws `ChatMarkdownView` with the heading, table,
`dart` label and code; raw `# Report` is absent; `1 of 1 · text/markdown`,
`text/markdown · 4.0 KiB` and `review · visit 1` remain visible.
- A `.markdown` name without a recorded MIME uses the same renderer and reports
`type not recorded`.
- The same path at 320 logical px, DPR 2, 2x text in dark Cupertino raises no
exception and contains no `Material` widget.
- Existing injected-builder/text-default/paging/external/unsupported tests were
updated only for the new builder signature.
## Checks (actual)
- `flutter test` — **953 tests passed**.
- `flutter test test/features/chat test/features/artifacts test/application` —
**279 tests passed**.
- `flutter analyze` — **No issues found**.
- `dart format --output=none --set-exit-if-changed` on the six changed/new Dart
files — exit 0 (0 changed at the final check).
- `node --check tool/chat_markdown_wire_probe.mjs` — exit 0.
- `flutter build web` (`lib/main.dart`) — built successfully.
- `flutter build web -t tool/chat_markdown_wire_probe.dart` with
`CHAT_MARKDOWN_WIRE_PROBE_STYLE=material` and `=cupertino` — both built.
- Browser probes, real headless Chrome, 390x844 at DPR 2, dark appearance,
isolated loopback OS-assigned ports:
- Material and Cupertino both opened the real `/chat` list, selected
`Markdown wire`, found `User message`, `Assistant message` and `Reasoning`,
captured the thread, OCR-confirmed `Quarterly status`, `release`,
`review pending`, `dart`, `count = 3`, `unsafe stay visible` and the exact
user string `Keep **this** exactly`, and confirmed the raw markers
`## Quarterly`, `` ```dart ``, `| --- |`, `**release**` and
`unsafe](javascript` were absent.
- The semantics tree exposed the formatted heading at level 2 in both hosts.
- Both runs exited 0 and wrote evidence JSON.
## Browser evidence
`/Users/alan/.local/share/pipelines/data/artifacts/91/public/c2-markdown-wire/`
- `material-markdown-thread.png`, `material-evidence.json`
- `cupertino-markdown-thread.png`, `cupertino-evidence.json`
SHA-256:
```
e9813cee1a69487a42025df4079dbafb05322070363f0d22eaccbc67fe45f390 material-markdown-thread.png
3726583850d74693fadb1aa8ddd8d2f2e6f8d49a28c8f661f49012e9c46fe5ec cupertino-markdown-thread.png
```
Both captures show the exact user characters, collapsed `Reasoning`, the
formatted `Quarterly status` heading, bold `release`, inline-code fill on
`gateway`, both list items, the table with a distinct header, the `dart` code
label with verbatim code, literal `<b>markup</b>`, and the inert `unsafe` label
inside the real Chat destination. The Cupertino capture is the dark native
layout with its own bar, buttons and grouped list styling.
## Not done / unverified
- No browser capture of the artifact viewer markdown path was taken; that wiring
is covered by the widget tests above. The only requested per-host browser
capture (a formatted assistant answer) is present.
- Safe links are not activatable in chat or the viewer; the renderer's
`onOpenLink` is unset because no host link-opening capability exists yet.
- Mobile/Android/iOS/macOS builds and device behavior were not run or verified;
this is web-only evidence.
- `PARITY.md` was not edited; its Chat message surfaces and Artifact preview rows
still need the manager's integrated update.
## Manager completion after a server persistence failure
The run ended with the server's duplicate step-evidence bug (since fixed and
deployed). The retained call-site changes are complete: 234 chat/artifacts tests
pass. Manager merged them directly. The artifact viewer now takes a context-aware
markdown builder so it can render themed markdown, and the gallery passes the same
renderer the chat uses, keeping the standalone default honest.
@@ -13,6 +13,7 @@ import 'package:pipelines_app/features/artifacts/ui/artifact_gallery_page.dart';
import 'package:pipelines_app/features/artifacts/ui/artifact_viewer_fetch.dart';
import 'package:pipelines_app/features/artifacts/ui/artifact_viewer_page.dart';
import 'package:pipelines_app/features/artifacts/ui/artifact_visit_list.dart';
import 'package:pipelines_app/features/chat/ui/transcript/chat_markdown_view.dart';
import '../domain/artifact_fixtures.dart';
import 'artifact_visit_list_fixture.dart';
@@ -37,7 +38,7 @@ Future<void> _pumpViewer(
required String selected,
bool cupertino = false,
ArtifactFetch? fetch,
Widget Function(String)? markdownBuilder,
Widget Function(BuildContext, String)? markdownBuilder,
ArtifactExternalOpen? openExternal,
}) async {
final page = ArtifactViewerPage(
@@ -45,7 +46,8 @@ Future<void> _pumpViewer(
selectedIdentity: selected,
configuredEndpoint: _endpoint,
fetch: fetch ?? (_) async => ArtifactFetchResult(status: 200, bytes: _png),
markdownBuilder: markdownBuilder ?? (value) => SelectableText(value),
markdownBuilder:
markdownBuilder ?? (context, value) => SelectableText(value),
openExternal: openExternal ?? (_) async => true,
);
await tester.pumpWidget(
@@ -64,6 +66,54 @@ TextButton _textButton(WidgetTester tester, String label) {
);
}
const String _markdownBody =
'# Report\n'
'\n'
'A paragraph with **bold** and `code`.\n'
'\n'
'| Step | State |\n'
'| --- | --- |\n'
'| build | ok |\n'
'\n'
'```dart\n'
'final n = 1;\n'
'```';
/// Opens the gallery's markdown row so the default builder composes the real
/// viewer exactly as the run detail does.
Future<void> _openMarkdownFromGallery(
WidgetTester tester, {
required bool cupertino,
String name = 'notes.md',
String contentType = 'text/markdown',
}) async {
final page = ArtifactGalleryPage(
steps: <JsonMap>[
step(
stage: 'review',
visit: 1,
artifacts: [
artifact(
name,
url: '/artifacts/1/1/$name',
contentType: contentType,
sizeBytes: 4096,
),
],
),
],
configuredEndpoint: _endpoint,
fetch: (_) async =>
ArtifactFetchResult(status: 200, bytes: utf8.encode(_markdownBody)),
);
await tester.pumpWidget(
cupertino ? CupertinoApp(home: page) : MaterialApp(home: page),
);
await tester.pumpAndSettle();
await tester.tap(find.text(name));
await tester.pumpAndSettle();
}
void main() {
testWidgets('an image is previewed in-app without opening a tab', (
tester,
@@ -153,7 +203,7 @@ void main() {
selected: identity,
fetch: (_) async =>
ArtifactFetchResult(status: 200, bytes: utf8.encode('# Title')),
markdownBuilder: (value) => Text('MARKDOWN $value'),
markdownBuilder: (context, value) => Text('MARKDOWN $value'),
);
expect(find.text('MARKDOWN # Title'), findsOneWidget);
@@ -403,4 +453,66 @@ void main() {
expect(find.text('cupertino text'), findsOneWidget);
expect(find.byType(CupertinoButton), findsWidgets);
});
testWidgets('the gallery previews markdown through the chat renderer', (
tester,
) async {
await _openMarkdownFromGallery(tester, cupertino: false);
expect(find.byType(ArtifactViewerPage), findsOneWidget);
expect(find.byType(ChatMarkdownView), findsOneWidget);
// The subset renders as native blocks, not raw markers.
expect(find.text('Report'), findsOneWidget);
expect(find.text('build'), findsOneWidget);
expect(find.text('ok'), findsOneWidget);
expect(find.text('dart'), findsOneWidget);
expect(find.text('final n = 1;'), findsOneWidget);
expect(find.text('# Report'), findsNothing);
expect(find.text('A paragraph with bold and code.'), findsOneWidget);
// The recorded facts stay visible above the preview.
expect(find.text('1 of 1 · text/markdown'), findsOneWidget);
expect(find.text('text/markdown · 4.0\u00a0KiB'), findsOneWidget);
expect(find.text('review · visit 1'), findsOneWidget);
});
testWidgets('a .markdown name previews without a recorded MIME', (
tester,
) async {
await _openMarkdownFromGallery(
tester,
cupertino: false,
name: 'notes.markdown',
contentType: '',
);
expect(find.byType(ArtifactViewerPage), findsOneWidget);
expect(find.byType(ChatMarkdownView), findsOneWidget);
expect(find.text('Report'), findsOneWidget);
expect(find.text('dart'), findsOneWidget);
expect(find.text('1 of 1 · artifact'), findsOneWidget);
expect(find.text('type not recorded · 4.0\u00a0KiB'), findsOneWidget);
});
testWidgets('Cupertino markdown preview is native at 320px and 2x text', (
tester,
) async {
tester.view.physicalSize = const Size(640, 1200);
tester.view.devicePixelRatio = 2;
tester.platformDispatcher.textScaleFactorTestValue = 2;
tester.platformDispatcher.platformBrightnessTestValue = Brightness.dark;
addTearDown(tester.view.resetPhysicalSize);
addTearDown(tester.view.resetDevicePixelRatio);
addTearDown(tester.platformDispatcher.clearTextScaleFactorTestValue);
addTearDown(tester.platformDispatcher.clearPlatformBrightnessTestValue);
await _openMarkdownFromGallery(tester, cupertino: true);
expect(tester.takeException(), isNull);
expect(find.byType(ChatMarkdownView), findsOneWidget);
expect(find.text('Report'), findsOneWidget);
expect(find.text('dart'), findsOneWidget);
expect(find.text('1 of 1 · text/markdown'), findsOneWidget);
// The viewer and its markdown add no Material control to a Cupertino host.
expect(find.byType(Material), findsNothing);
});
}
@@ -5,6 +5,7 @@ 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_thread_view.dart';
import 'package:pipelines_app/features/chat/ui/transcript/chat_markdown_view.dart';
import 'chat_thread_fixtures.dart';
@@ -306,6 +307,179 @@ void main() {
);
}
}
// Assistant prose goes through the landed markdown renderer; the user's
// message keeps the server's characters byte-for-byte.
for (final cupertino in [false, true]) {
testWidgets('assistant history renders markdown while user text stays exact '
'(${cupertino ? 'cupertino' : 'material'})', (tester) async {
final gateway = FakeChatGateway();
addTearDown(gateway.close);
const userText =
'Keep **these** characters [as-is](javascript:alert) <b>literal</b>.';
const answer =
'## Stored answer\n'
'\n'
'- first item\n'
'- second item\n'
'\n'
'```dart\n'
'final answer = 42;\n'
'```\n'
'\n'
'Literal <b>markup</b> and [unsafe](javascript:alert) stay inert.';
gateway.history = (_) async => chatPage([
chatMessage(
'u1',
'user',
parts: [
{'type': 'text', 'text': userText},
],
),
chatMessage(
'a1',
'assistant',
parts: [
{'type': 'text', 'text': answer},
],
),
]);
await tester.pumpWidget(
(cupertino ? cupertinoHost : host)(
ChatThreadView(gateway: gateway, sessionId: 's', onBack: () {}),
),
);
await tester.pumpAndSettle();
expect(tester.takeException(), isNull);
expect(find.byType(ChatMarkdownView), findsOneWidget);
// Stored assistant prose is parsed into native blocks.
expect(find.text('Stored answer'), findsOneWidget);
expect(find.text('first item'), findsOneWidget);
expect(find.text('second item'), findsOneWidget);
// A fenced block keeps its language label and exact code.
expect(find.text('dart'), findsOneWidget);
expect(find.text('final answer = 42;'), findsOneWidget);
// The subset's raw markers never reach the screen.
expect(find.textContaining('## Stored answer'), findsNothing);
expect(find.textContaining('```'), findsNothing);
// HTML stays literal and an unsafe link keeps only its label.
expect(
find.text(
'Literal <b>markup</b> and unsafe stay inert.',
findRichText: true,
),
findsOneWidget,
);
expect(
find.textContaining('javascript:alert'),
findsOneWidget,
reason: 'only the user text keeps the unsafe target verbatim',
);
// The user message is the server's exact plain text, not markdown.
expect(find.text(userText), findsOneWidget);
expect(
find.text('Keep these characters as-is literal.', findRichText: true),
findsNothing,
);
// No Material control is introduced into the Cupertino host.
if (cupertino) {
expect(find.byType(Material), findsNothing);
}
});
}
testWidgets('a live assistant text frame renders formatted markdown', (
tester,
) async {
final gateway = FakeChatGateway();
addTearDown(gateway.close);
gateway.history = (_) async => chatPage(const []);
await tester.pumpWidget(
host(ChatThreadView(gateway: gateway, sessionId: 's', onBack: () {})),
);
await tester.pumpAndSettle();
expect(find.byKey(const Key('chat-thread-empty')), findsOneWidget);
gateway.emitEvent(
chatTextFrame('### Live answer\n\n1. first\n2. second\n\n`live code`'),
);
await tester.pump();
await tester.pumpAndSettle();
expect(tester.takeException(), isNull);
expect(find.byType(ChatMarkdownView), findsOneWidget);
expect(find.text('Live answer'), findsOneWidget);
expect(find.text('1.'), findsOneWidget);
expect(find.text('2.'), findsOneWidget);
expect(find.text('first'), findsOneWidget);
expect(find.text('second'), findsOneWidget);
expect(find.text('live code'), findsOneWidget);
expect(find.textContaining('### Live answer'), findsNothing);
expect(find.textContaining('`live code`'), findsNothing);
expect(find.text('Assistant is responding…'), findsOneWidget);
});
testWidgets('formatted assistant output fits 320px at 2x in dark Cupertino', (
tester,
) async {
tester.view.physicalSize = const Size(640, 1200);
tester.view.devicePixelRatio = 2;
tester.platformDispatcher.textScaleFactorTestValue = 2;
tester.platformDispatcher.platformBrightnessTestValue = Brightness.dark;
addTearDown(tester.view.resetPhysicalSize);
addTearDown(tester.view.resetDevicePixelRatio);
addTearDown(tester.platformDispatcher.clearTextScaleFactorTestValue);
addTearDown(tester.platformDispatcher.clearPlatformBrightnessTestValue);
final gateway = FakeChatGateway();
addTearDown(gateway.close);
gateway.history = (_) async => chatPage([
chatMessage(
'a1',
'assistant',
parts: [
{
'type': 'text',
'text':
'## A long answer heading that wraps inside a narrow phone\n'
'\n'
'A paragraph long enough to wrap onto several lines without '
'clipping any words at twice the host text size.\n'
'\n'
'| Step | State |\n'
'| --- | --- |\n'
'| build | ok |\n'
'\n'
'```dart\n'
'final value = "a code line that scrolls sideways";\n'
'```',
},
],
),
]);
await tester.pumpWidget(
cupertinoHost(
ChatThreadView(gateway: gateway, sessionId: 's', onBack: () {}),
),
);
await tester.pumpAndSettle();
expect(tester.takeException(), isNull);
expect(
find.text('A long answer heading that wraps inside a narrow phone'),
findsOneWidget,
);
expect(find.text('build'), findsOneWidget);
expect(find.text('dart'), findsOneWidget);
expect(find.byType(Material), findsNothing);
});
}
/// The paged-history fixture shape: a page of consecutive assistant messages,
+123
View File
@@ -0,0 +1,123 @@
import 'dart:async';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:pipelines_app/core/api_gateway.dart';
import 'package:pipelines_app/features/chat/screen.dart';
/// Browser fixture for the C2 markdown wiring: the real Chat destination and
/// the real thread render one server-shaped conversation whose assistant text
/// is formatted markdown and whose user text must stay byte-exact.
///
/// Material and Cupertino hosts are the same fixture built with
/// `CHAT_MARKDOWN_WIRE_PROBE_STYLE`.
class _FixtureGateway implements ApiGateway {
final _frames = StreamController<JsonMap>.broadcast();
@override
Stream<JsonMap> get frames => _frames.stream;
@override
Future<JsonMap> call(String type, [JsonMap fields = const {}]) async {
switch (type) {
case 'chat_sessions':
return {
'chat_sessions': [
{
'id': 'probe-markdown',
'title': 'Markdown wire',
'directory': '/workspace/demo',
},
],
};
case 'chat_subscribe':
return {};
case 'chat_messages':
return {
'chat_history': {
'messages': [
{
'id': 'u-markdown',
'role': 'user',
'parts': [
{
'type': 'text',
'text': 'Keep **this** exactly: [link](javascript:alert).',
},
],
},
{
'id': 'a-markdown',
'role': 'assistant',
'parts': [
{
'type': 'reasoning',
'text': 'Reasoning stays plain **text**.',
},
{'type': 'text', 'text': _answer},
],
},
],
'next_cursor': '',
'has_more': false,
},
};
}
return {};
}
}
const String _answer =
'## Quarterly status\n'
'\n'
'The **release** is ready, with `gateway` passing.\n'
'\n'
'- build verified\n'
'- review pending\n'
'\n'
'| Step | State |\n'
'| --- | --- |\n'
'| build | ok |\n'
'\n'
'```dart\n'
'final count = 3;\n'
'```\n'
'\n'
'Literal <b>markup</b> and [unsafe](javascript:alert) stay visible.';
const _style = String.fromEnvironment(
'CHAT_MARKDOWN_WIRE_PROBE_STYLE',
defaultValue: 'material',
);
void main() {
final screen = ChatScreen(
gateway: _FixtureGateway(),
onSessionSelected: null,
);
if (_style == 'cupertino') {
runApp(
CupertinoApp(
theme: const CupertinoThemeData(brightness: Brightness.dark),
home: CupertinoPageScaffold(
navigationBar: const CupertinoNavigationBar(middle: Text('Chat')),
child: SafeArea(child: screen),
),
),
);
return;
}
runApp(
MaterialApp(
theme: ThemeData(
brightness: Brightness.dark,
platform: TargetPlatform.android,
),
home: Scaffold(
appBar: AppBar(title: const Text('Chat')),
body: screen,
),
),
);
}
+353
View File
@@ -0,0 +1,353 @@
// Real-browser evidence for the C2 markdown wiring fixture.
//
// Build one host, then run from app/:
// flutter build web -t tool/chat_markdown_wire_probe.dart \
// --dart-define=CHAT_MARKDOWN_WIRE_PROBE_STYLE=material \
// --output build/probe-chat-markdown-wire-material
// node tool/chat_markdown_wire_probe.mjs \
// --root build/probe-chat-markdown-wire-material \
// --out DIR --style material
//
// The Cupertino host is the same fixture built with the cupertino define.
// Requires Google Chrome and Tesseract: Flutter web paints text on the canvas,
// so OCR confirms the rendered answer while the semantics tree confirms the
// heading and the row structure.
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-markdown-wire-material',
);
const out = resolve(args.get('out') ?? 'build/chat-markdown-wire-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',
'.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);
res.writeHead(200, {
'content-type': mime[extname(filePath)] ?? 'application/octet-stream',
});
res.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-markdown-wire-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 < 180; 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');
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(),
level: x.getAttribute('aria-level'),
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 hasText = async (fragment) => {
const all = await nodes();
return all.some((node) =>
`${node.label ?? ''} ${node.text ?? ''}`
.toLowerCase()
.includes(fragment.toLowerCase()),
);
};
const click = async (name, { skipButtons = false } = {}) => {
const all = await nodes();
const score = (node) => {
const label = node.label ?? '';
const text = node.text ?? '';
if (label === name || text === name) return 0;
if (label.startsWith(name) || text.startsWith(name)) return 1;
if (label.includes(name) || text.includes(name)) return 2;
return -1;
};
const matches = all
.filter((node) => score(node) >= 0)
.filter((node) => !skipButtons || node.role !== 'button')
.sort((a, b) => score(a) - score(b) || a.w * a.h - b.w * b.h);
const node = matches[0];
if (!node) {
const shown = all
.map((entry) => entry.label ?? entry.text)
.filter(Boolean);
throw new Error(
`Could not find ${JSON.stringify(name)}: ${JSON.stringify(shown)}`,
);
}
const x = node.x + node.w / 2;
const y = node.y + node.h / 2;
for (const type of ['mousePressed', 'mouseReleased']) {
await send(
'Input.dispatchMouseEvent',
{ type, x, y, button: 'left', clickCount: 1 },
session,
);
}
await delay(900);
};
const capture = async (name) => {
const data = (
await send('Page.captureScreenshot', { format: 'png' }, session)
).result.data;
const file = join(out, `${style}-${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, 1600)}`,
);
}
};
await send('Page.navigate', { url: `${origin}/` }, session);
await delay(3500);
await evaluate("document.querySelector('flt-semantics-placeholder')?.click()");
await delay(1200);
await waitFor(
async () => hasText('Markdown wire'),
'session list semantics',
);
// The row's trailing actions are separate buttons whose labels also contain
// the title; open the row itself, never its overflow menu.
await click('Markdown wire', { skipButtons: true });
await waitFor(
async () => Boolean(await findNode('Assistant message')),
'thread semantics',
);
// The user row keeps its literal characters and the folded assistant turn
// keeps the unchanged Reasoning disclosure.
const user = await requireNode('User message');
const assistant = await requireNode('Assistant message');
await requireNode('Reasoning');
evidence.checks.push({
check: 'user and folded assistant rows are present once',
userLabel: user.label,
assistantLabel: assistant.label,
});
const shot = await capture('markdown-thread');
for (const text of [
'Quarterly status',
'release',
'review pending',
'dart',
'count = 3',
'unsafe',
// The user's own message is not parsed.
'Keep **this** exactly',
]) {
await assertOcr(shot, text);
}
const read = await ocr(shot);
for (const raw of [
'## Quarterly',
'```dart',
'| --- |',
'**release**',
'unsafe](javascript',
]) {
if (read.toLowerCase().includes(raw.toLowerCase())) {
throw new Error(`Raw markdown marker rendered: ${JSON.stringify(raw)}`);
}
}
await assertOcr(shot, 'unsafe stay visible');
const headings = await evaluate(`[...document.querySelectorAll(
'flt-semantics[aria-level], h1, h2, h3, h4, h5, h6')]
.map(x => ({
label: x.getAttribute('aria-label'),
text: (x.innerText || '').trim(),
level: x.getAttribute('aria-level') ??
(/^H[1-6]$/.test(x.tagName) ? x.tagName.slice(1) : null),
}))`);
const heading = headings.find((node) =>
`${node.label ?? ''}${node.text ?? ''}`.includes('Quarterly'),
);
if (!heading) {
throw new Error(
`The formatted heading is missing its level: ${JSON.stringify(headings)}`,
);
}
evidence.checks.push({
check: 'assistant answer renders formatted markdown blocks',
host: style,
headingLevel: heading.level,
rawMarkersAbsent: true,
capture: shot,
});
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);
}
}
}