import '../../../core/api_gateway.dart'; /// The most extra visits one retry may grant (the server accepts 1-5). const maxRetryExtraVisits = 5; /// The diagnostic phrases with which the server reports an exhausted loop. const _exhaustedPhrases = [ 'visit limit reached', 'visit budget exceeded', 'total stage budget exceeded', ]; /// Whether [run] failed because a loop exhausted its visits: its diagnostic /// names a visit limit or budget, or a recorded step's control says the limit /// was reached. Retrying it with extra visits is then worth suggesting. bool runExhaustedVisits(JsonMap run) { final diagnostic = '${run['diagnostic'] ?? ''}'.toLowerCase(); if (_exhaustedPhrases.any(diagnostic.contains)) return true; final steps = run['steps']; if (steps is! List) return false; return steps.whereType().any( (step) => step['control'] is Map && step['control']['limit_reached'] == true, ); } /// The stage keys of the run's graph, in declared order, for the extra /// visits stage picker. List retryStageKeys(JsonMap run) { final graph = run['graph']; final stages = graph is Map ? graph['stages'] : null; if (stages is! List) return const []; final keys = []; for (final stage in stages.whereType()) { final key = '${stage['key'] ?? ''}'.trim(); if (key.isNotEmpty && !keys.contains(key)) keys.add(key); } return keys; }