ebe79ebed0
Run detail gains a Findings tab (open-count badge) listing the server's findings ledger with status and minimum-severity filters and expandable event history; visit grants show as an Extra visits summary fact. The resume retry prompt can grant 1-5 extra visits to the exhausted stage or a chosen graph stage (never sent on restart), suggested when the run failed on a visit limit. A loop-exhaustion decision waiting on a model stage is now decidable from its recorded options, with readable labels, targets and the visit count. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
168 lines
5.0 KiB
Dart
168 lines
5.0 KiB
Dart
import '../../../core/api_gateway.dart';
|
|
import 'run_models.dart';
|
|
|
|
class PendingDecision {
|
|
const PendingDecision({
|
|
required this.runId,
|
|
required this.runLabel,
|
|
required this.stageKey,
|
|
required this.instructions,
|
|
required this.options,
|
|
this.exhausted,
|
|
});
|
|
final num runId;
|
|
final String runLabel;
|
|
final String stageKey;
|
|
final String instructions;
|
|
final List<DecisionOption> options;
|
|
|
|
/// The exhausted loop a server-created decision is about; null for a
|
|
/// declared human stage.
|
|
final ExhaustedLoop? exhausted;
|
|
}
|
|
|
|
/// The loop whose visits ran out, from a waiting step's `exhausted` output:
|
|
/// the model stage, the visit it reached and its limit.
|
|
class ExhaustedLoop {
|
|
const ExhaustedLoop({
|
|
required this.stage,
|
|
required this.visit,
|
|
required this.maxVisits,
|
|
required this.targets,
|
|
});
|
|
|
|
final String stage;
|
|
final int? visit;
|
|
final int? maxVisits;
|
|
|
|
/// Where each option leads, by option key.
|
|
final Map<String, String> targets;
|
|
|
|
static ExhaustedLoop? parse(dynamic output) {
|
|
final value = output is Map ? output['exhausted'] : null;
|
|
if (value is! Map) return null;
|
|
final targets = value['targets'];
|
|
return ExhaustedLoop(
|
|
stage: _string(value['stage']).trim(),
|
|
visit: _int(value['visit']),
|
|
maxVisits: _int(value['max_visits']),
|
|
targets: targets is Map
|
|
? {for (final e in targets.entries) '${e.key}': _string(e.value)}
|
|
: const {},
|
|
);
|
|
}
|
|
}
|
|
|
|
/// Readable labels for the options of a server-created exhaustion decision.
|
|
const exhaustedOptionLabels = {
|
|
'accept': 'Accept as is',
|
|
'continue': 'One more round',
|
|
'abort': 'Abort',
|
|
};
|
|
|
|
JsonMap? lastWaitingStep(List<dynamic> steps) {
|
|
for (final value in steps.reversed) {
|
|
if (value is Map && value['status'] == 'waiting') {
|
|
return Map<String, dynamic>.from(value);
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
String waitingPrompt(dynamic output) =>
|
|
output is Map && output['prompt'] is String
|
|
? output['prompt'] as String
|
|
: '';
|
|
|
|
List<String> waitingOptions(dynamic output) {
|
|
final options = output is Map ? output['options'] : null;
|
|
if (options is! List) return const [];
|
|
return options
|
|
.whereType<String>()
|
|
.where((entry) => entry.trim().isNotEmpty)
|
|
.toList();
|
|
}
|
|
|
|
List<String> humanOptions(dynamic edges) {
|
|
if (edges is! Map) return const [];
|
|
return edges.keys.map((key) => '$key').where((key) => key != 'error').toList()
|
|
..sort();
|
|
}
|
|
|
|
PendingDecision? pendingDecision(JsonMap? run) {
|
|
if (run == null ||
|
|
run['status'] != 'waiting' ||
|
|
_positive(run['export_pending_sequence'])) {
|
|
return null;
|
|
}
|
|
final steps = run['steps'] is List ? run['steps'] as List : const [];
|
|
final step = lastWaitingStep(steps);
|
|
final stage = _string(step?['stage']).trim().isNotEmpty
|
|
? _string(step?['stage'])
|
|
: _string(run['current_stage']);
|
|
if (stage.trim().isEmpty) return null;
|
|
JsonMap? declaration;
|
|
final graph = run['graph'];
|
|
final graphStages = graph is Map ? graph['stages'] : null;
|
|
if (graphStages is List) {
|
|
for (final value in graphStages.whereType<Map>()) {
|
|
if (value['key'] == stage) {
|
|
declaration = Map<String, dynamic>.from(value);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
final output = step?['output'];
|
|
final options = waitingOptions(output);
|
|
// A loop that exhausted its visits waits on its own (model) stage with the
|
|
// options the server recorded; any other non-human stage is not a decision.
|
|
final exhausted = ExhaustedLoop.parse(output);
|
|
final declared = exhausted == null || options.isEmpty;
|
|
if (declared &&
|
|
declaration?['type'] != null &&
|
|
declaration!['type'] != 'human') {
|
|
return null;
|
|
}
|
|
final dynamic edges = exhausted != null && !declared
|
|
? exhausted.targets
|
|
: declaration?['edges'];
|
|
final keys = options.isNotEmpty ? options : humanOptions(edges);
|
|
if (keys.isEmpty) return null;
|
|
final instructions = waitingPrompt(output).trim().isNotEmpty
|
|
? waitingPrompt(output)
|
|
: _string(declaration?['prompt']);
|
|
return PendingDecision(
|
|
runId: run['id'] is num ? run['id'] as num : 0,
|
|
runLabel: runIdentity(run['title'], run['name']).label,
|
|
stageKey: stage,
|
|
instructions: instructions,
|
|
exhausted: declared ? null : exhausted,
|
|
options: keys
|
|
.map(
|
|
(key) => DecisionOption(
|
|
key,
|
|
target: _targetLabel(key, edges is Map ? edges[key] : null),
|
|
label: declared ? '' : exhaustedOptionLabels[key] ?? '',
|
|
),
|
|
)
|
|
.toList(),
|
|
);
|
|
}
|
|
|
|
String _targetLabel(String option, dynamic target) {
|
|
if (target == null || '$target'.trim().isEmpty) return '';
|
|
final resolved = target == '#'
|
|
? (option == 'error' ? '#failure' : '#success')
|
|
: '$target';
|
|
return switch (resolved) {
|
|
'#success' => 'SUCCESS',
|
|
'#failure' => 'FAILURE',
|
|
_ => resolved,
|
|
};
|
|
}
|
|
|
|
bool _positive(dynamic value) => value is num && value > 0;
|
|
String _string(dynamic value) => value == null ? '' : '$value';
|
|
int? _int(dynamic value) =>
|
|
value is num ? value.toInt() : int.tryParse(_string(value).trim());
|