findScenarioSpan static method

(int, int) findScenarioSpan(
  1. List events,
  2. String contextLabel
)

Public wrapper around the scenario-span finder so audit invariants can reuse the schema's exactly-one-pair strictness instead of replicating loose name-only matching that drifts. Takes a free-form contextLabel (path or capture identifier) for the error message; the schema's own callers pass file.path.

Implementation

static (int, int) findScenarioSpan(List events, String contextLabel) {
  int? beginTs;
  int? endTs;
  var beginCount = 0;
  var endCount = 0;
  for (final event in events) {
    if (event is! Map) continue;
    final ph = event['ph'];
    if (ph != 'i' && ph != 'I' && ph != 'n') continue;
    final name = event['name'];
    if (name is! String) continue;
    final ts = event['ts'];
    if (ts is! num) continue;
    if (name == scenarioBeginMarker) {
      beginTs = ts.toInt();
      beginCount++;
    } else if (name == scenarioEndMarker) {
      endTs = ts.toInt();
      endCount++;
    }
  }
  if (beginCount != 1 || endCount != 1) {
    throw FormatException(
      'Scenario markers malformed in $contextLabel: expected exactly one '
      '"$scenarioBeginMarker" and one "$scenarioEndMarker" instant '
      'event, found beginCount=$beginCount, endCount=$endCount. '
      'Trace-record search needs a unique scenario span.',
    );
  }
  return (beginTs!, endTs!);
}