diff --git a/app/lib/application/pipelines_app.dart b/app/lib/application/pipelines_app.dart index 35858188..bd963ec3 100644 --- a/app/lib/application/pipelines_app.dart +++ b/app/lib/application/pipelines_app.dart @@ -378,9 +378,13 @@ class _PipelinesAppState extends State field: field, ); route = AppRoute.run(runId); - case SearchArtifactTarget(:final runId, :final sequence): + case SearchArtifactTarget(:final runId, :final sequence, :final url): if (runId != null) { - selection = RunSearchSelection(runId: runId, sequence: sequence); + selection = RunSearchSelection( + runId: runId, + sequence: sequence, + artifactUrl: url, + ); route = AppRoute.run(runId); } } diff --git a/app/lib/features/artifacts/domain/gallery_navigation.dart b/app/lib/features/artifacts/domain/gallery_navigation.dart index 856f31ee..b4c46ea3 100644 --- a/app/lib/features/artifacts/domain/gallery_navigation.dart +++ b/app/lib/features/artifacts/domain/gallery_navigation.dart @@ -38,9 +38,14 @@ String artifactVisitIdentityOf(ArtifactGroup group, ArtifactRecord artifact) { /// The artifacts the viewer can page through, in gallery order: group by group, /// each group's images, videos and markdown/text documents in recorded order, /// then the visit's recorded turn documents that open in the renderer. A session -/// export is external-only and never enters the page list. Pure: it only filters -/// and orders records the gallery already holds. -List pagedArtifacts(List groups) { +/// export is external-only and never enters the page list unless +/// [includeExternalDocuments] is requested by a search result, which must still +/// land on the exact recorded document in the full-screen viewer. Pure: it only +/// filters and orders records the gallery already holds. +List pagedArtifacts( + List groups, { + bool includeExternalDocuments = false, +}) { final pages = []; void add(ArtifactGroup group, ArtifactRecord artifact) { pages.add( @@ -55,7 +60,10 @@ List pagedArtifacts(List groups) { } for (final document in group.documents) { final artifact = document.record; - if (artifact == null || !document.opensInViewer) continue; + if (artifact == null || + (!document.opensInViewer && !includeExternalDocuments)) { + continue; + } add(group, artifact); } } diff --git a/app/lib/features/artifacts/ui/artifact_viewer_page.dart b/app/lib/features/artifacts/ui/artifact_viewer_page.dart index 049b8a4a..88d6c4d8 100644 --- a/app/lib/features/artifacts/ui/artifact_viewer_page.dart +++ b/app/lib/features/artifacts/ui/artifact_viewer_page.dart @@ -41,6 +41,7 @@ class ArtifactViewerPage extends StatefulWidget { super.key, required this.groups, required this.selectedIdentity, + this.includeExternalDocuments = false, this.configuredEndpoint = '', this.fetch = fetchArtifact, this.markdownBuilder = defaultArtifactMarkdown, @@ -53,6 +54,10 @@ class ArtifactViewerPage extends StatefulWidget { /// [artifactVisitIdentityOf] of the row the operator selected. final String selectedIdentity; + /// Search can select a turn document that is hidden from the normal gallery, + /// including the external-only session export. + final bool includeExternalDocuments; + final String configuredEndpoint; final ArtifactFetch fetch; final ArtifactMarkdownBuilder markdownBuilder; @@ -71,7 +76,10 @@ class _FetchedPreview { } class _ArtifactViewerPageState extends State { - late final List _pages = pagedArtifacts(widget.groups); + late final List _pages = pagedArtifacts( + widget.groups, + includeExternalDocuments: widget.includeExternalDocuments, + ); final Map _cache = {}; late int _index = _initialIndex(); int _generation = 0; diff --git a/app/lib/features/runs/ui/run_detail_tabs.dart b/app/lib/features/runs/ui/run_detail_tabs.dart index 06ea11d0..2106602d 100644 --- a/app/lib/features/runs/ui/run_detail_tabs.dart +++ b/app/lib/features/runs/ui/run_detail_tabs.dart @@ -1,3 +1,5 @@ +import 'dart:async'; + import '../../../ui/adaptive/floating_bar_layout.dart'; import '../../../ui/adaptive/segmented_bar.dart'; import '../../../ui/adaptive/top_glass_strip.dart'; @@ -11,6 +13,10 @@ import '../../../core/api_gateway.dart'; import '../../graph/widgets/run_graph_view.dart'; import '../../artifacts/ui/artifact_gallery_action.dart'; import '../../artifacts/ui/artifact_gallery_page.dart'; +import '../../artifacts/domain/artifact_groups.dart'; +import '../../artifacts/domain/artifact_record.dart'; +import '../../artifacts/domain/gallery_navigation.dart'; +import '../../artifacts/ui/artifact_viewer_page.dart'; import '../../changes/changes.dart'; import 'run_changes_action.dart'; import 'run_detail_content.dart'; @@ -110,6 +116,10 @@ class _RunDetailTabsState extends State { /// request never reopens a pane the operator closed in the meantime. RunSearchSelection? _appliedSearchSelection; + /// Search artifacts open above their visit, so the normal visit-page sync + /// must wait for the search callback to install both routes. + bool _searchArtifactVisitPending = false; + @override void initState() { super.initState(); @@ -239,9 +249,83 @@ class _RunDetailTabsState extends State { _selectedStage = stageKey; _selectedVisit = timelineIndex; _tab = RunDetailTab.timeline; + case RunArtifactTarget(:final visit, :final url): + if (detailVisitStepAt(widget.run, visit.timelineIndex) == null) return; + // The viewer is opened above the visit. Prevent build() from opening + // that visit first in its own callback; the search callback installs + // the visit route and then the viewer route in that order. + _selectedStage = visit.stageKey; + _selectedVisit = visit.timelineIndex; + _tab = RunDetailTab.timeline; + _searchArtifactVisitPending = true; + WidgetsBinding.instance.addPostFrameCallback( + (_) => mounted ? _openSearchArtifact(url, visit.timelineIndex) : null, + ); } } + void _openSearchArtifact(String url, int visitIndex) { + // _pushVisit reaches Navigator.push before its first await. Install the + // visit first so it remains the run-detail destination on Back, including + // when the recorded artifact has disappeared. + unawaited(_pushVisit(visitIndex)); + final steps = detailRunSteps(widget.run); + final groups = galleryGroups(steps); + ArtifactGroup? selectedGroup; + ArtifactRecord? selectedArtifact; + for (final group in groups) { + for (final artifact in [ + ...group.artifacts, + ...group.documents.map((document) => document.record), + ]) { + if (artifact == null || artifact.url != url) continue; + selectedGroup = group; + selectedArtifact = artifact; + break; + } + if (selectedArtifact != null) break; + } + if (selectedGroup == null || selectedArtifact == null) { + _showSearchArtifactMissingNotice(); + return; + } + final group = selectedGroup; + final artifact = selectedArtifact; + final page = ArtifactViewerPage( + groups: groups, + selectedIdentity: artifactVisitIdentityOf(group, artifact), + includeExternalDocuments: true, + configuredEndpoint: widget.configuredEndpoint, + ); + Navigator.of(context).push( + widget.cupertino + ? CupertinoPageRoute(builder: (_) => page) + : MaterialPageRoute(builder: (_) => page), + ); + } + + void _showSearchArtifactMissingNotice() { + final messenger = ScaffoldMessenger.maybeOf(context); + if (messenger != null) { + messenger.showSnackBar( + const SnackBar(content: Text('That artifact is no longer available.')), + ); + return; + } + showCupertinoDialog( + context: context, + builder: (context) => CupertinoAlertDialog( + content: const Text('That artifact is no longer available.'), + actions: [ + CupertinoDialogAction( + onPressed: () => Navigator.of(context).pop(), + child: const Text('OK'), + ), + ], + ), + ); + } + /// Selects the stage's latest recorded visit. An unvisited stage has none, /// so the detail stays where it is rather than fabricating a visit. void _openStage(String stageKey) { @@ -352,7 +436,9 @@ class _RunDetailTabsState extends State { @override Widget build(BuildContext context) { // A selected visit opens on its own page; the tab keeps the list. - if (_selectedVisit != _shownVisit) _syncVisitPage(); + if (_selectedVisit != _shownVisit && !_searchArtifactVisitPending) { + _syncVisitPage(); + } final tabs = _tabsFor(widget.run); // A tab the run no longer offers falls back to Summary. final shown = tabs.contains(_tab) || !renderedRunDetailTabs.contains(_tab) diff --git a/app/lib/features/runs/ui/run_search_selection.dart b/app/lib/features/runs/ui/run_search_selection.dart index ccb8bb5a..9dd4efae 100644 --- a/app/lib/features/runs/ui/run_search_selection.dart +++ b/app/lib/features/runs/ui/run_search_selection.dart @@ -11,7 +11,12 @@ import 'run_detail_visit_facts.dart'; /// consumed once, so reopening the run later never replays it. @immutable class RunSearchSelection { - const RunSearchSelection({required this.runId, this.sequence, this.field}); + const RunSearchSelection({ + required this.runId, + this.sequence, + this.field, + this.artifactUrl, + }); final int runId; @@ -21,15 +26,19 @@ class RunSearchSelection { /// The matched step field (`input`, `output`, …), or null for a visit hit. final String? field; + /// The recorded artifact URL for an artifact or turn-document hit. + final String? artifactUrl; + @override bool operator ==(Object other) => other is RunSearchSelection && other.runId == runId && other.sequence == sequence && - other.field == field; + other.field == field && + other.artifactUrl == artifactUrl; @override - int get hashCode => Object.hash(runId, sequence, field); + int get hashCode => Object.hash(runId, sequence, field, artifactUrl); } /// The collapsible Summary section a text hit names. @@ -57,6 +66,15 @@ final class RunVisitTarget extends RunSearchTarget { final int timelineIndex; } +/// A recorded artifact hit selects its visit first, then opens the artifact +/// viewer on the exact recorded file. +final class RunArtifactTarget extends RunSearchTarget { + const RunArtifactTarget({required this.visit, required this.url}); + + final RunVisitTarget visit; + final String url; +} + /// Resolves [selection] against the loaded [run] into the detail state to show. /// /// A text hit always selects the Summary, using the browser's `searchFieldTab` @@ -79,21 +97,31 @@ RunSearchTarget resolveRunSearchSelection( }, ); } - final sequence = selection.sequence; - if (sequence != null && sequence > 0) { - final steps = detailRunSteps(run); - for (var index = 0; index < steps.length; index++) { - if (_sequence(steps[index]) != sequence) continue; - final stage = '${steps[index]['stage'] ?? ''}'.trim(); - if (stage.isEmpty) break; - // The row index is the Timeline's own index, the identity the visit pane - // reads back through detailVisitStepAt, so no ordinal is guessed here. - return RunVisitTarget(stageKey: stage, timelineIndex: index); - } + final artifactUrl = selection.artifactUrl?.trim() ?? ''; + if (artifactUrl.isNotEmpty) { + final visit = _visitTarget(selection, run); + return visit == null + ? const RunSummaryTarget() + : RunArtifactTarget(visit: visit, url: artifactUrl); } + final visit = _visitTarget(selection, run); + if (visit != null) return visit; return const RunSummaryTarget(); } +RunVisitTarget? _visitTarget(RunSearchSelection selection, JsonMap run) { + final sequence = selection.sequence; + if (sequence == null || sequence <= 0) return null; + final steps = detailRunSteps(run); + for (var index = 0; index < steps.length; index++) { + if (_sequence(steps[index]) != sequence) continue; + final stage = '${steps[index]['stage'] ?? ''}'.trim(); + if (stage.isEmpty) return null; + return RunVisitTarget(stageKey: stage, timelineIndex: index); + } + return null; +} + int? _sequence(JsonMap step) { final value = step['sequence']; final number = value is num ? value.toInt() : int.tryParse('${value ?? ''}'); diff --git a/app/test/features/artifacts/domain/gallery_navigation_test.dart b/app/test/features/artifacts/domain/gallery_navigation_test.dart index cab78756..d65e29dd 100644 --- a/app/test/features/artifacts/domain/gallery_navigation_test.dart +++ b/app/test/features/artifacts/domain/gallery_navigation_test.dart @@ -1,4 +1,5 @@ import 'package:flutter_test/flutter_test.dart'; +import 'package:pipelines_app/features/artifacts/domain/artifact_document.dart'; import 'package:pipelines_app/features/artifacts/domain/artifact_groups.dart'; import 'package:pipelines_app/features/artifacts/domain/artifact_record.dart'; import 'package:pipelines_app/features/artifacts/domain/gallery_navigation.dart'; @@ -171,6 +172,38 @@ void main() { expect(pagedArtifacts(groups), isEmpty); expect(viewerIndexByUrl(groups), isEmpty); }); + + test('search navigation can include an external-only session export', () { + final groups = [ + ArtifactGroup( + stageKey: 'a', + visit: 1, + artifacts: const [], + documents: [ + ArtifactDocument( + name: 'session.html', + record: ArtifactRecord( + name: 'session.html', + url: '/artifacts/1/session.html', + contentType: 'text/html', + sizeBytes: 1, + raw: {}, + ), + absence: '', + ), + ], + ), + ]; + + expect(pagedArtifacts(groups), isEmpty); + expect( + pagedArtifacts( + groups, + includeExternalDocuments: true, + ).single.artifact.name, + 'session.html', + ); + }); }); group('paging', () { diff --git a/app/test/features/runs/ui/run_search_selection_test.dart b/app/test/features/runs/ui/run_search_selection_test.dart index 5fd77fc7..cd5e8576 100644 --- a/app/test/features/runs/ui/run_search_selection_test.dart +++ b/app/test/features/runs/ui/run_search_selection_test.dart @@ -270,6 +270,23 @@ void main() { expect(target.timelineIndex, 1); }); + test('an artifact selection keeps its URL while naming the visit', () { + final target = resolveRunSearchSelection( + const RunSearchSelection( + runId: 77, + sequence: 2, + artifactUrl: '/artifacts/77/2/release-report.txt', + ), + searchSelectionRun(), + ); + + expect(target, isA()); + final artifact = target as RunArtifactTarget; + expect(artifact.url, '/artifacts/77/2/release-report.txt'); + expect(artifact.visit.stageKey, 'build'); + expect(artifact.visit.timelineIndex, 1); + }); + test('an unrecorded sequence falls back to the plain Summary', () { _expectSummarySection( resolveRunSearchSelection(