Files
pipelines/app/lib/features/runs/ui/run_detail_visit_facts.dart
T
alan f57d8eeb76 feat(app): open a recorded run visit from its URL
`/runs/:id/stage/:key?visit=N` now selects that stage's N-th recorded
visit in the run detail Timeline, matching the web client's stage-local
visit ordering. A stage key without a visit opens the latest recorded
visit, an out-of-range index clamps to the last one, and a stage with no
recorded visit selects only its Graph node instead of inventing a visit.

Route plumbing carries the query end to end: boot seeds the router from
path and query, the parser reads path plus query, and restore keeps `?`
a query separator instead of percent-encoding it. That also repairs the
existing `?action=decide` and `/runs?pipeline=` URLs.

The Graph node click and the stage route share one sequence-ordered
resolver, so both open the same latest visit.
2026-10-22 10:48:36 +01:00

132 lines
5.5 KiB
Dart

import '../../../core/api_gateway.dart';
import '../../graph/domain/stage_definition.dart';
import '../../pipelines/pipeline_format.dart';
import 'run_detail_content.dart';
/// The exact empty wording for a visit's recorded Input and Output. They are
/// the run tabs' own sentences, so a visit never says something different for
/// the same absent fact.
const String visitEmptyInput = 'No input recorded.';
const String visitEmptyOutput = 'No output recorded.';
/// One always-visible fact of a recorded visit.
typedef VisitFact = ({String label, String value});
/// The detail's own recorded steps as maps, in recorded order.
List<JsonMap> detailRunSteps(JsonMap run) {
final steps = run['steps'];
if (steps is! List) return const <JsonMap>[];
return steps.whereType<Map>().map(JsonMap.from).toList();
}
/// The raw step at [index] of the detail's own `steps`, or null when the reply
/// has no such recorded visit.
///
/// The order is the server's recorded order, the same order [detailTimeline]
/// uses, so a row and its pane always describe one visit.
JsonMap? detailVisitStepAt(JsonMap run, int index) {
final steps = detailRunSteps(run);
if (index < 0 || index >= steps.length) return null;
return steps[index];
}
/// The row index of [stageKey]'s latest recorded visit, or null when the run
/// never recorded that stage.
///
/// The order matches [routeDetailVisitIndex] and the web client's
/// `visitsByStage`: a stage's visits follow their recorded sequence, not their
/// position in the reply's step list. So a Graph click and `/runs/:id/stage/:key`
/// open the same visit even when the steps are not in sequence order.
int? latestDetailVisitIndex(dynamic steps, String stageKey) =>
routeDetailVisitIndex(steps, stageKey, null);
/// The timeline row index a route's [stageKey] and [visit] name, or null when
/// the run recorded no visit of that stage.
///
/// A stage's visits follow their recorded sequence, as the web client's
/// `visitsByStage` does, so the same `?visit=N` selects the same visit in both
/// clients while equal sequences keep the timeline's own order. A null [visit]
/// is the stage's last recorded visit, the one a Graph node click opens; an
/// out-of-range index is clamped to that last visit, exactly as the web client
/// clamps it. Nothing is invented for a stage with no recorded visit.
int? routeDetailVisitIndex(dynamic steps, String stageKey, int? visit) {
final visits = detailTimeline(steps);
final indices = <int>[
for (var index = 0; index < visits.length; index += 1)
if (visits[index].stage == stageKey) index,
];
if (indices.isEmpty) return null;
indices.sort((a, b) {
final bySequence = (visits[a].sequence ?? 0).compareTo(
visits[b].sequence ?? 0,
);
return bySequence != 0 ? bySequence : a.compareTo(b);
});
final wanted = visit == null
? indices.length - 1
: visit.clamp(0, indices.length - 1);
return indices[wanted];
}
/// The visit's recorded facts, in the pane's order. A field the server did not
/// record is absent rather than zero, false or a guessed value.
List<VisitFact> detailVisitFacts(JsonMap step) {
final facts = <VisitFact>[];
final status = _text(step['status']);
if (status != null) facts.add((label: 'Status', value: status));
final outcome = _text(step['outcome']);
if (outcome != null) facts.add((label: 'Outcome', value: outcome));
final duration = detailVisitDuration(step);
if (duration != null) facts.add((label: 'Duration', value: duration));
final model = detailVisitProviderModel(step);
if (model != null) facts.add((label: 'Provider/model', value: model));
final target = _text(step['target']);
if (target != null) facts.add((label: 'Target', value: target));
return facts;
}
/// The visit's duration, only when the server recorded both timestamps and the
/// finish is not before the start. One stamp is not a zero-length visit.
String? detailVisitDuration(JsonMap step) {
final seconds = durationSeconds(
_text(step['started_at']),
_text(step['finished_at']),
);
return seconds == null ? null : formatDuration(seconds);
}
/// The effective provider/model this visit ran with, joined as
/// `provider / model` when both were recorded. A visit that recorded neither
/// has no row; one recorded part is shown alone, never completed locally.
String? detailVisitProviderModel(JsonMap step) {
final provider = _text(step['provider']);
final model = _text(step['model']);
if (provider != null && model != null) return '$provider / $model';
return provider ?? model;
}
/// A recorded verdict, or null when the server recorded no option and no
/// reason. An empty recorded verdict is not rendered as a decision.
({String? option, String? reason})? detailVisitVerdict(JsonMap step) {
final verdict = step['verdict'];
if (verdict is! Map) return null;
final option = _text(verdict['option']);
final reason = _text(verdict['reason']);
if (option == null && reason == null) return null;
return (option: option, reason: reason);
}
/// The visit's diagnostic exactly as recorded, or null when it is blank.
String? detailVisitDiagnostic(JsonMap step) => _text(step['diagnostic']);
/// Whether the reply's pinned graph declares [stageKey], so the Definition
/// section shows the authored definition rather than its missing wording.
bool detailVisitHasDefinition(JsonMap? graph, String stageKey) =>
graph != null && stageDefinitionFor(graph, stageKey) != null;
String? _text(dynamic value) {
if (value == null) return null;
final text = '$value'.trim();
return text.isEmpty ? null : text;
}