diagnostics method

String diagnostics()

Returns a diagnostic summary of all reflections so far.

Implementation

String diagnostics() {
  if (_history.isEmpty) return 'No reflections recorded yet.';

  final avgSeverity =
      _history.map((r) => r.severity).reduce((a, b) => a + b) /
          _history.length;

  final triggerCounts = <String, int>{};
  for (final r in _history) {
    triggerCounts[r.triggeredBy.name] =
        (triggerCounts[r.triggeredBy.name] ?? 0) + 1;
  }

  final buf = StringBuffer();
  buf.writeln(
      'SelfReflection: ${_history.length} reflection(s), '
      'avg severity: ${avgSeverity.toStringAsFixed(2)}');
  buf.writeln('Triggers: $triggerCounts');
  if (_history.isNotEmpty) {
    buf.writeln(
        'Last: iter ${_history.last.iteration} — '
        '"${_trunc(_history.last.insight)}"');
  }
  return buf.toString().trimRight();
}