Files
pipelines/app/lib/features/graph/domain/run_graph_state.dart
T

141 lines
5.3 KiB
Dart

import 'graph_protocol.dart';
/// Which stages and routes a run actually recorded.
///
/// This is executed-route evidence, deliberately distinct from the graph
/// definition's nodes and edges. It mirrors `runGraphState` from
/// `web/src/runDetail.js` (Android `RunGraphState.from`): every recorded step's
/// stage is visited once in first-appearance order, a step with a non-blank
/// outcome and target contributes one deduplicated `source\0outcome\0target`
/// route, and only a resolved terminal is added by its target. A non-terminal
/// target is not visited until a step lands on it.
///
/// The run map is the same detail produced by the runs feature; this helper
/// reads recorded steps and never folds live events a second time.
class RunGraphState {
RunGraphState({
required List<String> visitedStages,
required List<String> takenRoutes,
required this.currentStage,
required this.executing,
required this.currentStageStartedAt,
Map<String, List<int>> routeOrders = const {},
}) : visitedStages = List.unmodifiable(visitedStages),
takenRoutes = List.unmodifiable(takenRoutes),
_routeOrders = Map.unmodifiable(routeOrders);
final Map<String, List<int>> _routeOrders;
/// Where in the run each pass over a taken route came: 1 for the first
/// route the run took, 2 for the next, and so on; a loop has several.
List<int> routeOrder(String source, String outcome, String target) {
final resolved = resolveTarget(outcome, target);
if (resolved == null || resolved.isEmpty) return const [];
return _routeOrders['$source\u0000$outcome\u0000$resolved'] ?? const [];
}
/// Visited stage keys in first-appearance order, without duplicates.
final List<String> visitedStages;
/// Taken routes as `source\0outcome\0resolvedTarget`, in recorded order.
final List<String> takenRoutes;
/// The run's current stage, else the last recorded visit's stage, else null.
final String? currentStage;
final bool executing;
/// The running visit's start for [currentStage], only while executing.
final String? currentStageStartedAt;
bool isStageVisited(String key) => visitedStages.contains(key);
/// True when a recorded visit took this definition route. The target is
/// resolved with the same `#` rule the definition edges use.
bool isRouteTaken(String source, String outcome, String target) {
final resolved = resolveTarget(outcome, target);
if (resolved == null || resolved.isEmpty) return false;
return takenRoutes.contains('$source\u0000$outcome\u0000$resolved');
}
/// The active stage for a live run, else null. A finished run has none.
String? get activeStage {
if (!executing || currentStage == null || currentStage!.isEmpty) {
return null;
}
return currentStage;
}
}
List<Map<String, dynamic>> _steps(dynamic run) {
if (run is! Map) return const <Map<String, dynamic>>[];
final steps = run['steps'];
if (steps is! List) return const <Map<String, dynamic>>[];
return steps.whereType<Map>().map(Map<String, dynamic>.from).toList();
}
String _text(dynamic value) => '${value ?? ''}';
/// Derives [RunGraphState] from one run detail map.
///
/// Missing or malformed steps yield no visits and no routes; nothing is
/// fabricated from a missing capture. Repeated visits stay one identity per
/// stage, and self edges or loops never recurse.
RunGraphState runGraphState(dynamic run) {
final steps = _steps(run);
final visitedStages = <String>[];
final seenStages = <String>{};
final takenRoutes = <String>[];
final seenRoutes = <String>{};
final routeOrders = <String, List<int>>{};
var ordinal = 0;
void visit(String? key) {
if (key == null || key.isEmpty || !seenStages.add(key)) return;
visitedStages.add(key);
}
for (final step in steps) {
final stage = _text(step['stage']);
if (stage.isNotEmpty) visit(stage);
final outcome = _text(step['outcome']);
final target = _text(step['target']);
if (outcome.trim().isEmpty || target.trim().isEmpty) continue;
final resolved = resolveTarget(outcome, target);
if (resolved == null || resolved.isEmpty) continue;
final route = '$stage\u0000$outcome\u0000$resolved';
if (seenRoutes.add(route)) takenRoutes.add(route);
routeOrders.putIfAbsent(route, () => []).add(++ordinal);
if (isTerminalTarget(resolved)) visit(resolved);
}
final lastStage = steps.isEmpty ? '' : _text(steps.last['stage']);
final rawCurrent = _text(run is Map ? run['current_stage'] : null);
final currentStage = rawCurrent.trim().isNotEmpty
? rawCurrent
: (lastStage.trim().isNotEmpty ? lastStage : null);
final executing = run is Map && run['executing'] == true;
String? currentStageStartedAt;
if (executing && currentStage != null) {
for (var index = steps.length - 1; index >= 0; index -= 1) {
final step = steps[index];
if (_text(step['stage']) != currentStage) continue;
if (_text(step['status']) != 'running') continue;
final started = _text(step['started_at']);
if (started.trim().isNotEmpty) currentStageStartedAt = started;
break;
}
}
return RunGraphState(
visitedStages: visitedStages,
takenRoutes: takenRoutes,
currentStage: currentStage,
executing: executing,
currentStageStartedAt: currentStageStartedAt,
routeOrders: routeOrders,
);
}