diff --git a/src/agents/orchestrator.py b/src/agents/orchestrator.py index 380404d..8862636 100644 --- a/src/agents/orchestrator.py +++ b/src/agents/orchestrator.py @@ -936,9 +936,32 @@ class AgentOrchestrator: if analysis: sources = analysis.get("sources", []) - sources_json = json.dumps(sources, ensure_ascii=False) if sources else previous_sources_json new_summary = analysis.get("summary", "") or previous_summary + # Validierung: Fehlende Quellennummern im Summary erkennen und reparieren + if sources and new_summary: + import re as _re + referenced_nrs = set(int(m) for m in _re.findall(r'\[(\d+)\]', new_summary)) + defined_nrs = set() + for s in sources: + try: + defined_nrs.add(int(s.get("nr", 0))) + except (ValueError, TypeError): + pass + missing_nrs = sorted(referenced_nrs - defined_nrs) + if missing_nrs: + logger.warning( + "Incident %d: %d Quellennummern im Summary ohne Eintrag in sources: %s", + incident_id, len(missing_nrs), missing_nrs[:20] + ) + # Platzhalter einfuegen damit die Nummern nicht unverlinkt bleiben + for nr in missing_nrs: + sources.append({"nr": nr, "name": "Quelle", "url": ""}) + logger.info("Platzhalter fuer fehlende Quelle [%d] eingefuegt", nr) + sources.sort(key=lambda s: int(s.get("nr", 0)) if isinstance(s.get("nr"), int) or (isinstance(s.get("nr"), str) and str(s.get("nr", "")).isdigit()) else 9999) + + sources_json = json.dumps(sources, ensure_ascii=False) if sources else previous_sources_json + await db.execute( "UPDATE incidents SET summary = ?, sources_json = ?, updated_at = ? WHERE id = ?", (new_summary, sources_json, now, incident_id),