From bff7115e4e4b5f6d91a28019c262448b30f4abd1 Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Tue, 3 Nov 2026 13:01:53 +0000 Subject: [PATCH] Search: find operator chats by title, directory and message text A search with kind chat matches the operator-session registry's titles and directories and the newest messages of recent transcripts through the chat broker (bounded count, concurrency and one-second budget), answered off the connection's read loop. The all-kind search is unchanged for older clients. The app sends the chat search after its run search and shows a Chats group with title, harness, directory and snippet; a chat hit opens that chat in the Chat tab. The probe fixture answers chat searches over ?chats=N. --- app/lib/application/pipelines_app.dart | 4 + .../search/domain/search_chat_row.dart | 47 ++++ .../search/domain/search_chat_slice.dart | 83 +++++++ .../search/domain/search_controller.dart | 80 +++++- .../features/search/domain/search_models.dart | 1 + .../features/search/domain/search_row.dart | 11 + .../features/search/domain/search_target.dart | 39 ++- .../search/ui/cupertino_search_body.dart | 23 +- .../search/ui/material_search_body.dart | 23 +- .../search/ui/search_bar_controls.dart | 4 +- .../features/search/ui/search_result_row.dart | 74 ++++-- .../search/ui/search_result_slivers.dart | 71 ++++++ .../application/search_chat_open_test.dart | 131 ++++++++++ app/test/application/search_wiring_test.dart | 77 +++++- .../search/search_chat_controller_test.dart | 160 ++++++++++++ .../features/search/search_chat_row_test.dart | 73 ++++++ .../search/search_chat_screen_test.dart | 124 ++++++++++ .../features/search/search_query_test.dart | 2 + .../search/search_screen_states_test.dart | 7 +- .../features/search/search_screen_test.dart | 7 +- .../features/search/search_test_support.dart | 31 +++ app/tool/perf/chat_search_fixture.dart | 79 ++++++ app/tool/perf/perf_fixture.dart | 7 + app/tool/search_selection_probe_entry.dart | 19 ++ docs/cli.md | 14 ++ internal/pipeline/search.go | 38 +-- internal/protocol/search.go | 72 +++++- internal/protocol/search_chat_test.go | 67 +++++ internal/server/search_chats.go | 228 ++++++++++++++++++ internal/server/search_chats_test.go | 166 +++++++++++++ internal/server/websocket.go | 15 ++ 31 files changed, 1659 insertions(+), 118 deletions(-) create mode 100644 app/lib/features/search/domain/search_chat_row.dart create mode 100644 app/lib/features/search/domain/search_chat_slice.dart create mode 100644 app/lib/features/search/ui/search_result_slivers.dart create mode 100644 app/test/application/search_chat_open_test.dart create mode 100644 app/test/features/search/search_chat_controller_test.dart create mode 100644 app/test/features/search/search_chat_row_test.dart create mode 100644 app/test/features/search/search_chat_screen_test.dart create mode 100644 app/tool/perf/chat_search_fixture.dart create mode 100644 internal/protocol/search_chat_test.go create mode 100644 internal/server/search_chats.go create mode 100644 internal/server/search_chats_test.go diff --git a/app/lib/application/pipelines_app.dart b/app/lib/application/pipelines_app.dart index bd963ec3..2b3701bb 100644 --- a/app/lib/application/pipelines_app.dart +++ b/app/lib/application/pipelines_app.dart @@ -387,6 +387,10 @@ class _PipelinesAppState extends State ); route = AppRoute.run(runId); } + case SearchChatTarget(:final sessionId): + // The Chat tab selects the routed session in its list and shows its + // thread, exactly as a chat notification opens it. + route = AppRoute.chatSession(sessionId); } if (route == null) return; setState(() => _pendingRunSearch = selection); diff --git a/app/lib/features/search/domain/search_chat_row.dart b/app/lib/features/search/domain/search_chat_row.dart new file mode 100644 index 00000000..9365371e --- /dev/null +++ b/app/lib/features/search/domain/search_chat_row.dart @@ -0,0 +1,47 @@ +import '../../../core/api_gateway.dart'; +import 'search_row.dart'; + +/// The chat payload of a `chat` search hit, or null for any other hit. +JsonMap? searchChatOf(JsonMap hit) { + final chat = hit['chat']; + return hit['kind'] == 'chat' && chat is Map + ? Map.from(chat) + : null; +} + +/// A chat hit's row: the chat's title, its harness and directory as the +/// muted facts, and the matching message's snippet when message text matched. +/// It opens only with the server's own `chat:` target. +SearchRowView searchChatRowView( + JsonMap chat, { + required String query, + required String? target, + required String? recency, +}) { + final title = '${chat['title'] ?? ''}'.trim(); + final directory = '${chat['directory'] ?? ''}'.trim(); + final snippet = '${chat['snippet'] ?? ''}'; + final facts = [ + searchHarnessLabel('${chat['backend'] ?? ''}'), + if (directory.isNotEmpty) directory, + if (chat['closed'] == true) 'closed', + ].join(' · '); + return SearchRowView( + title: title.isEmpty ? 'Untitled chat' : title, + secondary: facts, + kindLabel: 'Chat', + snippet: snippet.isEmpty ? null : snippet, + snippetSpans: snippetSpans(snippet, query), + recency: recency, + target: target, + ); +} + +/// The harness's display name, matching the server's own names; an unknown +/// harness shows as sent. +String searchHarnessLabel(String backend) => switch (backend) { + 'claude' => 'Claude Code', + 'codex' => 'Codex', + '' || 'opencode' => 'OpenCode', + _ => backend, +}; diff --git a/app/lib/features/search/domain/search_chat_slice.dart b/app/lib/features/search/domain/search_chat_slice.dart new file mode 100644 index 00000000..2a0cc2e5 --- /dev/null +++ b/app/lib/features/search/domain/search_chat_slice.dart @@ -0,0 +1,83 @@ +import '../../../core/api_gateway.dart'; +import '../data/search_repository.dart'; +import 'search_models.dart'; +import 'search_query.dart'; + +/// Wording for a server whose search does not know the `chat` kind (it +/// predates chat search and rejects the kind by name). +const unsupportedChatSearchCopy = + 'This server does not search chats. Update the Pipelines server to ' + 'include them.'; + +final _unknownKind = RegExp(r'unknown search kind', caseSensitive: false); + +/// The Chats section's state: one `kind: chat` request per sent query, run +/// beside (never instead of, nor ahead of) the run/artifact search, so the +/// server's transcript readback cannot delay the other results. +class SearchChatSlice { + List results = const []; + bool loading = false; + bool loaded = false; + bool truncated = false; + String? error; + + void clear() { + results = const []; + loading = false; + loaded = false; + truncated = false; + error = null; + } + + /// Requests chats for [query]. [current] reports whether this request is + /// still the newest edit; a stale reply is dropped without a change. + Future load( + SearchRepository repository, + String query, { + required bool Function() current, + required void Function() changed, + }) async { + loading = true; + error = null; + changed(); + try { + final response = await repository.search( + query: query, + kind: SearchKind.chat, + ); + if (!current()) return; + if (response == null) { + results = const []; + truncated = false; + error = unsupportedChatSearchCopy; + } else { + results = response.results + .where((hit) => hit['kind'] == SearchKind.chat.wire) + .toList(growable: false); + truncated = response.truncated; + } + } catch (failure) { + if (!current()) return; + results = const []; + truncated = false; + final text = searchFailureText(failure); + error = _unknownKind.hasMatch(text) || text == unsupportedSearchCopy + ? unsupportedChatSearchCopy + : text; + } + loading = false; + loaded = true; + changed(); + } + + /// A short honest note under the Chats section: why chats are missing, or + /// that the server's bounds left some transcripts unsearched. + String? get note { + if (error != null) return error; + if (truncated) { + return "Some chats were not searched within the server's time or " + 'result limit.'; + } + return null; + } +} diff --git a/app/lib/features/search/domain/search_controller.dart b/app/lib/features/search/domain/search_controller.dart index b9a3f8f8..8367c15c 100644 --- a/app/lib/features/search/domain/search_controller.dart +++ b/app/lib/features/search/domain/search_controller.dart @@ -4,6 +4,7 @@ import 'package:flutter/foundation.dart'; import '../../../core/api_gateway.dart'; import '../data/search_repository.dart'; +import 'search_chat_slice.dart'; import 'search_models.dart'; import 'search_query.dart'; import 'search_row.dart'; @@ -24,6 +25,7 @@ class OmniSearchController extends ChangeNotifier { final SearchRepository repository; final Duration debounce; final SearchSequencer _sequencer = SearchSequencer(); + final SearchChatSlice _chats = SearchChatSlice(); Timer? _timer; bool _disposed = false; @@ -51,7 +53,35 @@ class OmniSearchController extends ChangeNotifier { int get scannedBytes => _scannedBytes; int get cap => _cap; int get scanBoundBytes => _scanBoundBytes; - String? get error => _error; + + /// The failure the body shows: the run search's, or with the Chats kind + /// selected (no run search is sent) the chat search's. + String? get error => + _error ?? (parsed.kind == SearchKind.chat ? _chats.error : null); + + /// Chat hits for the Chats section: shown for every kind that searches + /// chats (all kinds, or Chats alone). + List get shownChats => + _searchesChats(parsed.kind) ? _chats.results : const []; + + /// The Chats section's note: why chats are missing, or that the server's + /// bounds left some unsearched. With Chats selected alone a failure is the + /// body's error instead, so only the bounds note remains. + String? get chatsNote => switch (parsed.kind) { + SearchKind.all => _chats.note, + SearchKind.chat when _chats.error == null => _chats.note, + _ => null, + }; + + /// Whether the Chats section is drawn as its own group above the others. + bool get showsChatSection => + parsed.kind == SearchKind.all && + (shownChats.isNotEmpty || chatsNote != null); + + static bool _searchesChats(SearchKind kind) => + kind == SearchKind.all || kind == SearchKind.chat; + + bool get _hasShown => shownResults.isNotEmpty || shownChats.isNotEmpty; /// The results already in hand, re-narrowed to the effective kind so the list /// always matches the control while a narrowed request loads. @@ -64,18 +94,22 @@ class OmniSearchController extends ChangeNotifier { } SearchBodyKind get bodyKind { - if (_error != null) return SearchBodyKind.error; - if (shownResults.isNotEmpty) return SearchBodyKind.results; - if (_loading && _results.isEmpty) return SearchBodyKind.searching; + if (error != null) return SearchBodyKind.error; + if (_hasShown || showsChatSection) return SearchBodyKind.results; + if ((_loading && _results.isEmpty) || _chats.loading) { + return SearchBodyKind.searching; + } if (emptyMessage != null) return SearchBodyKind.empty; return SearchBodyKind.idle; } /// A thin progress report above retained results while a newer query loads. - bool get showProgress => _loading && _results.isNotEmpty; + bool get showProgress => + (_loading && _results.isNotEmpty) || + (_chats.loading && _chats.results.isNotEmpty); String? get emptyMessage { - if (_error != null || _loading || shownResults.isNotEmpty) return null; + if (error != null || _loading || _chats.loading || _hasShown) return null; final effective = parsed; if (effective.query.trim().isEmpty) { if (!_loaded || effective.kind == SearchKind.all) return null; @@ -171,14 +205,39 @@ class OmniSearchController extends ChangeNotifier { Future _send(SearchEdit sent) async { final effective = parseSearchQuery(sent.query, _selectedKind); + final query = effective.query.trim(); + final chatsOnly = effective.kind == SearchKind.chat; + // The run/artifact search goes first; the chat search follows as its own + // request, so a slow transcript readback never holds the other results. + final runs = chatsOnly + ? null + : repository.search(query: query, kind: effective.kind); + if (_searchesChats(effective.kind) && (query.isNotEmpty || chatsOnly)) { + unawaited( + _chats.load( + repository, + query, + current: () => _sequencer.accepts(sent.token) && !_disposed, + changed: _notify, + ), + ); + } else { + _chats.clear(); + } + if (runs == null) { + _results = const []; + _truncated = false; + _loading = false; + _loaded = true; + _error = null; + _notify(); + return; + } _loading = true; _error = null; _notify(); try { - final response = await repository.search( - query: effective.query.trim(), - kind: effective.kind, - ); + final response = await runs; if (!_sequencer.accepts(sent.token) || _disposed) return; if (response == null) { _loading = false; @@ -207,6 +266,7 @@ class OmniSearchController extends ChangeNotifier { } void _clearResults() { + _chats.clear(); _results = const []; _truncated = false; _scannedSteps = 0; diff --git a/app/lib/features/search/domain/search_models.dart b/app/lib/features/search/domain/search_models.dart index 671e38d9..8cecc750 100644 --- a/app/lib/features/search/domain/search_models.dart +++ b/app/lib/features/search/domain/search_models.dart @@ -9,6 +9,7 @@ enum SearchKind { step('step', 'Steps'), field('field', 'Fields'), artifact('artifact', 'Artifacts'), + chat('chat', 'Chats'), version('version', 'Versions'); const SearchKind(this.wire, this.label); diff --git a/app/lib/features/search/domain/search_row.dart b/app/lib/features/search/domain/search_row.dart index a6d2ffc9..dd238c47 100644 --- a/app/lib/features/search/domain/search_row.dart +++ b/app/lib/features/search/domain/search_row.dart @@ -1,5 +1,6 @@ import '../../../core/api_gateway.dart'; import '../../../ui/text/relative_time.dart'; +import 'search_chat_row.dart'; import 'search_format.dart'; export 'search_format.dart' show humanBytes, formatSearchTimestamp; @@ -129,6 +130,15 @@ SearchRowView searchRowView(JsonMap hit, String query) { target: target, ); } + final chat = searchChatOf(hit); + if (chat != null) { + return searchChatRowView( + chat, + query: query, + target: target, + recency: recency, + ); + } final kind = _nonBlank(hit['kind']); return SearchRowView( title: _nonBlank(hit['target']) ?? 'Unrecognised result', @@ -191,6 +201,7 @@ String? _serverTarget(JsonMap hit) { 'step', 'field', 'artifact', + 'chat', ].any((key) => _map(hit[key]) != null); return known ? target : null; } diff --git a/app/lib/features/search/domain/search_target.dart b/app/lib/features/search/domain/search_target.dart index bd3b1003..ded46e46 100644 --- a/app/lib/features/search/domain/search_target.dart +++ b/app/lib/features/search/domain/search_target.dart @@ -1,15 +1,16 @@ import '../../../core/api_gateway.dart'; /// The kind of recorded entity a resolved target names. -enum SearchTargetKind { pipeline, version, run, step, field, artifact } +enum SearchTargetKind { pipeline, version, run, step, field, artifact, chat } /// The exact identity behind a recorded search hit. /// /// [wire] is the server's own stable target token /// (`internal/protocol/search.go`): `pipeline:`, `pipeline::v`, -/// `run:`, `run::`, `run:::` or the -/// recorded artifact URL. It is derived from the typed payload, never copied -/// from the hit, and [SearchTarget.fromWire] accepts it back. +/// `run:`, `run::`, `run:::`, the +/// recorded artifact URL or `chat:`. It is derived from the typed +/// payload, never copied from the hit, and [SearchTarget.fromWire] accepts it +/// back. /// /// The token is the identity: two targets that serialise to the same token are /// the same target even when one of them also carries recorded visit facts the @@ -63,6 +64,10 @@ sealed class SearchTarget { // The server's artifact target is its own relative file URL // (`/artifacts///`); an absolute form is accepted only when // it carries that same path. + if (token.startsWith('chat:')) { + final session = token.substring('chat:'.length); + return session.trim().isEmpty ? null : SearchChatTarget(session); + } if (token.startsWith('/artifacts/')) return SearchArtifactTarget(token); if (token.startsWith('http://') || token.startsWith('https://')) { final uri = Uri.tryParse(token); @@ -172,6 +177,21 @@ final class SearchArtifactTarget extends SearchTarget { String get wire => url; } +/// An operator chat session hit. [messageId] is the matching message when the +/// hit matched message text; the token names only the session. +final class SearchChatTarget extends SearchTarget { + const SearchChatTarget(this.sessionId, {this.messageId}); + + final String sessionId; + final String? messageId; + + @override + SearchTargetKind get kind => SearchTargetKind.chat; + + @override + String get wire => 'chat:$sessionId'; +} + /// Either the exact [SearchTarget] a hit names, or the reason it names none. sealed class SearchTargetResolution { const SearchTargetResolution(); @@ -272,6 +292,17 @@ SearchTargetResolution _identityFor(JsonMap hit) { return ResolvedSearchTarget(SearchFieldTarget(runId, sequence, name)); } + final chat = _payload(hit, 'chat'); + if (chat != null) { + final session = _text(chat['session_id']); + if (session == null) { + return const UnresolvedSearchTarget('chat session id is missing'); + } + return ResolvedSearchTarget( + SearchChatTarget(session, messageId: _text(chat['message_id'])), + ); + } + final artifact = _payload(hit, 'artifact'); if (artifact != null) { final url = _text(artifact['url']); diff --git a/app/lib/features/search/ui/cupertino_search_body.dart b/app/lib/features/search/ui/cupertino_search_body.dart index 7ff2d235..7777883d 100644 --- a/app/lib/features/search/ui/cupertino_search_body.dart +++ b/app/lib/features/search/ui/cupertino_search_body.dart @@ -2,7 +2,7 @@ import 'package:flutter/cupertino.dart'; import '../../../core/api_gateway.dart'; import '../domain/search_controller.dart'; -import 'search_result_row.dart'; +import 'search_result_slivers.dart'; import 'search_layout.dart'; /// The Cupertino search view: an optional status header and the shared list @@ -59,18 +59,13 @@ class CupertinoSearchBody extends StatelessWidget { ), ]; case SearchBodyKind.results: - return [ - SliverList.builder( - itemCount: controller.shownResults.length, - itemBuilder: (context, index) => SearchResultRow( - hit: controller.shownResults[index], - query: query, - onOpen: onOpen, - cupertino: true, - divider: index < controller.shownResults.length - 1, - ), - ), - ]; + return searchResultSlivers( + controller: controller, + query: query, + onOpen: onOpen, + cupertino: true, + message: (text) => _CupertinoMessage(message: text), + ); case SearchBodyKind.empty: return [ SliverToBoxAdapter( @@ -82,7 +77,7 @@ class CupertinoSearchBody extends StatelessWidget { SliverFillRemaining( hasScrollBody: false, child: _CupertinoMessage( - message: 'Search pipelines, runs and artifacts.', + message: 'Search pipelines, runs, artifacts and chats.', ), ), ]; diff --git a/app/lib/features/search/ui/material_search_body.dart b/app/lib/features/search/ui/material_search_body.dart index 49f13d0a..17008700 100644 --- a/app/lib/features/search/ui/material_search_body.dart +++ b/app/lib/features/search/ui/material_search_body.dart @@ -2,7 +2,7 @@ import 'package:flutter/material.dart'; import '../../../core/api_gateway.dart'; import '../domain/search_controller.dart'; -import 'search_result_row.dart'; +import 'search_result_slivers.dart'; import 'search_layout.dart'; /// The Material search view: an optional status header and a flat native @@ -50,18 +50,13 @@ class MaterialSearchBody extends StatelessWidget { SliverFillRemaining(hasScrollBody: false, child: _Searching()), ]; case SearchBodyKind.results: - return [ - SliverList.builder( - itemCount: controller.shownResults.length, - itemBuilder: (context, index) => SearchResultRow( - hit: controller.shownResults[index], - query: query, - onOpen: onOpen, - cupertino: false, - divider: index < controller.shownResults.length - 1, - ), - ), - ]; + return searchResultSlivers( + controller: controller, + query: query, + onOpen: onOpen, + cupertino: false, + message: (text) => _SearchMessage(message: text), + ); case SearchBodyKind.empty: return [ SliverToBoxAdapter( @@ -73,7 +68,7 @@ class MaterialSearchBody extends StatelessWidget { SliverFillRemaining( hasScrollBody: false, child: _SearchMessage( - message: 'Search pipelines, runs and artifacts.', + message: 'Search pipelines, runs, artifacts and chats.', ), ), ]; diff --git a/app/lib/features/search/ui/search_bar_controls.dart b/app/lib/features/search/ui/search_bar_controls.dart index aeeb2f89..93ac2038 100644 --- a/app/lib/features/search/ui/search_bar_controls.dart +++ b/app/lib/features/search/ui/search_bar_controls.dart @@ -12,7 +12,7 @@ const searchBarControlsKey = ValueKey('search.bar'); const searchTokenHint = 'Filter with t:pipeline or type:artifact (pipeline, run, step, field, ' - 'artifact, version). Last type wins; choosing Kind replaces it. Quote ' + 'artifact, chat, version). Last type wins; choosing Kind replaces it. Quote ' 'tokens to search literally.'; /// Search's controls as the top bar's main pill (in place of a title): the @@ -57,7 +57,7 @@ class SearchBarControls extends StatelessWidget { autofocus: autofocus, inBar: true, cupertino: cupertino, - placeholder: 'Search pipelines, runs and artifacts', + placeholder: 'Search pipelines, runs, artifacts and chats', onChanged: onChanged, onSubmitted: (_) => onSubmitted(), onCleared: onCleared, diff --git a/app/lib/features/search/ui/search_result_row.dart b/app/lib/features/search/ui/search_result_row.dart index 5da536ca..b1668066 100644 --- a/app/lib/features/search/ui/search_result_row.dart +++ b/app/lib/features/search/ui/search_result_row.dart @@ -11,7 +11,9 @@ import 'search_result_view.dart'; /// artifact's picture), the title with its recency, and one muted line that /// names the kind and then shows the snippet (its first query occurrence /// emphasised by weight, never by parsing server text as markup) or, without -/// one, the kind's own facts. An artifact hit's open choices are its menu. +/// one, the kind's own facts. A chat hit with a matching message shows its +/// harness and directory, then the snippet on a second line. An artifact +/// hit's open choices are its menu. class SearchResultRow extends StatelessWidget { const SearchResultRow({ super.key, @@ -46,33 +48,55 @@ class SearchResultRow extends StatelessWidget { ), title: Text(row.title), time: row.recency, - subtitle: Text.rich(_line(row)), + subtitle: kind == 'chat' && row.snippet != null + ? Column( + crossAxisAlignment: CrossAxisAlignment.start, + mainAxisSize: MainAxisSize.min, + children: [ + Text.rich(_facts(row)), + Text.rich(TextSpan(children: _snippet(row))), + ], + ) + : Text.rich(_line(row)), + compactSubtitle: kind == 'chat' && row.snippet != null + ? Text.rich(TextSpan(children: _snippet(row))) + : null, menu: searchArtifactActions(context, hit), ); } - TextSpan _line(SearchRowView row) { - final snippet = row.snippet; - return TextSpan( - children: [ - TextSpan( - text: row.kindLabel, - style: const TextStyle(fontWeight: FontWeight.w600), - ), - const TextSpan(text: ' · '), - if (snippet == null) - TextSpan(text: row.secondary) - else - for (final span in row.snippetSpans) - TextSpan( - text: span.text, - style: span.matched - ? const TextStyle(fontWeight: FontWeight.bold) - : null, - ), - ], - ); - } + TextSpan _line(SearchRowView row) => row.snippet == null + ? _facts(row) + : TextSpan(children: [_kind(row), ..._snippet(row)]); + + /// The kind, then the kind's own facts (a chat's harness and directory). + TextSpan _facts(SearchRowView row) => TextSpan( + children: [ + _kind(row), + TextSpan(text: row.secondary), + ], + ); + + TextSpan _kind(SearchRowView row) => TextSpan( + children: [ + TextSpan( + text: row.kindLabel, + style: const TextStyle(fontWeight: FontWeight.w600), + ), + const TextSpan(text: ' · '), + ], + ); + + /// The snippet with its first query occurrence emphasised by weight. + List _snippet(SearchRowView row) => [ + for (final span in row.snippetSpans) + TextSpan( + text: span.text, + style: span.matched + ? const TextStyle(fontWeight: FontWeight.bold) + : null, + ), + ]; } IconData searchMaterialIcon(String kind) => switch (kind) { @@ -82,6 +106,7 @@ IconData searchMaterialIcon(String kind) => switch (kind) { 'step' => Icons.arrow_forward, 'field' => Icons.notes, 'artifact' => Icons.attach_file, + 'chat' => Icons.chat_bubble_outline, _ => Icons.help_outline, }; @@ -92,5 +117,6 @@ IconData searchCupertinoIcon(String kind) => switch (kind) { 'step' => CupertinoIcons.arrow_right, 'field' => CupertinoIcons.doc_text, 'artifact' => CupertinoIcons.paperclip, + 'chat' => CupertinoIcons.chat_bubble, _ => CupertinoIcons.question_circle, }; diff --git a/app/lib/features/search/ui/search_result_slivers.dart b/app/lib/features/search/ui/search_result_slivers.dart new file mode 100644 index 00000000..db7ca888 --- /dev/null +++ b/app/lib/features/search/ui/search_result_slivers.dart @@ -0,0 +1,71 @@ +import 'package:flutter/widgets.dart'; + +import '../../../core/api_gateway.dart'; +import '../../../ui/layout/section_heading.dart'; +import '../domain/search_controller.dart'; +import '../domain/search_models.dart'; +import 'search_bounds_note.dart'; +import 'search_result_row.dart'; + +const searchChatsSectionKey = ValueKey('search.section.chats'); +const searchOthersSectionKey = ValueKey('search.section.others'); +const searchChatsNoteKey = ValueKey('search.chats.note'); + +/// The results body shared by both hosts. With every kind searched, chat hits +/// lead as their own "Chats" group above the pipeline/run/artifact hits; with +/// Chats selected alone they are the list. A run search with no hits still +/// says so under the Chats group. +List searchResultSlivers({ + required OmniSearchController controller, + required String query, + required ValueChanged onOpen, + required bool cupertino, + required Widget Function(String message) message, +}) { + final chats = controller.shownChats; + final others = controller.shownResults; + final grouped = controller.showsChatSection; + final note = controller.chatsNote; + Widget rows(List hits) => SliverList.builder( + itemCount: hits.length, + itemBuilder: (context, index) => SearchResultRow( + hit: hits[index], + query: query, + onOpen: onOpen, + cupertino: cupertino, + divider: index < hits.length - 1, + ), + ); + final empty = controller.emptyMessage; + return [ + if (grouped) + SliverToBoxAdapter( + child: SectionHeading( + key: searchChatsSectionKey, + title: chats.isEmpty ? 'Chats' : 'Chats (${chats.length})', + header: true, + ), + ), + if (chats.isNotEmpty) rows(chats), + if (note != null) + SliverToBoxAdapter( + child: Padding( + padding: const EdgeInsets.symmetric(vertical: 8), + child: SearchBoundsNote(key: searchChatsNoteKey, text: note), + ), + ), + if (grouped && others.isNotEmpty) + const SliverToBoxAdapter( + child: SectionHeading( + key: searchOthersSectionKey, + title: 'Pipelines and runs', + header: true, + ), + ), + if (others.isNotEmpty) rows(others), + if (others.isEmpty && + controller.parsed.kind != SearchKind.chat && + empty != null) + SliverToBoxAdapter(child: message(empty)), + ]; +} diff --git a/app/test/application/search_chat_open_test.dart b/app/test/application/search_chat_open_test.dart new file mode 100644 index 00000000..aee27d72 --- /dev/null +++ b/app/test/application/search_chat_open_test.dart @@ -0,0 +1,131 @@ +import 'dart:convert'; + +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:pipelines_app/core/transport/pipelines_client.dart'; +import 'package:pipelines_app/features/chat/screen.dart'; +import 'package:pipelines_app/features/search/search_screen.dart'; + +import '../core/transport/pipelines_client_test.dart' show FakeChannel; +import 'search_wiring_test.dart' + show pumpApp, reply, replyRunList, tapTab, waitForSearch; + +const _session = 'ses_chatsearch1'; + +Map _results(String kind, List> hits) => { + 'type': 'search_results', + 'search_results': { + 'query': 'flaky', + 'kind': kind, + 'results': hits, + 'truncated': false, + 'scanned_steps': 0, + 'scanned_bytes': 0, + 'cap': 50, + 'scan_bound_bytes': 1 << 20, + }, +}; + +/// The whole path from the Search tab: a typed query, the run search and the +/// chat search the app sends after it, then a tap on the chat hit opens that +/// chat in the Chat tab with its session selected. +void main() { + for (final cupertino in [false, true]) { + testWidgets('${cupertino ? 'Cupertino' : 'Material'}: a chat hit opens ' + 'its chat in the Chat tab', (tester) async { + tester.view.physicalSize = const Size(900, 1000); + tester.view.devicePixelRatio = 1; + addTearDown(tester.view.resetPhysicalSize); + addTearDown(tester.view.resetDevicePixelRatio); + final channel = FakeChannel()..readyCompleter.complete(); + final provider = PlatformRouteInformationProvider( + initialRouteInformation: RouteInformation(uri: Uri.parse('/runs')), + ); + addTearDown(provider.dispose); + await pumpApp( + tester, + channel: channel, + client: PipelinesClient(channelFactory: (_) => channel), + provider: provider, + cupertino: cupertino, + ); + await replyRunList(tester, channel); + + await tapTab(tester, 'Search'); + await tester.pump(); + await tester.pump(const Duration(milliseconds: 600)); + await tester.pump(const Duration(milliseconds: 100)); + await tester.enterText( + find.byKey(const ValueKey('search.field')), + 'flaky', + ); + await tester.pump(const Duration(milliseconds: 300)); + + final runs = await waitForSearch(tester, channel); + channel.incoming.add(jsonEncode({'id': runs['id'], ..._results('', [])})); + await tester.pump(); + await tester.pump(); + final chats = await waitForSearch(tester, channel, chats: true); + expect(chats['search'], {'query': 'flaky', 'kind': 'chat', 'limit': 0}); + channel.incoming.add( + jsonEncode({ + 'id': chats['id'], + ..._results('chat', [ + { + 'kind': 'chat', + 'target': 'chat:$_session', + 'score': 1, + 'matched_field': 'message', + 'chat': { + 'session_id': _session, + 'title': 'Fix CI', + 'directory': '/work/api', + 'backend': 'claude', + 'message_id': 'm2', + 'snippet': 'the flaky retry loop', + }, + }, + ]), + }), + ); + await tester.pump(); + await tester.pump(); + expect(find.text('Chats (1)'), findsOneWidget); + expect(find.textContaining('the flaky retry loop'), findsOneWidget); + + await tester.tap( + find.byKey(const ValueKey('search.result.chat:$_session')), + ); + await tester.pump(); + await tester.pump(const Duration(milliseconds: 400)); + await tester.pump(const Duration(milliseconds: 400)); + + expect(provider.value.uri.path, '/chat/$_session'); + expect(find.byType(SearchScreen), findsNothing); + final screen = tester.widget(find.byType(ChatScreen)); + expect(screen.initialSessionId, _session); + + await reply( + tester, + channel, + 'chat_sessions', + body: { + 'type': 'chat_sessions', + 'chat_sessions': [ + { + 'id': _session, + 'title': 'Fix CI', + 'directory': '/work/api', + 'backend': 'claude', + }, + ], + }, + ); + await tester.pump(const Duration(milliseconds: 400)); + expect(find.text('Fix CI'), findsWidgets); + + await tester.pumpWidget(const SizedBox.shrink()); + await tester.pump(); + }); + } +} diff --git a/app/test/application/search_wiring_test.dart b/app/test/application/search_wiring_test.dart index d33140a4..e7afcd39 100644 --- a/app/test/application/search_wiring_test.dart +++ b/app/test/application/search_wiring_test.dart @@ -50,6 +50,63 @@ Future> waitForRequest( fail('Timed out waiting for $type #$index'); } +/// The search requests the app sent: the run/artifact searches, or with +/// [chats] the `kind: chat` searches it sends after each of them. +List> searchRequestsOf( + FakeChannel channel, { + bool chats = false, +}) => requestsOf(channel, 'search') + .where( + (request) => ((request['search'] as Map?)?['kind'] == 'chat') == chats, + ) + .toList(); + +Future> waitForSearch( + WidgetTester tester, + FakeChannel channel, { + int index = 0, + bool chats = false, +}) async { + for (var attempt = 0; attempt < 80; attempt++) { + final requests = searchRequestsOf(channel, chats: chats); + if (index < requests.length) return requests[index]; + await tester.pump(const Duration(milliseconds: 5)); + } + fail('Timed out waiting for ${chats ? 'chat ' : ''}search #$index'); +} + +/// Answers the [index]th chat search (sent after each run search's reply) +/// with no chats, so the app's serialized requests continue. +Future replyChatSearch( + WidgetTester tester, + FakeChannel channel, { + int index = 0, +}) async { + final request = await waitForSearch( + tester, + channel, + index: index, + chats: true, + ); + channel.incoming.add( + jsonEncode({ + 'id': request['id'], + 'type': 'search_results', + 'search_results': { + 'query': (request['search'] as Map)['query'], + 'kind': 'chat', + 'results': [], + 'cap': 50, + 'scanned_steps': 0, + 'scanned_bytes': 0, + 'scan_bound_bytes': 1 << 20, + }, + }), + ); + await tester.pump(); + await tester.pump(); +} + Future reply( WidgetTester tester, FakeChannel channel, @@ -106,7 +163,7 @@ Future searchArtifactHit( await tester.pump(const Duration(milliseconds: 100)); await tester.enterText(find.byKey(const ValueKey('search.field')), 'report'); await tester.pump(const Duration(milliseconds: 300)); - final request = await waitForRequest(tester, channel, 'search', index: index); + final request = await waitForSearch(tester, channel, index: index); channel.incoming.add( jsonEncode({ 'id': request['id'], @@ -125,6 +182,7 @@ Future searchArtifactHit( ); await tester.pump(); await tester.pump(); + await replyChatSearch(tester, channel, index: index); } /// Selects the artifact hit, leaving run 7's `get_run` request unanswered. @@ -401,7 +459,7 @@ void main() { ); await tester.pump(const Duration(milliseconds: 300)); - final request = await waitForRequest(tester, channel, 'search'); + final request = await waitForSearch(tester, channel); expect(request['search'], {'query': 'release', 'kind': '', 'limit': 0}); channel.incoming.add( jsonEncode({ @@ -421,6 +479,7 @@ void main() { ); await tester.pump(); await tester.pump(); + await replyChatSearch(tester, channel); expect(find.text('Run 7 · Preserved run'), findsOneWidget); await tester.tap(find.byKey(const ValueKey('search.result.run:7'))); @@ -437,7 +496,8 @@ void main() { expect(find.text('Run 7'), findsWidgets); // One app socket carried the search and the run reads. expect(sockets, 1); - expect(requestsOf(channel, 'search'), hasLength(1)); + expect(searchRequestsOf(channel), hasLength(1)); + expect(searchRequestsOf(channel, chats: true), hasLength(1)); await tester.pumpWidget(const SizedBox.shrink()); await tester.pump(); @@ -480,12 +540,7 @@ void main() { await tester.pump(const Duration(milliseconds: 100)); await tester.enterText(find.byKey(const ValueKey('search.field')), query); await tester.pump(const Duration(milliseconds: 300)); - final request = await waitForRequest( - tester, - channel, - 'search', - index: index, - ); + final request = await waitForSearch(tester, channel, index: index); channel.incoming.add( jsonEncode({ 'id': request['id'], @@ -504,6 +559,7 @@ void main() { ); await tester.pump(); await tester.pump(); + await replyChatSearch(tester, channel, index: index); } // An artifact hit opens the run's Artifacts tab and that artifact's viewer. @@ -592,7 +648,7 @@ void main() { 'report', ); await tester.pump(const Duration(milliseconds: 300)); - final request = await waitForRequest(tester, channel, 'search'); + final request = await waitForSearch(tester, channel); channel.incoming.add( jsonEncode({ 'id': request['id'], @@ -611,6 +667,7 @@ void main() { ); await tester.pump(); await tester.pump(); + await replyChatSearch(tester, channel); await tester.tap( find.byKey(const ValueKey('search.result./artifacts/7/2/report.txt')), diff --git a/app/test/features/search/search_chat_controller_test.dart b/app/test/features/search/search_chat_controller_test.dart new file mode 100644 index 00000000..f1f8f464 --- /dev/null +++ b/app/test/features/search/search_chat_controller_test.dart @@ -0,0 +1,160 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:pipelines_app/features/search/domain/search_chat_slice.dart'; +import 'package:pipelines_app/features/search/domain/search_controller.dart'; +import 'package:pipelines_app/features/search/domain/search_models.dart'; + +import 'search_test_support.dart'; + +const runHit = { + 'kind': 'run', + 'target': 'run:7', + 'run': {'id': 7}, +}; + +void main() { + test('an all-kind search sends the run search, then chats as their own ' + 'request, and shows runs before chats answer', () async { + final gateway = QueuedGateway(holdChats: true); + final controller = controllerFor(gateway); + addTearDown(controller.dispose); + + controller.updateQuery('flaky'); + await pumpEventQueue(); + expect(gateway.lastSearch, {'query': 'flaky', 'kind': '', 'limit': 0}); + expect(gateway.chatCalls.single['search'], { + 'query': 'flaky', + 'kind': 'chat', + 'limit': 0, + }); + + gateway.succeed(0, envelope([runHit])); + await pumpEventQueue(); + expect(controller.shownResults.single['target'], 'run:7'); + expect(controller.bodyKind, SearchBodyKind.results); + expect(controller.shownChats, isEmpty); + expect(controller.showsChatSection, isFalse); + + gateway.chatPending.single.complete( + envelope([ + chatHit('ses_a', snippet: 'the flaky retry'), + // A non-chat row in a chat reply is not a chat. + runHit, + ]), + ); + await pumpEventQueue(); + expect(controller.shownChats.single['target'], 'chat:ses_a'); + expect(controller.showsChatSection, isTrue); + expect(controller.shownResults.single['target'], 'run:7'); + expect(controller.chatsNote, isNull); + }); + + test('chat hits alone keep the body out of its empty state', () async { + final gateway = QueuedGateway(holdChats: true); + final controller = controllerFor(gateway); + addTearDown(controller.dispose); + + controller.updateQuery('fix'); + await pumpEventQueue(); + gateway.succeed(0, envelope(const [])); + await pumpEventQueue(); + // Runs answered empty while chats still load: still searching. + expect(controller.bodyKind, SearchBodyKind.searching); + expect(controller.emptyMessage, isNull); + + gateway.chatPending.single.complete(envelope([chatHit('ses_a')])); + await pumpEventQueue(); + expect(controller.bodyKind, SearchBodyKind.results); + expect(controller.emptyMessage, isNull); + }); + + test( + 'a narrowed kind sends no chat search; Chats alone sends only chats', + () async { + final gateway = QueuedGateway(); + final controller = controllerFor(gateway); + addTearDown(controller.dispose); + + controller.updateQuery('t:run flaky'); + await pumpEventQueue(); + expect(gateway.calls, hasLength(1)); + expect(gateway.chatCalls, isEmpty); + + controller.selectKind(SearchKind.chat); + await pumpEventQueue(); + expect(gateway.calls, hasLength(1)); + expect(gateway.chatCalls.single['search'], { + 'query': 'flaky', + 'kind': 'chat', + 'limit': 0, + }); + expect(controller.shownResults, isEmpty); + expect(controller.showsChatSection, isFalse); + }, + ); + + test('a stale chat reply never replaces the newest query', () async { + final gateway = QueuedGateway(holdChats: true); + final controller = controllerFor(gateway); + addTearDown(controller.dispose); + + controller.updateQuery('first'); + await pumpEventQueue(); + controller.updateQuery('second'); + await pumpEventQueue(); + expect(gateway.chatCalls, hasLength(2)); + + gateway.chatPending[0].complete(envelope([chatHit('ses_old')])); + await pumpEventQueue(); + expect(controller.shownChats, isEmpty); + + gateway.chatPending[1].complete(envelope([chatHit('ses_new')])); + await pumpEventQueue(); + expect(controller.shownChats.single['target'], 'chat:ses_new'); + }); + + test('a server without chat search says so beside its run results, and ' + 'is the error with Chats selected', () async { + final gateway = QueuedGateway(holdChats: true); + final controller = controllerFor(gateway); + addTearDown(controller.dispose); + + controller.updateQuery('flaky'); + await pumpEventQueue(); + gateway.succeed(0, envelope([runHit])); + gateway.chatPending.single.completeError( + StateError( + 'unknown search kind "chat"; use one of pipeline, version, run, ' + 'step, field, artifact', + ), + ); + await pumpEventQueue(); + expect(controller.error, isNull); + expect(controller.shownResults, hasLength(1)); + expect(controller.chatsNote, unsupportedChatSearchCopy); + expect(controller.showsChatSection, isTrue); + + controller.selectKind(SearchKind.chat); + await pumpEventQueue(); + gateway.chatPending.last.completeError( + StateError('unknown search kind "chat"'), + ); + await pumpEventQueue(); + expect(controller.error, unsupportedChatSearchCopy); + expect(controller.bodyKind, SearchBodyKind.error); + }); + + test('a truncated chat reply carries a bounds note', () async { + final gateway = QueuedGateway(holdChats: true); + final controller = controllerFor(gateway); + addTearDown(controller.dispose); + + controller.updateQuery('flaky'); + await pumpEventQueue(); + gateway.succeed(0, envelope(const [])); + gateway.chatPending.single.complete( + envelope([chatHit('ses_a')], truncated: true), + ); + await pumpEventQueue(); + expect(controller.chatsNote, contains('not searched')); + }); +} diff --git a/app/test/features/search/search_chat_row_test.dart b/app/test/features/search/search_chat_row_test.dart new file mode 100644 index 00000000..46c48be1 --- /dev/null +++ b/app/test/features/search/search_chat_row_test.dart @@ -0,0 +1,73 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:pipelines_app/features/search/domain/search_row.dart'; +import 'package:pipelines_app/features/search/domain/search_target.dart'; + +import 'search_test_support.dart' show chatHit; + +void main() { + test('a message hit shows the title, harness and directory and its ' + 'snippet with the match split out', () { + final row = searchRowView( + chatHit('ses_a', snippet: 'the FLAKY retry'), + 'flaky', + ); + expect(row.title, 'Fix CI'); + expect(row.kindLabel, 'Chat'); + expect(row.secondary, 'Claude Code · /work/api'); + expect(row.snippet, 'the FLAKY retry'); + expect(row.snippetSpans.map((span) => (span.text, span.matched)), [ + ('the ', false), + ('FLAKY', true), + (' retry', false), + ]); + expect(row.target, 'chat:ses_a'); + expect(row.openable, isTrue); + }); + + test('a title hit has no snippet; closed and untitled chats say so', () { + final base = chatHit('ses_b', title: ' '); + final hit = { + ...base, + 'chat': { + ...(base['chat'] as Map), + 'backend': '', + 'closed': true, + }, + }; + final row = searchRowView(hit, 'x'); + expect(row.title, 'Untitled chat'); + expect(row.secondary, 'OpenCode · /work/api · closed'); + expect(row.snippet, isNull); + }); + + test('a chat hit resolves to its session, keeping the matched message', () { + final resolved = searchTargetFor(chatHit('ses_a')); + expect(resolved, isA()); + final target = (resolved as ResolvedSearchTarget).target; + expect(target, isA()); + expect((target as SearchChatTarget).sessionId, 'ses_a'); + expect(target.kind, SearchTargetKind.chat); + expect(target.wire, 'chat:ses_a'); + + final message = chatHit('ses_a', snippet: 'x'); + (message['chat'] as Map)['message_id'] = 'm9'; + final withMessage = + (searchTargetFor(message) as ResolvedSearchTarget).target + as SearchChatTarget; + expect(withMessage.messageId, 'm9'); + expect(withMessage, target); + + expect(SearchTarget.fromWire('chat:ses_a'), target); + expect(SearchTarget.fromWire('chat:'), isNull); + }); + + test('a chat hit without a session id or with a mismatched target is not ' + 'openable', () { + final missing = chatHit('ses_a'); + (missing['chat'] as Map).remove('session_id'); + expect(searchTargetFor(missing), isA()); + + final mismatched = {...chatHit('ses_a'), 'target': 'chat:ses_b'}; + expect(searchTargetFor(mismatched), isA()); + }); +} diff --git a/app/test/features/search/search_chat_screen_test.dart b/app/test/features/search/search_chat_screen_test.dart new file mode 100644 index 00000000..d20adb5e --- /dev/null +++ b/app/test/features/search/search_chat_screen_test.dart @@ -0,0 +1,124 @@ +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/search/search_screen.dart'; +import 'package:pipelines_app/features/search/ui/search_result_slivers.dart'; + +import 'search_screen_test.dart' + show ImmediateGateway, field, response, search, useNarrowSurface; +import 'search_test_support.dart' show chatHit; + +/// Run hits for every kind but chat; chat hits for the chat request. +JsonMap replies(String query, String kind) => kind == 'chat' + ? response([ + chatHit('ses_a', title: 'API refactor', snippet: 'the release retry'), + chatHit('ses_b', title: 'Release notes'), + ]) + : response([field]); + +Widget host(Widget child, {required bool cupertino}) => + cupertino ? CupertinoApp(home: child) : MaterialApp(home: child); + +void main() { + for (final cupertino in [false, true]) { + final platform = cupertino ? 'Cupertino' : 'Material'; + + testWidgets('$platform: chat hits lead as a Chats group with title, ' + 'harness, directory and snippet, and open their chat', (tester) async { + useNarrowSurface(tester); + final gateway = ImmediateGateway(replies); + JsonMap? opened; + await tester.pumpWidget( + host( + SearchScreen( + gateway: gateway, + onOpen: (value) => opened = value, + cupertino: cupertino, + ), + cupertino: cupertino, + ), + ); + await search(tester, 'release'); + + expect(gateway.calls.single['search'], { + 'query': 'release', + 'kind': '', + 'limit': 0, + }); + expect(gateway.chatCalls.single['search'], { + 'query': 'release', + 'kind': 'chat', + 'limit': 0, + }); + expect(find.text('Chats (2)'), findsOneWidget); + expect(find.text('Pipelines and runs'), findsOneWidget); + final chats = tester.getTopLeft(find.byKey(searchChatsSectionKey)); + final others = tester.getTopLeft(find.byKey(searchOthersSectionKey)); + final chatRow = find.byKey(const ValueKey('search.result.chat:ses_a')); + final runRow = find.byKey( + const ValueKey('search.result.run:11:4:output'), + ); + expect(chats.dy, lessThan(tester.getTopLeft(chatRow).dy)); + expect(tester.getTopLeft(chatRow).dy, lessThan(others.dy)); + expect(others.dy, lessThan(tester.getTopLeft(runRow).dy)); + + expect(find.text('API refactor'), findsOneWidget); + expect(find.text('Release notes'), findsOneWidget); + expect( + find.textContaining('Chat · Claude Code · /work/api'), + findsNWidgets(2), + ); + expect(find.textContaining('the release retry'), findsOneWidget); + expect(tester.takeException(), isNull); + + await tester.tap(chatRow); + await tester.pump(); + expect(opened?['target'], 'chat:ses_a'); + expect((opened?['chat'] as Map)['session_id'], 'ses_a'); + }); + } + + testWidgets('Chats selected alone lists chats without group headings', ( + tester, + ) async { + useNarrowSurface(tester); + final gateway = ImmediateGateway(replies); + await tester.pumpWidget( + MaterialApp( + home: SearchScreen(gateway: gateway, onOpen: (_) {}, cupertino: false), + ), + ); + await search(tester, 't:chat release'); + + expect(gateway.calls, isEmpty); + expect(gateway.chatCalls, hasLength(1)); + expect(find.text('API refactor'), findsOneWidget); + expect(find.byKey(searchChatsSectionKey), findsNothing); + expect(find.byKey(searchOthersSectionKey), findsNothing); + }); + + testWidgets('an older server without chat search keeps its run results and ' + 'says chats were not searched', (tester) async { + useNarrowSurface(tester); + final gateway = ImmediateGateway((query, kind) { + if (kind == 'chat') { + throw StateError('unknown search kind "chat"; use one of pipeline'); + } + return response([field]); + }); + await tester.pumpWidget( + MaterialApp( + home: SearchScreen(gateway: gateway, onOpen: (_) {}, cupertino: false), + ), + ); + await search(tester, 'release'); + + expect( + find.byKey(const ValueKey('search.result.run:11:4:output')), + findsOneWidget, + ); + expect(find.byKey(searchChatsNoteKey), findsOneWidget); + expect(find.textContaining('does not search chats'), findsOneWidget); + }); +} diff --git a/app/test/features/search/search_query_test.dart b/app/test/features/search/search_query_test.dart index 942a9b73..b6cda0aa 100644 --- a/app/test/features/search/search_query_test.dart +++ b/app/test/features/search/search_query_test.dart @@ -14,6 +14,7 @@ void main() { 'step', 'field', 'artifact', + 'chat', 'version', ]); expect(SearchKind.values.map((kind) => kind.label), [ @@ -23,6 +24,7 @@ void main() { 'Steps', 'Fields', 'Artifacts', + 'Chats', 'Versions', ]); expect(SearchKind.fromWire('run'), SearchKind.run); diff --git a/app/test/features/search/search_screen_states_test.dart b/app/test/features/search/search_screen_states_test.dart index 6bf5c5cb..69409bb1 100644 --- a/app/test/features/search/search_screen_states_test.dart +++ b/app/test/features/search/search_screen_states_test.dart @@ -10,10 +10,13 @@ class RecordingGateway implements ApiGateway { final JsonMap Function(String query, String kind) reply; final calls = []; + /// The `kind: chat` searches sent beside the others. + final chatCalls = []; + @override Future call(String type, [JsonMap fields = const {}]) async { - calls.add(Map.of(fields)); final search = Map.from(fields['search'] as Map); + (search['kind'] == 'chat' ? chatCalls : calls).add(Map.of(fields)); return reply(search['query'] as String, search['kind'] as String); } @@ -56,7 +59,7 @@ void main() { expect(gateway.calls, isEmpty); expect( - find.text('Search pipelines, runs and artifacts.'), + find.text('Search pipelines, runs, artifacts and chats.'), findsOneWidget, ); expect(find.textContaining('No matches'), findsNothing); diff --git a/app/test/features/search/search_screen_test.dart b/app/test/features/search/search_screen_test.dart index 5e866264..1ec3ba60 100644 --- a/app/test/features/search/search_screen_test.dart +++ b/app/test/features/search/search_screen_test.dart @@ -10,10 +10,13 @@ class ImmediateGateway implements ApiGateway { final JsonMap Function(String query, String kind) reply; final calls = []; + /// The `kind: chat` searches sent beside the others. + final chatCalls = []; + @override Future call(String type, [JsonMap fields = const {}]) async { - calls.add(Map.of(fields)); final search = Map.from(fields['search'] as Map); + (search['kind'] == 'chat' ? chatCalls : calls).add(Map.of(fields)); return reply(search['query'] as String, search['kind'] as String); } @@ -29,6 +32,8 @@ class FailingThenSucceedingGateway implements ApiGateway { @override Future call(String type, [JsonMap fields = const {}]) async { + final search = fields['search']; + if (search is Map && search['kind'] == 'chat') return success; calls++; if (calls == 1) throw StateError('unknown command: search'); return success; diff --git a/app/test/features/search/search_test_support.dart b/app/test/features/search/search_test_support.dart index fdad7425..9dc97d00 100644 --- a/app/test/features/search/search_test_support.dart +++ b/app/test/features/search/search_test_support.dart @@ -4,12 +4,28 @@ import 'package:pipelines_app/core/api_gateway.dart'; import 'package:pipelines_app/features/search/data/search_repository.dart'; import 'package:pipelines_app/features/search/domain/search_controller.dart'; +/// Queues run/artifact searches in [calls]/[pending]. A `kind: chat` search +/// (sent beside every all-kind search) is recorded in [chatCalls] and, unless +/// [holdChats] is set, answered at once with no chats. class QueuedGateway implements ApiGateway { + QueuedGateway({this.holdChats = false}); + + final bool holdChats; final calls = []; final pending = >[]; + final chatCalls = []; + final chatPending = >[]; @override Future call(String type, [JsonMap fields = const {}]) { + final search = fields['search']; + if (search is Map && search['kind'] == 'chat') { + chatCalls.add(Map.of(fields)); + if (!holdChats) return Future.value(envelope(const [])); + final completer = Completer(); + chatPending.add(completer); + return completer.future; + } calls.add(Map.of(fields)); final completer = Completer(); pending.add(completer); @@ -48,3 +64,18 @@ OmniSearchController controllerFor(ApiGateway gateway) => OmniSearchController( repository: SearchRepository(gateway), debounce: Duration.zero, ); + +/// A server-shaped `kind: chat` hit; a [snippet] makes it a message match. +JsonMap chatHit(String id, {String title = 'Fix CI', String? snippet}) => { + 'kind': 'chat', + 'target': 'chat:$id', + 'score': snippet == null ? 3 : 1, + 'matched_field': snippet == null ? 'title' : 'message', + 'chat': { + 'session_id': id, + 'title': title, + 'directory': '/work/api', + 'backend': 'claude', + 'snippet': ?snippet, + }, +}; diff --git a/app/tool/perf/chat_search_fixture.dart b/app/tool/perf/chat_search_fixture.dart new file mode 100644 index 00000000..984d9f6e --- /dev/null +++ b/app/tool/perf/chat_search_fixture.dart @@ -0,0 +1,79 @@ +// Server-shaped `kind: chat` search replies over the `?chats=N` sessions +// (tool/perf/perf_fixture.dart), so Search can show and open chat hits in +// captures. Without `?chats=` there are no chats and no hits. +import 'package:pipelines_app/core/api_gateway.dart'; + +import 'perf_fixture.dart'; + +/// Hits the way `internal/server/search_chats.go` answers: a title or +/// directory match from the registry row, else the newest transcript message +/// whose text matches, with a snippet around the match. One hit per chat, best +/// match first. A blank query lists every chat. +List perfChatSearchHits(String query) { + final needle = query.trim().toLowerCase(); + final hits = []; + for (final row in perfChatRows()) { + final title = '${row['title'] ?? ''}'; + final directory = '${row['directory'] ?? ''}'; + final chat = { + 'session_id': row['id'], + 'title': title, + 'directory': directory, + 'backend': row['backend'] ?? 'opencode', + 'updated_at': ?row['updated_at'], + }; + String field; + int score; + if (needle.isEmpty) { + field = ''; + score = 0; + } else if (title.toLowerCase().contains(needle)) { + field = 'title'; + score = 3; + } else if (directory.toLowerCase().contains(needle)) { + field = 'directory'; + score = 2; + } else { + final match = _newestMatch(needle); + if (match == null) continue; + field = 'message'; + score = 1; + chat + ..['message_id'] = 'perf-m${match.index}' + ..['role'] = match.index.isEven ? 'user' : 'assistant' + ..['snippet'] = match.snippet; + } + final updated = row['updated_at']; + hits.add({ + 'kind': 'chat', + 'target': 'chat:${row['id']}', + 'score': score, + 'matched_field': field, + if (updated is int) + 'recency': DateTime.fromMillisecondsSinceEpoch( + updated, + isUtc: true, + ).toIso8601String(), + 'chat': chat, + }); + } + hits.sort((a, b) => (b['score'] as int).compareTo(a['score'] as int)); + return hits; +} + +({int index, String snippet})? _newestMatch(String needle) { + for (var m = 19; m >= 0; m--) { + final line = perfChatMessageText(m).replaceAll(RegExp(r'\s+'), ' '); + final at = line.toLowerCase().indexOf(needle); + if (at < 0) continue; + final start = at - 40 < 0 ? 0 : at - 40; + final end = at + needle.length + 40 > line.length + ? line.length + : at + needle.length + 40; + final snippet = + '${start > 0 ? '…' : ''}${line.substring(start, end)}' + '${end < line.length ? '…' : ''}'; + return (index: m, snippet: snippet); + } + return null; +} diff --git a/app/tool/perf/perf_fixture.dart b/app/tool/perf/perf_fixture.dart index 685023e0..0621e4bb 100644 --- a/app/tool/perf/perf_fixture.dart +++ b/app/tool/perf/perf_fixture.dart @@ -264,3 +264,10 @@ JsonMap? perfChatReply(JsonMap request) { } return null; } + +/// The `?chats=N` list rows, as chat_sessions serves them. +List perfChatRows() => [for (var i = 0; i < _chats; i++) _chatRow(i)]; + +/// The text of message `perf-m` in the bench's short transcript, newest +/// last (20 messages). +String perfChatMessageText(int m) => m.isEven ? 'Question $m' : _answer(m); diff --git a/app/tool/search_selection_probe_entry.dart b/app/tool/search_selection_probe_entry.dart index 52bb9e3c..f28b6b05 100644 --- a/app/tool/search_selection_probe_entry.dart +++ b/app/tool/search_selection_probe_entry.dart @@ -28,6 +28,7 @@ import 'package:web/web.dart' as web; import 'package:web_socket_channel/web_socket_channel.dart'; import 'perf/backdrop_census.dart'; +import 'perf/chat_search_fixture.dart'; import 'perf/chat_scroll_fixture.dart'; import 'perf/chat_scroll_probe.dart'; import 'perf/frame_log.dart'; @@ -449,6 +450,24 @@ JsonMap? probeReply(JsonMap request) { } final query = (search['query'] as String).trim().toLowerCase(); final kind = search['kind'] as String; + if (kind == 'chat') { + // The server answers chats as their own request (search_chats.go). + return { + 'id': id, + 'type': 'search_results', + 'search_results': { + 'query': search['query'], + 'kind': kind, + 'results': perfChatSearchHits(query), + 'truncated': false, + 'scanned_steps': 0, + 'scanned_bytes': 0, + 'cap': 50, + 'scan_bound_bytes': 1048576, + 'scanned_chats': 3, + }, + }; + } final results = [ for (final entry in probeHits) if ((kind.isEmpty || entry.hit['kind'] == kind) && diff --git a/docs/cli.md b/docs/cli.md index a81da118..ff0a0c7c 100644 --- a/docs/cli.md +++ b/docs/cli.md @@ -118,6 +118,20 @@ confirmation. - Targets are stable and navigable: `pipeline:`, `pipeline::v`, `run:`, `run::`, `run:::`, or an artifact URL served by the artifact endpoint. +- Chats: a `search` request with `kind: "chat"` (protocol only; the all-kind + search never includes chats, so older clients see no change) matches the + operator's chat sessions. Every listed chat's title and directory are + matched from the registry; for up to 24 of the most recently used chats not + matched there, the newest 100 messages' text is read through the chat broker, + four at a time, within a one-second budget. A transcript still unread when the + budget expires is left out and the reply says `truncated`; `scanned_chats` + reports how many transcripts were read. Each chat yields at most one hit + (`target` `chat:`, `matched_field` `title`, `directory` or + `message`) whose `chat` payload carries the title, directory, harness and, + for a message match, the message id, role and a snippet. The server answers it + off the connection's read loop, so it never delays other replies; a newer chat + search on the same connection cancels the previous one. The app sends it after + its run search and shows the hits as a Chats group. ```sh pipelines search "release notes" --json diff --git a/internal/pipeline/search.go b/internal/pipeline/search.go index 6669dfab..b3255b23 100644 --- a/internal/pipeline/search.go +++ b/internal/pipeline/search.go @@ -701,42 +701,8 @@ func findMatch(value any, text string) (string, bool) { return "", false } -// searchSnippet reduces text to a single clean line and centers a short window -// on the first case-insensitive match, folding runes so UTF-8 boundaries are -// never split and the result is always well-formed. -func searchSnippet(text, query string) string { - normalized := protocol.NormalizeText(text) - runes := []rune(normalized) - index := protocol.RuneIndexFold(runes, []rune(query)) - if index < 0 { - return truncateRunes(normalized, 80) - } - const margin = 40 - start := index - margin - if start < 0 { - start = 0 - } - end := index + len([]rune(query)) + margin - if end > len(runes) { - end = len(runes) - } - snippet := string(runes[start:end]) - if start > 0 { - snippet = "…" + snippet - } - if end < len(runes) { - snippet = snippet + "…" - } - return snippet -} - -func truncateRunes(text string, limit int) string { - runes := []rune(text) - if len(runes) <= limit { - return text - } - return string(runes[:limit]) + "…" -} +// searchSnippet is the shared protocol snippet window. +func searchSnippet(text, query string) string { return protocol.SearchSnippet(text, query) } func searchPipelineTarget(id int64) string { return fmt.Sprintf("pipeline:%d", id) } diff --git a/internal/protocol/search.go b/internal/protocol/search.go index 3e8af7dd..71078fb1 100644 --- a/internal/protocol/search.go +++ b/internal/protocol/search.go @@ -24,6 +24,12 @@ const ( SearchKindStep = "step" SearchKindField = "field" SearchKindArtifact = "artifact" + // SearchKindChat narrows to operator chat sessions. It is not part of + // SearchKinds: the store's all-kind search never includes chats, so an + // older client's reply is unchanged. A client asks for chats with this + // kind as its own request, which the server answers concurrently with + // (and never ahead of or blocking) the run/artifact search. + SearchKindChat = "chat" ) // SearchKinds is the accepted Kind vocabulary in display order; the CLI help @@ -52,6 +58,13 @@ const ( // SearchMaxScanSteps bounds how many recorded steps one query inspects while // scanning input/output/transcript field text. SearchMaxScanSteps = 2000 + // SearchMaxScanChats bounds how many of the most recently used operator + // chats one chat search reads message text from; titles and directories + // of every listed chat are always matched. + SearchMaxScanChats = 24 + // SearchChatMessagesPerChat is how many of a chat's newest messages one + // chat search reads. + SearchChatMessagesPerChat = 100 // SearchMaxScanBytes bounds the total field bytes (input_json + output_json + // evidence_json + transcript) one query decodes and searches before it stops // and reports Truncated. @@ -85,12 +98,15 @@ type SearchResponse struct { ScannedBytes int `json:"scanned_bytes"` Cap int `json:"cap"` ScanBoundBytes int `json:"scan_bound_bytes"` + // ScannedChats is how many chat transcripts a chat search read message + // text from (kind chat only); additive, so older peers ignore it. + ScannedChats int `json:"scanned_chats,omitempty"` } // SearchResult is one typed, navigable match. Kind selects which payload is // set; Target is a stable token an existing CLI command can resolve // (pipeline:, pipeline::v, run:, run::, -// run:::, or an artifact URL). Score and MatchedField +// run:::, an artifact URL, or chat:). Score and MatchedField // expose the ranking tier and why the hit matched so a client can re-rank; // Recency is the hit's sortable UTC timestamp. type SearchResult struct { @@ -105,6 +121,7 @@ type SearchResult struct { Step *SearchStep `json:"step,omitempty"` Field *SearchField `json:"field,omitempty"` Artifact *SearchArtifact `json:"artifact,omitempty"` + Chat *SearchChat `json:"chat,omitempty"` } // SearchPipeline is a pipeline name/identity hit: its latest version and stage count. @@ -166,6 +183,24 @@ type SearchArtifact struct { URL string `json:"url"` } +// SearchChat is an operator chat session hit: its registry facts and, for a +// message-text match, the matching message's id, role and a snippet around +// the first match. MatchedField is "title", "directory" or "message". +type SearchChat struct { + SessionID string `json:"session_id"` + Title string `json:"title"` + Directory string `json:"directory,omitempty"` + Backend string `json:"backend,omitempty"` + Closed bool `json:"closed,omitempty"` + UpdatedAt int64 `json:"updated_at,omitempty"` + MessageID string `json:"message_id,omitempty"` + Role string `json:"role,omitempty"` + Snippet string `json:"snippet,omitempty"` +} + +// SearchChatTarget is a chat hit's target token. +func SearchChatTarget(sessionID string) string { return "chat:" + sessionID } + // Label is the compact human description of a hit, built from the result's // typed payload rather than its target token, so it is the single definition // shared by the CLI and the TUI. A text-field hit's snippet is deliberately not @@ -198,10 +233,45 @@ func (r SearchResult) Label() string { if r.Artifact != nil { return fmt.Sprintf("run %d · step %d · %s · %s", r.Artifact.RunID, r.Artifact.Sequence, r.Artifact.Name, r.Artifact.ContentType) } + case SearchKindChat: + if r.Chat != nil { + label := "chat · " + r.Chat.Title + if r.Chat.Directory != "" { + label += " · " + r.Chat.Directory + } + return label + } } return r.Target } +// SearchSnippet reduces text to a single clean line and centers a short window +// on the first case-insensitive match, folding runes so UTF-8 boundaries are +// never split and the result is always well-formed. Text with no match is +// truncated from its start. +func SearchSnippet(text, query string) string { + normalized := NormalizeText(text) + runes := []rune(normalized) + index := RuneIndexFold(runes, []rune(query)) + if index < 0 { + if len(runes) <= 80 { + return normalized + } + return string(runes[:80]) + "…" + } + const margin = 40 + start := max(index-margin, 0) + end := min(index+len([]rune(query))+margin, len(runes)) + snippet := string(runes[start:end]) + if start > 0 { + snippet = "…" + snippet + } + if end < len(runes) { + snippet = snippet + "…" + } + return snippet +} + // RuneIndexFold returns the rune index where query first matches text // case-insensitively, or -1 when it does not. It folds runes rather than bytes // so a caller never splits a UTF-8 boundary, and an empty query never matches. diff --git a/internal/protocol/search_chat_test.go b/internal/protocol/search_chat_test.go new file mode 100644 index 00000000..a7241cba --- /dev/null +++ b/internal/protocol/search_chat_test.go @@ -0,0 +1,67 @@ +package protocol + +import ( + "encoding/json" + "slices" + "strings" + "testing" +) + +// TestSearchChatResultRoundTrip pins the additive chat hit: its payload and +// ScannedChats survive the wire, and a reply without them encodes neither key, +// so an older peer's search_results body is unchanged. +func TestSearchChatResultRoundTrip(t *testing.T) { + response := Message{Type: SearchResults, ID: "c1", SearchResults: &SearchResponse{ + Query: "flaky", Kind: SearchKindChat, Cap: 50, ScannedChats: 3, + Results: []SearchResult{{ + Kind: SearchKindChat, Target: SearchChatTarget("ses_abc"), Score: 1, MatchedField: "message", + Chat: &SearchChat{SessionID: "ses_abc", Title: "Fix CI", Directory: "/work/repo", Backend: "claude", MessageID: "m9", Role: "assistant", Snippet: "the flaky test", UpdatedAt: 1700000000000}, + }}, + }} + encoded, err := json.Marshal(response) + if err != nil { + t.Fatal(err) + } + var echoed Message + if err := json.Unmarshal(encoded, &echoed); err != nil { + t.Fatal(err) + } + got := echoed.SearchResults + if got == nil || got.ScannedChats != 3 || got.Kind != SearchKindChat || len(got.Results) != 1 { + t.Fatalf("chat search response lost: %+v", got) + } + hit := got.Results[0] + if hit.Target != "chat:ses_abc" || hit.Chat == nil || *hit.Chat != *response.SearchResults.Results[0].Chat { + t.Fatalf("chat payload lost: %+v", hit) + } + if label := hit.Label(); label != "chat · Fix CI · /work/repo" { + t.Fatalf("label = %q", label) + } + + plain, err := json.Marshal(SearchResponse{Results: []SearchResult{{Kind: SearchKindRun, Target: "run:1"}}}) + if err != nil { + t.Fatal(err) + } + if strings.Contains(string(plain), "scanned_chats") || strings.Contains(string(plain), `"chat"`) { + t.Fatalf("a run-only reply encodes chat keys: %s", plain) + } +} + +// TestSearchKindChatIsNotAStoreKind keeps chats out of the all-kind store +// vocabulary, so an older client's default search never changes shape. +func TestSearchKindChatIsNotAStoreKind(t *testing.T) { + if slices.Contains(SearchKinds, SearchKindChat) { + t.Fatalf("SearchKinds = %v, chat must be requested on its own", SearchKinds) + } +} + +func TestSearchSnippetWindowsTheMatch(t *testing.T) { + text := strings.Repeat("a", 60) + " Needle here " + strings.Repeat("b", 60) + got := SearchSnippet(text, "needle") + if !strings.HasPrefix(got, "…") || !strings.HasSuffix(got, "…") || !strings.Contains(got, "Needle") { + t.Fatalf("snippet = %q", got) + } + if got := SearchSnippet("short\nline", "zzz"); got != "short line" { + t.Fatalf("unmatched short snippet = %q", got) + } +} diff --git a/internal/server/search_chats.go b/internal/server/search_chats.go new file mode 100644 index 00000000..35d0c964 --- /dev/null +++ b/internal/server/search_chats.go @@ -0,0 +1,228 @@ +package server + +import ( + "context" + "sort" + "strings" + "time" + + "git.a-wan.net/alan/pipelines/internal/pipeline" + "git.a-wan.net/alan/pipelines/internal/protocol" +) + +// chatSearchBudget bounds how long one chat search waits for transcript +// readback. Registry (title/directory) hits never wait on it; a transcript +// still unread when it expires is left out and the reply says Truncated. +// It is short because a client that serializes its requests (the app does) +// holds its next request behind this reply. It is a variable so a test can +// shorten it. +var chatSearchBudget = time.Second + +// chatSearchConcurrency is how many transcripts one chat search reads at once. +const chatSearchConcurrency = 4 + +// Chat hit scores: a title match outranks a directory match, which outranks a +// message-text match; recency breaks ties. +const ( + chatScoreTitleExact = 4 + chatScoreTitle = 3 + chatScoreDirectory = 2 + chatScoreMessage = 1 +) + +// isChatSearch reports a search request narrowed to chats. It is answered by +// searchChats, off the connection's read loop, so it never delays the +// run/artifact search a client sends beside it. +func isChatSearch(request protocol.Message) bool { + return request.Type == protocol.Search && request.Search != nil && + strings.TrimSpace(request.Search.Kind) == protocol.SearchKindChat +} + +// chatSearchReply answers one kind=chat search request. +func chatSearchReply(ctx context.Context, request protocol.Message, broker chatBroker, registry operatorRegistry) protocol.Message { + if registry == nil { + return protocol.Message{Type: protocol.Error, ID: request.ID, Error: "chat search unavailable: operator session registry is not configured on this server"} + } + response, err := searchChats(ctx, *request.Search, broker, registry) + if err != nil { + return protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()} + } + return protocol.Message{Type: protocol.SearchResults, ID: request.ID, SearchResults: response} +} + +// searchChats matches the operator's chat sessions: the title and directory of +// every listed registry session (one bounded statement), then the newest +// SearchChatMessagesPerChat messages of up to SearchMaxScanChats of the most +// recently used sessions the registry did not already match, read through the +// chat broker concurrently within chatSearchBudget. One session yields at most +// one hit, its best match. A blank query lists the most recent chats. +func searchChats(ctx context.Context, req protocol.SearchRequest, broker chatBroker, registry operatorRegistry) (*protocol.SearchResponse, error) { + query := strings.TrimSpace(req.Query) + response := &protocol.SearchResponse{ + Query: req.Query, + Kind: protocol.SearchKindChat, + Results: []protocol.SearchResult{}, + Cap: chatSearchLimit(req.Limit), + ScanBoundBytes: protocol.SearchMaxScanBytes, + } + sessions, err := registry.OperatorSessions(ctx) + if err != nil { + return nil, err + } + var results []protocol.SearchResult + var unmatched []pipeline.OperatorSession + for _, session := range sessions { + if query == "" { + results = append(results, chatSearchHit(session, 0, "")) + continue + } + switch { + case strings.EqualFold(session.Title, query): + results = append(results, chatSearchHit(session, chatScoreTitleExact, "title")) + case containsFold(session.Title, query): + results = append(results, chatSearchHit(session, chatScoreTitle, "title")) + case containsFold(session.Directory, query): + results = append(results, chatSearchHit(session, chatScoreDirectory, "directory")) + default: + unmatched = append(unmatched, session) + } + } + if query != "" && broker != nil && len(unmatched) > 0 { + if len(unmatched) > protocol.SearchMaxScanChats { + unmatched = unmatched[:protocol.SearchMaxScanChats] + } + hits, scanned, complete := searchChatTranscripts(ctx, broker, unmatched, query) + results = append(results, hits...) + response.ScannedChats = scanned + if !complete { + response.Truncated = true + } + } + sort.SliceStable(results, func(i, j int) bool { + a, b := results[i], results[j] + if a.Score != b.Score { + return a.Score > b.Score + } + if a.Recency != b.Recency { + return a.Recency > b.Recency + } + return a.Target < b.Target + }) + if len(results) > response.Cap { + results = results[:response.Cap] + response.Truncated = true + } + if results != nil { + response.Results = results + } + return response, nil +} + +// chatSearchLimit clamps a requested cap like the store's search does. +func chatSearchLimit(limit int) int { + if limit <= 0 { + return protocol.SearchDefaultLimit + } + return min(limit, protocol.SearchMaxLimit) +} + +// chatTranscriptHit is one transcript read's outcome. +type chatTranscriptHit struct { + hit *protocol.SearchResult + scanned bool +} + +// searchChatTranscripts reads each session's newest messages with bounded +// concurrency and returns the message hits that arrived within the budget, +// how many transcripts were read, and whether every read finished. Workers +// that outlive the budget see a canceled context and write into a buffered +// channel, so none of them blocks or leaks past its own read. +func searchChatTranscripts(ctx context.Context, broker chatBroker, sessions []pipeline.OperatorSession, query string) ([]protocol.SearchResult, int, bool) { + ctx, cancel := context.WithTimeout(ctx, chatSearchBudget) + defer cancel() + jobs := make(chan pipeline.OperatorSession, len(sessions)) + for _, session := range sessions { + jobs <- session + } + close(jobs) + out := make(chan chatTranscriptHit, len(sessions)) + for range min(chatSearchConcurrency, len(sessions)) { + go func() { + for session := range jobs { + if ctx.Err() != nil { + out <- chatTranscriptHit{} + continue + } + out <- searchChatTranscript(ctx, broker, session, query) + } + }() + } + var hits []protocol.SearchResult + scanned := 0 + for range sessions { + select { + case result := <-out: + if result.scanned { + scanned++ + } + if result.hit != nil { + hits = append(hits, *result.hit) + } + case <-ctx.Done(): + return hits, scanned, false + } + } + return hits, scanned, scanned == len(sessions) +} + +// searchChatTranscript matches query against the text parts of one session's +// newest messages, newest first, and returns a hit for the newest match. +func searchChatTranscript(ctx context.Context, broker chatBroker, session pipeline.OperatorSession, query string) chatTranscriptHit { + page, err := broker.Messages(ctx, session.ID, protocol.SearchChatMessagesPerChat, "") + if err != nil || ctx.Err() != nil { + return chatTranscriptHit{} + } + for i := len(page.Messages) - 1; i >= 0; i-- { + message := page.Messages[i] + for _, part := range message.Parts { + if part.Type != protocol.ChatPartText || !containsFold(part.Text, query) { + continue + } + hit := chatSearchHit(session, chatScoreMessage, "message") + hit.Chat.MessageID = message.ID + hit.Chat.Role = message.Role + hit.Chat.Snippet = protocol.SearchSnippet(part.Text, query) + return chatTranscriptHit{hit: &hit, scanned: true} + } + } + return chatTranscriptHit{scanned: true} +} + +func chatSearchHit(session pipeline.OperatorSession, score int, field string) protocol.SearchResult { + backend := session.Backend + if backend == "" { + backend = "opencode" + } + hit := protocol.SearchResult{ + Kind: protocol.SearchKindChat, + Target: protocol.SearchChatTarget(session.ID), + Score: score, + MatchedField: field, + Chat: &protocol.SearchChat{ + SessionID: session.ID, + Title: session.Title, + Directory: session.Directory, + Backend: backend, + Closed: session.ClosedAt > 0, + UpdatedAt: session.UpdatedAt, + }, + } + if session.UpdatedAt > 0 { + hit.Recency = time.UnixMilli(session.UpdatedAt).UTC().Format(time.RFC3339) + } + return hit +} + +func containsFold(value, query string) bool { + return query != "" && strings.Contains(strings.ToLower(value), strings.ToLower(query)) +} diff --git a/internal/server/search_chats_test.go b/internal/server/search_chats_test.go new file mode 100644 index 00000000..dad2070f --- /dev/null +++ b/internal/server/search_chats_test.go @@ -0,0 +1,166 @@ +package server + +import ( + "context" + "testing" + "time" + + "github.com/coder/websocket" + "github.com/coder/websocket/wsjson" + + "git.a-wan.net/alan/pipelines/internal/protocol" +) + +// transcriptChat serves a fixed newest page per session. A session listed in +// slow blocks until its read is canceled, like a harness that never answers. +type transcriptChat struct { + *fakeChat + pages map[string][]protocol.ChatMessage + slow map[string]bool +} + +func (c *transcriptChat) Messages(ctx context.Context, sessionID string, limit int, _ string) (protocol.ChatHistoryPage, error) { + if c.slow[sessionID] { + <-ctx.Done() + return protocol.ChatHistoryPage{}, ctx.Err() + } + if limit != protocol.SearchChatMessagesPerChat { + return protocol.ChatHistoryPage{}, context.Canceled + } + return protocol.ChatHistoryPage{Messages: c.pages[sessionID]}, nil +} + +func chatText(id, role, text string) protocol.ChatMessage { + return protocol.ChatMessage{ID: id, Role: role, Parts: []protocol.ChatPart{{Type: protocol.ChatPartText, Text: text}}} +} + +func chatSearchFixture(t *testing.T, broker *transcriptChat) (context.Context, *websocket.Conn) { + t.Helper() + store := newOperatorStore(t) + ctx, _, conn := dialChatOptions(t, Options{Chat: broker, OperatorRegistry: store}) + rows := []struct{ id, directory, title, backend string }{ + {"ses_title", "/work/site", "Flaky test hunt", "claude"}, + {"ses_dir", "/work/flaky-repo", "Docs pass", "codex"}, + {"ses_text", "/work/api", "API refactor", ""}, + {"ses_none", "/work/other", "Unrelated", ""}, + {"ses_slow", "/work/slow", "Slow harness", ""}, + } + for _, row := range rows { + if err := store.RecordOperatorSession(ctx, row.id, row.directory, row.title, protocol.ChatSessionTagOperator); err != nil { + t.Fatal(err) + } + if row.backend != "" { + if err := store.SetOperatorSessionBackend(ctx, row.id, row.backend); err != nil { + t.Fatal(err) + } + } + } + return ctx, conn +} + +func chatSearch(t *testing.T, ctx context.Context, conn *websocket.Conn, id, query string) { + t.Helper() + writeChat(t, ctx, conn, protocol.Message{Type: protocol.Search, ID: id, Search: &protocol.SearchRequest{Query: query, Kind: protocol.SearchKindChat}}) +} + +func readReply(t *testing.T, ctx context.Context, conn *websocket.Conn, id string) protocol.Message { + t.Helper() + for { + var reply protocol.Message + if err := wsjson.Read(ctx, conn, &reply); err != nil { + t.Fatalf("read reply %s: %v", id, err) + } + if reply.ID == id { + return reply + } + } +} + +func TestChatSearchMatchesTitleDirectoryAndMessageText(t *testing.T) { + broker := &transcriptChat{fakeChat: scriptedChat(), pages: map[string][]protocol.ChatMessage{ + "ses_text": { + chatText("m1", protocol.ChatRoleUser, "Why is the build flaky?"), + chatText("m2", protocol.ChatRoleAssistant, "The FLAKY part is the retry loop in client.go."), + {ID: "m3", Role: protocol.ChatRoleAssistant, Parts: []protocol.ChatPart{{Type: protocol.ChatPartReasoning, Text: "flaky reasoning is not searched"}}}, + }, + "ses_none": {chatText("n1", protocol.ChatRoleUser, "nothing relevant")}, + "ses_title": {chatText("t1", protocol.ChatRoleUser, "flaky again")}, + }} + ctx, conn := chatSearchFixture(t, broker) + chatSearch(t, ctx, conn, "c1", "flaky") + reply := readReply(t, ctx, conn, "c1") + if reply.Type != protocol.SearchResults || reply.SearchResults == nil { + t.Fatalf("reply = %+v", reply) + } + got := reply.SearchResults + if got.Kind != protocol.SearchKindChat || got.Truncated || got.ScannedChats != 3 { + t.Fatalf("bounds = kind %q truncated %v scanned %d, want chat, complete, 3 transcripts (title/dir hits skip readback)", got.Kind, got.Truncated, got.ScannedChats) + } + if len(got.Results) != 3 { + t.Fatalf("results = %+v, want title, directory and message hits", got.Results) + } + title, dir, text := got.Results[0], got.Results[1], got.Results[2] + if title.Target != "chat:ses_title" || title.MatchedField != "title" || title.Chat.Backend != "claude" || title.Chat.Snippet != "" { + t.Fatalf("title hit = %+v %+v", title, title.Chat) + } + if dir.Target != "chat:ses_dir" || dir.MatchedField != "directory" || dir.Chat.Directory != "/work/flaky-repo" || dir.Chat.Backend != "codex" { + t.Fatalf("directory hit = %+v %+v", dir, dir.Chat) + } + if text.Target != "chat:ses_text" || text.MatchedField != "message" || text.Chat.MessageID != "m2" || text.Chat.Role != protocol.ChatRoleAssistant || text.Chat.Backend != "opencode" { + t.Fatalf("message hit = %+v %+v (want the newest matching text part)", text, text.Chat) + } + if text.Chat.Snippet != "The FLAKY part is the retry loop in client.go." || text.Chat.Title != "API refactor" || text.Recency == "" { + t.Fatalf("message hit facts = %+v", text.Chat) + } +} + +// TestChatSearchBudgetNeverBlocksOtherReplies: a harness that never answers +// only costs its own transcript. The connection keeps answering other requests +// while the chat search waits, and the chat reply still arrives within the +// budget with the registry and finished-transcript hits, marked Truncated. +func TestChatSearchBudgetNeverBlocksOtherReplies(t *testing.T) { + previous := chatSearchBudget + chatSearchBudget = 400 * time.Millisecond + t.Cleanup(func() { chatSearchBudget = previous }) + broker := &transcriptChat{fakeChat: scriptedChat(), slow: map[string]bool{"ses_slow": true}, pages: map[string][]protocol.ChatMessage{ + "ses_text": {chatText("m1", protocol.ChatRoleUser, "a flaky retry")}, + }} + ctx, conn := chatSearchFixture(t, broker) + started := time.Now() + chatSearch(t, ctx, conn, "c1", "flaky") + writeChat(t, ctx, conn, protocol.Message{Type: protocol.Ping, ID: "p1"}) + var first protocol.Message + if err := wsjson.Read(ctx, conn, &first); err != nil { + t.Fatal(err) + } + if first.ID != "p1" { + t.Fatalf("first reply = %s %s, want the ping answered while transcripts load", first.ID, first.Type) + } + reply := readReply(t, ctx, conn, "c1") + if elapsed := time.Since(started); elapsed > 3*time.Second { + t.Fatalf("chat search took %v, budget is %v", elapsed, chatSearchBudget) + } + got := reply.SearchResults + if got == nil || !got.Truncated || len(got.Results) != 3 { + t.Fatalf("reply = %+v, want three hits and Truncated", got) + } + if got.Results[2].Target != "chat:ses_text" { + t.Fatalf("message hit missing: %+v", got.Results) + } +} + +func TestChatSearchBlankListsRecentAndNeedsRegistry(t *testing.T) { + broker := &transcriptChat{fakeChat: scriptedChat()} + ctx, conn := chatSearchFixture(t, broker) + chatSearch(t, ctx, conn, "b1", " ") + reply := readReply(t, ctx, conn, "b1") + if reply.SearchResults == nil || len(reply.SearchResults.Results) != 5 || reply.SearchResults.ScannedChats != 0 { + t.Fatalf("blank chat search = %+v, want every listed chat and no transcript reads", reply.SearchResults) + } + + ctx, _, bare := dialChatOptions(t, Options{Chat: broker}) + chatSearch(t, ctx, bare, "n1", "x") + if reply := readReply(t, ctx, bare, "n1"); reply.Type != protocol.Error { + t.Fatalf("no-registry chat search = %+v, want an error", reply) + } +} diff --git a/internal/server/websocket.go b/internal/server/websocket.go index 1f6da9bf..319e6b6f 100644 --- a/internal/server/websocket.go +++ b/internal/server/websocket.go @@ -217,6 +217,11 @@ func HandlerWithOptions(ctx context.Context, options Options, stores ...*pipelin if notes != nil { defer notes.detach(clientID) } + // A chat search runs beside the read loop so its transcript readback + // never delays the run search sent with it; a newer one cancels the + // previous, whose reply the client would discard as stale anyway. + cancelChatSearch := func() {} + defer func() { cancelChatSearch() }() for { var request protocol.Message if err := wsjson.Read(connCtx, conn, &request); err != nil { @@ -227,6 +232,16 @@ func HandlerWithOptions(ctx context.Context, options Options, stores ...*pipelin _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "pipeline authoring is disabled on this execution server; ask the operator to publish through an explicitly enabled authoring server"}) continue } + if isChatSearch(request) { + cancelChatSearch() + searchCtx, cancel := context.WithCancel(connCtx) + cancelChatSearch = cancel + go func(request protocol.Message) { + defer cancel() + _ = write(chatSearchReply(searchCtx, request, options.Chat, registry)) + }(request) + continue + } if handleChat(connCtx, request, options.Chat, hub, registry, clientID, write) { continue }