Commit
·
5e761eb
1
Parent(s):
d04e93b
fix: P0 dedup bug + HF judge infinite loop prevention
Browse filesBug 1 - Semantic Dedup Removing All Evidence:
- Was calling _deduplicate_and_rank(all_evidence) every iteration
- Old items already in vector store found THEMSELVES as duplicates
- Fix: Only dedup NEW evidence, not entire accumulated list
Bug 2 - HF Judge Infinite Loop:
- HF Inference failures returned "continue" with 0% confidence
- Loop never terminated when API kept failing
- Fix: Track consecutive failures, force "synthesize" after 3 failures
Both bugs caused Simple mode (free tier) to loop forever with no output.
Now: Evidence accumulates properly AND loop terminates gracefully.
docs/bugs/P0_ORCHESTRATOR_DEDUP_AND_JUDGE_BUGS.md
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# P0 Bug Report: Orchestrator Dedup + Judge Failures
|
| 2 |
+
|
| 3 |
+
## Status
|
| 4 |
+
- **Date:** 2025-11-29
|
| 5 |
+
- **Priority:** P0 (Blocker - Simple mode broken on HF Spaces)
|
| 6 |
+
- **Component:** `src/orchestrator.py`, `src/agent_factory/judges.py`
|
| 7 |
+
|
| 8 |
+
---
|
| 9 |
+
|
| 10 |
+
## Symptoms
|
| 11 |
+
|
| 12 |
+
When running Simple mode (free tier) on HuggingFace Spaces:
|
| 13 |
+
|
| 14 |
+
1. **Judge always returns 0% confidence** → loops forever with "continue"
|
| 15 |
+
2. **Deduplication removes ALL evidence** after iteration 1
|
| 16 |
+
3. **Never synthesizes** → user sees infinite loop
|
| 17 |
+
|
| 18 |
+
### Example Output
|
| 19 |
+
|
| 20 |
+
```
|
| 21 |
+
📚 SEARCH_COMPLETE: Found 20 new sources (19 total) ← Iteration 1 OK
|
| 22 |
+
✅ JUDGE_COMPLETE: Assessment: continue (confidence: 0%) ← FAIL: 0% = fallback
|
| 23 |
+
|
| 24 |
+
📚 SEARCH_COMPLETE: Found 12 new sources (11 total) ← Iteration 2 BROKEN
|
| 25 |
+
...
|
| 26 |
+
📚 SEARCH_COMPLETE: Found 31 new sources (0 total) ← 0 TOTAL = all removed!
|
| 27 |
+
✅ JUDGE_COMPLETE: Assessment: continue (confidence: 0%) ← Still failing
|
| 28 |
+
```
|
| 29 |
+
|
| 30 |
+
---
|
| 31 |
+
|
| 32 |
+
## Root Cause Analysis
|
| 33 |
+
|
| 34 |
+
### Bug 1: Semantic Deduplication Removes Old Evidence
|
| 35 |
+
|
| 36 |
+
**File:** `src/orchestrator.py:213-219`
|
| 37 |
+
|
| 38 |
+
```python
|
| 39 |
+
# URL dedup (correct)
|
| 40 |
+
seen_urls = {e.citation.url for e in all_evidence}
|
| 41 |
+
unique_new = [e for e in new_evidence if e.citation.url not in seen_urls]
|
| 42 |
+
all_evidence.extend(unique_new)
|
| 43 |
+
|
| 44 |
+
# BUG: Passes ALL evidence (including old) to semantic dedup
|
| 45 |
+
all_evidence = await self._deduplicate_and_rank(all_evidence, query)
|
| 46 |
+
```
|
| 47 |
+
|
| 48 |
+
**Problem:** The `deduplicate()` function checks each item against the vector store. Items from iteration 1 are ALREADY in the store. When re-checked in iteration 2+, they find THEMSELVES (distance ≈ 0) and are removed as "duplicates".
|
| 49 |
+
|
| 50 |
+
**Result:** After iteration 1, evidence count drops to 0.
|
| 51 |
+
|
| 52 |
+
### Bug 2: HF Inference Judge Always Failing
|
| 53 |
+
|
| 54 |
+
**File:** `src/agent_factory/judges.py:186-254`
|
| 55 |
+
|
| 56 |
+
**Evidence:** Judge returns this every time:
|
| 57 |
+
- `confidence: 0.0`
|
| 58 |
+
- `recommendation: "continue"`
|
| 59 |
+
- Next queries are just the original query with suffixes
|
| 60 |
+
|
| 61 |
+
This is the `_create_fallback_assessment()` response, meaning:
|
| 62 |
+
- The HF Inference API calls are failing
|
| 63 |
+
- All 3 fallback models (Llama, Mistral, Zephyr) are failing
|
| 64 |
+
- Likely due to rate limits, quota, or model availability
|
| 65 |
+
|
| 66 |
+
---
|
| 67 |
+
|
| 68 |
+
## The Fix
|
| 69 |
+
|
| 70 |
+
### Fix 1: Only Dedup NEW Evidence (not all_evidence)
|
| 71 |
+
|
| 72 |
+
```python
|
| 73 |
+
# Before (broken)
|
| 74 |
+
all_evidence.extend(unique_new)
|
| 75 |
+
all_evidence = await self._deduplicate_and_rank(all_evidence, query)
|
| 76 |
+
|
| 77 |
+
# After (fixed)
|
| 78 |
+
# Only dedup the NEW evidence against the store
|
| 79 |
+
if unique_new:
|
| 80 |
+
unique_new = await self._deduplicate_new_evidence(unique_new, query)
|
| 81 |
+
all_evidence.extend(unique_new)
|
| 82 |
+
```
|
| 83 |
+
|
| 84 |
+
Or simpler - disable semantic dedup until we fix it properly:
|
| 85 |
+
|
| 86 |
+
```python
|
| 87 |
+
# Disable broken semantic dedup
|
| 88 |
+
# all_evidence = await self._deduplicate_and_rank(all_evidence, query)
|
| 89 |
+
```
|
| 90 |
+
|
| 91 |
+
### Fix 2: Handle HF Inference Failures Gracefully
|
| 92 |
+
|
| 93 |
+
Option A: After N failed judge calls, force synthesize with available evidence
|
| 94 |
+
Option B: Increase retry count or add longer backoff
|
| 95 |
+
Option C: Fall back to MockJudgeHandler (which DOES work) after failures
|
| 96 |
+
|
| 97 |
+
```python
|
| 98 |
+
# In _create_fallback_assessment, track failures
|
| 99 |
+
if self._consecutive_failures >= 3:
|
| 100 |
+
# Force synthesis instead of infinite loop
|
| 101 |
+
return JudgeAssessment(
|
| 102 |
+
sufficient=True, # STOP
|
| 103 |
+
confidence=0.1,
|
| 104 |
+
recommendation="synthesize",
|
| 105 |
+
...
|
| 106 |
+
)
|
| 107 |
+
```
|
| 108 |
+
|
| 109 |
+
---
|
| 110 |
+
|
| 111 |
+
## Test Plan
|
| 112 |
+
|
| 113 |
+
- [ ] Disable semantic dedup OR fix to only process new items
|
| 114 |
+
- [ ] Verify evidence accumulates across iterations (not drops to 0)
|
| 115 |
+
- [ ] Test HF Inference with fresh HF_TOKEN
|
| 116 |
+
- [ ] If HF keeps failing, fall back to MockJudgeHandler
|
| 117 |
+
- [ ] Verify "synthesize" is eventually reached
|
| 118 |
+
- [ ] Deploy and test on HF Space
|
| 119 |
+
|
| 120 |
+
---
|
| 121 |
+
|
| 122 |
+
## Priority Justification
|
| 123 |
+
|
| 124 |
+
**P0** because:
|
| 125 |
+
- Simple mode (free tier) is the DEFAULT experience
|
| 126 |
+
- Currently produces infinite loop with no output
|
| 127 |
+
- Users see "confidence: 0%" and think tool is broken
|
| 128 |
+
- Blocks hackathon demo for users without API keys
|
| 129 |
+
|
| 130 |
+
---
|
| 131 |
+
|
| 132 |
+
## Quick Workaround
|
| 133 |
+
|
| 134 |
+
Disable semantic dedup by setting `enable_embeddings=False` in orchestrator creation:
|
| 135 |
+
|
| 136 |
+
```python
|
| 137 |
+
orchestrator = create_orchestrator(
|
| 138 |
+
...
|
| 139 |
+
enable_embeddings=False, # Disable broken dedup
|
| 140 |
+
)
|
| 141 |
+
```
|
| 142 |
+
|
| 143 |
+
Or users can enter an OpenAI/Anthropic API key to bypass HF Inference issues.
|
src/agent_factory/judges.py
CHANGED
|
@@ -195,6 +195,8 @@ class HFInferenceJudgeHandler:
|
|
| 195 |
"HuggingFaceH4/zephyr-7b-beta", # Fallback (Ungated)
|
| 196 |
]
|
| 197 |
|
|
|
|
|
|
|
| 198 |
def __init__(self, model_id: str | None = None) -> None:
|
| 199 |
"""
|
| 200 |
Initialize with HF Inference client.
|
|
@@ -206,6 +208,7 @@ class HFInferenceJudgeHandler:
|
|
| 206 |
# Will automatically use HF_TOKEN from env if available
|
| 207 |
self.client = InferenceClient()
|
| 208 |
self.call_count = 0
|
|
|
|
| 209 |
self.last_question: str | None = None
|
| 210 |
self.last_evidence: list[Evidence] | None = None
|
| 211 |
|
|
@@ -217,11 +220,22 @@ class HFInferenceJudgeHandler:
|
|
| 217 |
"""
|
| 218 |
Assess evidence using HuggingFace Inference API.
|
| 219 |
Attempts models in order until one succeeds.
|
|
|
|
|
|
|
| 220 |
"""
|
| 221 |
self.call_count += 1
|
| 222 |
self.last_question = question
|
| 223 |
self.last_evidence = evidence
|
| 224 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 225 |
# Format the user prompt
|
| 226 |
if evidence:
|
| 227 |
user_prompt = format_user_prompt(question, evidence)
|
|
@@ -233,7 +247,9 @@ class HFInferenceJudgeHandler:
|
|
| 233 |
|
| 234 |
for model in models_to_try:
|
| 235 |
try:
|
| 236 |
-
|
|
|
|
|
|
|
| 237 |
except Exception as e:
|
| 238 |
# Check for 402/Quota errors to fail fast
|
| 239 |
error_str = str(e)
|
|
@@ -249,8 +265,13 @@ class HFInferenceJudgeHandler:
|
|
| 249 |
last_error = e
|
| 250 |
continue
|
| 251 |
|
| 252 |
-
# All models failed
|
| 253 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 254 |
return self._create_fallback_assessment(question, str(last_error))
|
| 255 |
|
| 256 |
@retry(
|
|
@@ -402,6 +423,41 @@ IMPORTANT: Respond with ONLY valid JSON matching this schema:
|
|
| 402 |
),
|
| 403 |
)
|
| 404 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 405 |
def _create_fallback_assessment(
|
| 406 |
self,
|
| 407 |
question: str,
|
|
|
|
| 195 |
"HuggingFaceH4/zephyr-7b-beta", # Fallback (Ungated)
|
| 196 |
]
|
| 197 |
|
| 198 |
+
MAX_CONSECUTIVE_FAILURES: ClassVar[int] = 3 # Force synthesis after N failures
|
| 199 |
+
|
| 200 |
def __init__(self, model_id: str | None = None) -> None:
|
| 201 |
"""
|
| 202 |
Initialize with HF Inference client.
|
|
|
|
| 208 |
# Will automatically use HF_TOKEN from env if available
|
| 209 |
self.client = InferenceClient()
|
| 210 |
self.call_count = 0
|
| 211 |
+
self.consecutive_failures = 0 # Track failures to prevent infinite loops
|
| 212 |
self.last_question: str | None = None
|
| 213 |
self.last_evidence: list[Evidence] | None = None
|
| 214 |
|
|
|
|
| 220 |
"""
|
| 221 |
Assess evidence using HuggingFace Inference API.
|
| 222 |
Attempts models in order until one succeeds.
|
| 223 |
+
|
| 224 |
+
After MAX_CONSECUTIVE_FAILURES, forces synthesis to prevent infinite loops.
|
| 225 |
"""
|
| 226 |
self.call_count += 1
|
| 227 |
self.last_question = question
|
| 228 |
self.last_evidence = evidence
|
| 229 |
|
| 230 |
+
# BUG FIX: After N consecutive failures, force synthesis to break infinite loop
|
| 231 |
+
if self.consecutive_failures >= self.MAX_CONSECUTIVE_FAILURES:
|
| 232 |
+
logger.warning(
|
| 233 |
+
"Max consecutive failures reached, forcing synthesis",
|
| 234 |
+
failures=self.consecutive_failures,
|
| 235 |
+
evidence_count=len(evidence),
|
| 236 |
+
)
|
| 237 |
+
return self._create_forced_synthesis_assessment(question, evidence)
|
| 238 |
+
|
| 239 |
# Format the user prompt
|
| 240 |
if evidence:
|
| 241 |
user_prompt = format_user_prompt(question, evidence)
|
|
|
|
| 247 |
|
| 248 |
for model in models_to_try:
|
| 249 |
try:
|
| 250 |
+
result = await self._call_with_retry(model, user_prompt, question)
|
| 251 |
+
self.consecutive_failures = 0 # Reset on success
|
| 252 |
+
return result
|
| 253 |
except Exception as e:
|
| 254 |
# Check for 402/Quota errors to fail fast
|
| 255 |
error_str = str(e)
|
|
|
|
| 265 |
last_error = e
|
| 266 |
continue
|
| 267 |
|
| 268 |
+
# All models failed - increment failure counter
|
| 269 |
+
self.consecutive_failures += 1
|
| 270 |
+
logger.error(
|
| 271 |
+
"All HF models failed",
|
| 272 |
+
error=str(last_error),
|
| 273 |
+
consecutive_failures=self.consecutive_failures,
|
| 274 |
+
)
|
| 275 |
return self._create_fallback_assessment(question, str(last_error))
|
| 276 |
|
| 277 |
@retry(
|
|
|
|
| 423 |
),
|
| 424 |
)
|
| 425 |
|
| 426 |
+
def _create_forced_synthesis_assessment(
|
| 427 |
+
self, question: str, evidence: list[Evidence]
|
| 428 |
+
) -> JudgeAssessment:
|
| 429 |
+
"""Force synthesis after repeated failures to prevent infinite loops."""
|
| 430 |
+
findings = _extract_titles_from_evidence(
|
| 431 |
+
evidence,
|
| 432 |
+
max_items=5,
|
| 433 |
+
fallback_message="No findings available (API failures prevented analysis).",
|
| 434 |
+
)
|
| 435 |
+
|
| 436 |
+
return JudgeAssessment(
|
| 437 |
+
details=AssessmentDetails(
|
| 438 |
+
mechanism_score=0,
|
| 439 |
+
mechanism_reasoning="AI analysis unavailable after repeated API failures.",
|
| 440 |
+
clinical_evidence_score=0,
|
| 441 |
+
clinical_reasoning="AI analysis unavailable after repeated API failures.",
|
| 442 |
+
drug_candidates=["AI analysis required for drug identification."],
|
| 443 |
+
key_findings=findings,
|
| 444 |
+
),
|
| 445 |
+
sufficient=True, # FORCE STOP
|
| 446 |
+
confidence=0.1,
|
| 447 |
+
recommendation="synthesize",
|
| 448 |
+
next_search_queries=[],
|
| 449 |
+
reasoning=(
|
| 450 |
+
f"⚠️ **HF Inference Unavailable** ⚠️\n\n"
|
| 451 |
+
f"The free tier AI service failed {self.MAX_CONSECUTIVE_FAILURES} times. "
|
| 452 |
+
f"Search found {len(evidence)} sources (listed below) but they could not "
|
| 453 |
+
"be analyzed by AI.\n\n"
|
| 454 |
+
"**Options:**\n"
|
| 455 |
+
"- Add an OpenAI or Anthropic API key for reliable analysis\n"
|
| 456 |
+
"- Try again later when HF Inference is available\n"
|
| 457 |
+
"- Review the raw search results below"
|
| 458 |
+
),
|
| 459 |
+
)
|
| 460 |
+
|
| 461 |
def _create_fallback_assessment(
|
| 462 |
self,
|
| 463 |
question: str,
|
src/orchestrator.py
CHANGED
|
@@ -213,10 +213,14 @@ class Orchestrator:
|
|
| 213 |
# Deduplicate evidence by URL (fast, basic)
|
| 214 |
seen_urls = {e.citation.url for e in all_evidence}
|
| 215 |
unique_new = [e for e in new_evidence if e.citation.url not in seen_urls]
|
| 216 |
-
all_evidence.extend(unique_new)
|
| 217 |
|
| 218 |
-
#
|
| 219 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 220 |
|
| 221 |
yield AgentEvent(
|
| 222 |
type="search_complete",
|
|
|
|
| 213 |
# Deduplicate evidence by URL (fast, basic)
|
| 214 |
seen_urls = {e.citation.url for e in all_evidence}
|
| 215 |
unique_new = [e for e in new_evidence if e.citation.url not in seen_urls]
|
|
|
|
| 216 |
|
| 217 |
+
# BUG FIX: Only dedup NEW evidence, not all_evidence
|
| 218 |
+
# Old evidence is already in the vector store - re-checking it
|
| 219 |
+
# would mark items as duplicates of themselves (distance ≈ 0)
|
| 220 |
+
if unique_new:
|
| 221 |
+
unique_new = await self._deduplicate_and_rank(unique_new, query)
|
| 222 |
+
|
| 223 |
+
all_evidence.extend(unique_new)
|
| 224 |
|
| 225 |
yield AgentEvent(
|
| 226 |
type="search_complete",
|