Baida07 commited on
Commit
38877df
Β·
verified Β·
1 Parent(s): cc6873c

fix: restore anti-hallucination claim validation (github a98beb49)

Browse files
Files changed (1) hide show
  1. agents/unified_loop_tools.py +44 -4
agents/unified_loop_tools.py CHANGED
@@ -538,10 +538,50 @@ class DirectToolsMixin:
538
  elif ": errore" in r_str or ": timeout" in r_str:
539
  n_errors += 1
540
  return ("\n\n".join(results), n_called, n_success, n_errors)
541
- def _validate_claims(self, response: str, results_str: str) -> bool:
542
- """S428: Anti-hallucination layer. Valida i claim della risposta contro i dati reali."""
543
- if not results_str or "[ERRORE]" in results_str: return True
544
- return True
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
545
  _TOOL_NEEDED_RE = re.compile(
546
  r"\b(meteo|temperatura|weather|forecast|cerca|search|trova|find|googla|google|"
547
  r"immagine|foto|photo|image|disegna|draw|genera|create|calcola|calculate|math|"
 
538
  elif ": errore" in r_str or ": timeout" in r_str:
539
  n_errors += 1
540
  return ("\n\n".join(results), n_called, n_success, n_errors)
541
+ # ── Claim Validation (S428 Sprint1-Fix3) ─────────────────────────────────
542
+ # A failed live tool must never be represented as a successful live lookup.
543
+ _FALSE_CLAIM_RE = re.compile(
544
+ r"\b(ho\s+trovato(?:\s+che)?|ho\s+recuperato|ho\s+cercato\s+e\s+trovato|"
545
+ r"dai\s+risultati(?:\s+della\s+ricerca)?|stando\s+ai\s+risultati|"
546
+ r"i\s+risultati\s+(?:mostrano|indicano|confermano)|"
547
+ r"la\s+ricerca\s+ha\s+(?:trovato|restituito)|"
548
+ r"secondo\s+i\s+risultati|dalle\s+mie\s+ricerche|"
549
+ r"I\s+found|the\s+results?\s+show|based\s+on\s+(?:the\s+)?results?|"
550
+ r"according\s+to\s+(?:the\s+)?(?:search\s+)?results?)\b",
551
+ re.IGNORECASE,
552
+ )
553
+ _REALTIME_GOAL_RE = re.compile(
554
+ r"\b(notizie|news|ultime\s+notizie|cerca|ricerca\s+web|"
555
+ r"weather|meteo|previsioni|temperatura|"
556
+ r"bitcoin|ethereum|cambio\s+valuta|tasso|crypto|"
557
+ r"versione\s+(?:attuale|corrente|recente)|aggiornamenti\s+su|release)\b",
558
+ re.IGNORECASE,
559
+ )
560
+
561
+ @staticmethod
562
+ def _validate_claims(
563
+ response: str,
564
+ n_success: int,
565
+ n_errors: int,
566
+ goal: str,
567
+ false_claim_re: "re.Pattern[str]",
568
+ realtime_goal_re: "re.Pattern[str]",
569
+ ) -> str:
570
+ """Add transparency when failed live tools are presented as successful."""
571
+ if n_success > 0 or n_errors == 0:
572
+ return response
573
+ if not realtime_goal_re.search(goal):
574
+ return response
575
+ if not false_claim_re.search(response):
576
+ return response
577
+ disclaimer = (
578
+ "\n\n---\n"
579
+ "**Nota tecnica**: i servizi di ricerca in tempo reale non erano "
580
+ "raggiungibili durante questa risposta. Le informazioni sopra provengono "
581
+ "dal mio training e potrebbero non essere aggiornate. "
582
+ "Per dati live consulta una fonte ufficiale."
583
+ )
584
+ return response + disclaimer
585
  _TOOL_NEEDED_RE = re.compile(
586
  r"\b(meteo|temperatura|weather|forecast|cerca|search|trova|find|googla|google|"
587
  r"immagine|foto|photo|image|disegna|draw|genera|create|calcola|calculate|math|"