from __future__ import annotations from typing import Any def estimate_uncertainty(message: str, analysis: dict[str, Any], family_candidates: list[dict[str, Any]] | None = None) -> dict[str, Any]: candidates = [str((item or {}).get('family') or '') for item in (family_candidates or []) if (item or {}).get('family')] confidence = float(analysis.get('confidence', 0.0) or 0.0) score = 0.0 reasons: list[str] = [] if len(set(candidates)) >= 2: score += 0.35 reasons.append('multiple_family_candidates') if confidence < 0.75: score += 0.4 reasons.append('low_confidence') elif confidence < 0.9: score += 0.2 reasons.append('medium_confidence') if analysis.get('needs_memory') and analysis.get('needs_setup_context'): score += 0.15 reasons.append('mixed_memory_and_setup') if 'http' in (message or '').lower() and analysis.get('task_type') not in {'summarize', 'research'}: score += 0.1 reasons.append('url_in_non_research_query') if analysis.get('composition_reason'): score += 0.1 reasons.append('composed_path') if score >= 0.65: level = 'high' elif score >= 0.3: level = 'medium' else: level = 'low' return {'level': level, 'score': round(score, 3), 'reasons': reasons}