Files
pipelines/app/lib/features/runs/domain/run_findings.dart
T
alan ebe79ebed0 App: findings ledger tab, retry extra visits and exhaustion decisions
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>
2026-12-03 12:45:51 +00:00

152 lines
4.5 KiB
Dart

import '../../../core/api_gateway.dart';
/// A finding's severity, most severe first. Unknown wire values parse as null
/// and are kept visible under the "all" severity filter only.
enum FindingSeverity {
critical,
important,
minor,
nit;
static FindingSeverity? parse(dynamic value) {
final text = '${value ?? ''}'.trim().toLowerCase();
for (final severity in values) {
if (severity.name == text) return severity;
}
return null;
}
}
/// The ledger statuses the server records for a finding.
const findingStatuses = ['open', 'fixed', 'dismissed', 'settled'];
/// One recorded status change of a finding.
class FindingEvent {
const FindingEvent({
required this.sequence,
required this.stage,
required this.status,
required this.reason,
});
final int? sequence;
final String stage;
final String status;
final String reason;
}
/// One entry of the run's findings ledger, as the server recorded it.
class RunFinding {
const RunFinding({
required this.id,
required this.severityText,
required this.origin,
required this.location,
required this.summary,
required this.status,
required this.reason,
required this.raisedBy,
required this.raisedSequence,
required this.updatedBy,
required this.updatedSequence,
required this.events,
});
final String id;
/// The severity exactly as recorded; [severity] is its parsed form.
final String severityText;
final String origin;
final String location;
final String summary;
final String status;
final String reason;
final String raisedBy;
final int? raisedSequence;
final String updatedBy;
final int? updatedSequence;
final List<FindingEvent> events;
FindingSeverity? get severity => FindingSeverity.parse(severityText);
bool get isOpen => status == 'open';
}
/// The run's findings ledger; empty when the server sent none (older servers).
List<RunFinding> runFindings(JsonMap? run) {
final list = run?['findings'];
if (list is! List) return const [];
return [
for (final item in list.whereType<Map>())
if (_text(item['id']).isNotEmpty || _text(item['summary']).isNotEmpty)
RunFinding(
id: _text(item['id']),
severityText: _text(item['severity']).toLowerCase(),
origin: _text(item['origin']),
location: _text(item['location']),
summary: _text(item['summary']),
status: _text(item['status']).toLowerCase(),
reason: _text(item['reason']),
raisedBy: _text(item['raised_by']),
raisedSequence: _int(item['raised_sequence']),
updatedBy: _text(item['updated_by']),
updatedSequence: _int(item['updated_sequence']),
events: _events(item['events']),
),
];
}
/// How many findings are still open.
int openFindingCount(Iterable<RunFinding> findings) =>
findings.where((finding) => finding.isOpen).length;
/// The status filter a fresh Findings view starts on: open items when any
/// are open, otherwise everything.
String? defaultFindingStatus(Iterable<RunFinding> findings) =>
openFindingCount(findings) > 0 ? 'open' : null;
/// The findings matching [status] (null = all) at or above [minSeverity]
/// (null = all), in their recorded order.
List<RunFinding> filterFindings(
Iterable<RunFinding> findings, {
String? status,
FindingSeverity? minSeverity,
}) => [
for (final finding in findings)
if ((status == null || finding.status == status) &&
(minSeverity == null ||
(finding.severity != null &&
finding.severity!.index <= minSeverity.index)))
finding,
];
/// The run's extra visit grants as `review +1, implement +2`, or null when
/// none were granted.
String? visitGrantsSummary(JsonMap? run) {
final grants = run?['visit_grants'];
if (grants is! Map) return null;
final parts = [
for (final entry in grants.entries)
if (_int(entry.value) case final count? when count > 0)
'${entry.key} +$count',
];
return parts.isEmpty ? null : parts.join(', ');
}
List<FindingEvent> _events(dynamic value) {
if (value is! List) return const [];
return [
for (final item in value.whereType<Map>())
FindingEvent(
sequence: _int(item['sequence']),
stage: _text(item['stage']),
status: _text(item['status']).toLowerCase(),
reason: _text(item['reason']),
),
];
}
String _text(dynamic value) => value == null ? '' : '$value'.trim();
int? _int(dynamic value) =>
value is num ? value.toInt() : int.tryParse(_text(value));