Add show_diff chat tool and diff card

The Pipelines MCP server gains show_diff: it runs Git in a repository
inside the operator's home (default: the chat's directory) and records
the per-file patches. Without base it shows uncommitted work against
HEAD; with base it shows everything since the merge base (a pull
request preview), optionally committed changes only. Bounded at 300
files, 256 KiB per patch and 1 MiB total, with partial diffs labelled;
diff bodies share the per-chat retention budget with HTML views.

The chat draws the item as a card listing each changed file with its
stat; a row opens the existing file diff viewer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-21 17:08:02 +00:00
parent 79e5183469
commit 059eb1bb0d
15 changed files with 1077 additions and 16 deletions
@@ -0,0 +1,147 @@
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/changes/changes.dart';
import 'package:pipelines_app/features/chat/domain/chat_shown_items.dart';
import 'package:pipelines_app/features/chat/ui/transcript/shown/chat_shown_card.dart';
JsonMap _file(String path, String kind, String patch) => {
'path': path,
'kind': kind,
'patch': patch,
};
JsonMap _item({
List<JsonMap>? files,
String status = 'complete',
int omitted = 0,
bool expired = false,
String base = 'main',
}) {
final list =
files ??
[
_file(
'app/lib/a.dart',
'modified',
'--- a/app/lib/a.dart\n+++ b/app/lib/a.dart\n@@ -1,2 +1,2 @@\n one\n-two\n+TWO\n',
),
_file('docs/new.md', 'added', '@@ -0,0 +1,2 @@\n+# New\n+text\n'),
];
return {
'id': 'd1',
'kind': 'diff',
'created_at': 1,
'title': 'PR preview',
'expired': expired,
'diff': {
'repo': '/home/op/repos/pipelines',
'base': base,
'base_commit': 'abc123def456',
'head': 'feature',
'files': list.length + omitted,
'additions': 3,
'deletions': 1,
if (!expired)
'changes': {
'status': status,
'files': list,
'omitted': omitted,
'skipped': 0,
if (status != 'complete')
'diagnostic': 'some patches were left out by the size limit.',
},
},
};
}
Future<void> _pump(WidgetTester tester, JsonMap item) async {
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: SingleChildScrollView(
child: ChatShownCard(
row: {'kind': chatShownRowKind, 'item': item},
endpoint: 'ws://127.0.0.1:1/connect',
),
),
),
),
);
await tester.pumpAndSettle();
}
void main() {
test('diff items are a recorded kind and show_diff calls are hidden', () {
expect(chatShownKinds, contains('diff'));
expect(
chatPipelinesToolRepresented({
'type': 'tool',
'name': 'mcp__pipelines__show_diff',
'state': {'status': 'completed'},
}),
isTrue,
);
});
testWidgets('a diff card lists its files and opens the diff viewer', (
tester,
) async {
await _pump(tester, _item());
expect(find.text('PR preview'), findsOneWidget);
expect(
find.text('pipelines · feature vs main · with uncommitted work'),
findsOneWidget,
);
expect(find.text('2 files changed +3 -1'), findsOneWidget);
expect(find.text('app/lib/a.dart'), findsOneWidget);
expect(find.text('docs/new.md'), findsOneWidget);
expect(find.byKey(const Key('chat-shown-diff-partial')), findsNothing);
await tester.tap(find.text('app/lib/a.dart'));
await tester.pumpAndSettle();
expect(find.byType(FileDiffPage), findsOneWidget);
expect(find.textContaining('TWO', findRichText: true), findsWidgets);
});
testWidgets('a long diff lists the first files until Show all', (
tester,
) async {
final files = [
for (var i = 0; i < 12; i++)
_file('f$i.txt', 'added', '@@ -0,0 +1 @@\n+x\n'),
];
await _pump(tester, _item(files: files));
expect(find.text('f7.txt'), findsOneWidget);
expect(find.text('f8.txt'), findsNothing);
await tester.ensureVisible(find.byKey(const Key('chat-shown-diff-all')));
await tester.tap(find.byKey(const Key('chat-shown-diff-all')));
await tester.pumpAndSettle();
expect(find.text('f11.txt'), findsOneWidget);
expect(find.byKey(const Key('chat-shown-diff-all')), findsNothing);
});
testWidgets('a partial diff says what was left out', (tester) async {
await _pump(tester, _item(status: 'partial', omitted: 3));
expect(find.byKey(const Key('chat-shown-diff-partial')), findsOneWidget);
expect(find.text('3 more files are not listed.'), findsOneWidget);
expect(find.text('5 files changed +3 -1'), findsOneWidget);
});
testWidgets('an expired diff keeps its summary and says so', (tester) async {
await _pump(tester, _item(expired: true));
expect(find.text('2 files changed +3 -1'), findsOneWidget);
expect(find.text('This diff is no longer retained'), findsOneWidget);
expect(find.byType(ChangeFileRow), findsNothing);
});
testWidgets('without a base it describes the uncommitted changes', (
tester,
) async {
await _pump(tester, _item(base: ''));
expect(
find.text('pipelines · feature · uncommitted changes'),
findsOneWidget,
);
});
}