def should_continue(history: list, current: _ResultProto, confidence_drop_threshold: float = 0.1) -> bool: """Decide whether the iteration loop should run another pass. Three reasons to stop, in order of priority: 1. Candidates are stable (same set as last pass): nothing new will be found. 2. Suggested keywords are stable: the LLM is repeating itself. 3. Confidence is decreasing past the threshold: the loop is making things worse. """ if history: prev = history[-1] if getattr(prev, "candidates", None) == current.candidates: return False if getattr(prev, "suggested_keywords", None) == current.suggested_keywords: return False prev_conf = getattr(prev, "confidence", None) if prev_conf is not None and current.confidence < prev_conf - confidence_drop_threshold: return False return current.needs_iteration()