reflect method
Analyses context and dag and returns an actionable insight, or null.
This is the primary entry point called by AgentLoopController.
Implementation
@override
Future<String?> reflect(AgentContext context, ExecutionDAG dag) async {
_logger.info(
'Self-reflection triggered at iteration ${context.iterationNumber}');
// Update rolling trackers
_updateTrackers(context, dag);
// Run all detectors in priority order
final detection = _detect(context, dag);
if (detection == null) {
_logger.debug('No issues detected — scheduled reflection');
// Still produce a scheduled reflection for learning purposes
final insight = _scheduledReflection(context, dag);
_record(
iteration: context.iterationNumber,
insight: insight,
severity: 0.1,
trigger: ReflectionTrigger.scheduled,
goalId: context.goal.id,
);
return insight;
}
final (trigger, severity, ruleBasedInsight) = detection;
// If LLM is available and severity is high enough, use it for richer output
String insight;
if (_llm != null &&
_config.enableLLMReflection &&
severity >= 0.5) {
insight = await _llmReflection(context, dag, trigger, ruleBasedInsight);
} else {
insight = ruleBasedInsight;
}
_record(
iteration: context.iterationNumber,
insight: insight,
severity: severity,
trigger: trigger,
goalId: context.goal.id,
);
_logger.info(
'Reflection [${trigger.name}, severity: ${severity.toStringAsFixed(2)}]: '
'"${_trunc(insight)}"');
return insight;
}