Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

feat: Add iterative refinement strategy with confidence progression display

LearningCircuita370e8815217
diff --git a/src/local_deep_research/advanced_search_system/strategies/iterative_refinement_strategy.py b/src/local_deep_research/advanced_search_system/strategies/iterative_refinement_strategy.py
index cbb5d2a4c3..8111962d49 100644
--- a/src/local_deep_research/advanced_search_system/strategies/iterative_refinement_strategy.py
+++ b/src/local_deep_research/advanced_search_system/strategies/iterative_refinement_strategy.py
@@ -96,6 +96,40 @@ class IterativeRefinementStrategy(BaseSearchStrategy):
# Step 1: Run initial research
results = self.initial_strategy.analyze_topic(query)
+ # Validate initial results before proceeding
+ if not results:
+ logger.warning("Initial research returned no results")
+ return {
+ "findings": [],
+ "iterations": 0,
+ "questions_by_iteration": {},
+ "formatted_findings": "Error: Initial research failed to return results.",
+ "current_knowledge": "",
+ "error": "Initial research failed",
+ }
+
+ if "error" in results:
+ logger.error(
+ f"Initial research failed with error: {results['error']}"
+ )
+ return results
+
+ if not results.get("all_links_of_system"):
+ logger.warning(
+ "Initial research returned no sources, skipping refinement"
+ )
+ # Still return the results we have, just without refinement
+ results["refinement_metadata"] = {
+ "strategy": "iterative_refinement",
+ "refinements_performed": 0,
+ "max_refinements": self.max_refinements,
+ "final_confidence": 0.0,
+ "total_sources": 0,
+ "refinement_history": [],
+ "skipped_reason": "No initial sources found",
+ }
+ return results
+
# Build initial research context
research_context = self._build_research_context(results, query)
@@ -167,16 +201,12 @@ class IterativeRefinementStrategy(BaseSearchStrategy):
)
# Step 4: Create follow-up strategy with accumulated context
+ # Reuse the initial strategy directly - it already has all configuration
+ # The EnhancedContextualFollowUpStrategy will handle context properly
followup_strategy = EnhancedContextualFollowUpStrategy(
model=self.model,
search=self.search,
- delegate_strategy=self.initial_strategy.__class__(
- search=self.search,
- model=self.model,
- all_links_of_system=[], # Fresh links for this follow-up
- settings_snapshot=self.settings_snapshot,
- search_original_query=False, # Don't search original in follow-up
- ),
+ delegate_strategy=self.initial_strategy,
all_links_of_system=[], # Fresh for this iteration
settings_snapshot=self.settings_snapshot,
research_context=research_context,
@@ -188,15 +218,32 @@ class IterativeRefinementStrategy(BaseSearchStrategy):
self.progress_callback
)
- # Step 5: Run follow-up research
- followup_results = followup_strategy.analyze_topic(
- followup_query
- )
+ # Step 5: Run follow-up research with error handling
+ try:
+ followup_results = followup_strategy.analyze_topic(
+ followup_query
+ )
- # Step 6: Merge results
- results = self._merge_results(
- results, followup_results, followup_query
- )
+ # Check if follow-up produced useful results
+ new_sources = followup_results.get(
+ "all_links_of_system", []
+ )
+ if len(new_sources) < 2:
+ logger.info(
+ f"Refinement yielded only {len(new_sources)} new sources, stopping refinement"
+ )
+ break
+
+ # Step 6: Merge results
+ results = self._merge_results(
+ results, followup_results, followup_query
+ )
+ except Exception:
+ logger.exception(
+ f"Follow-up research failed for query: {followup_query[:100]}..."
+ )
+ # Re-raise to see the actual error
+ raise
# Update accumulated sources
new_sources = followup_results.get("all_links_of_system", [])
@@ -216,6 +263,50 @@ class IterativeRefinementStrategy(BaseSearchStrategy):
refinement_count += 1
+ # Reorganize formatted_findings to show final result at the top
+ logger.info(
+ f"Checking reorganization: refinement_count={refinement_count}, has separator={'---\\n## Refinement:' in results.get('formatted_findings', '')}"
+ )
+ if refinement_count > 0 and "---\n## Refinement:" in results.get(
+ "formatted_findings", ""
+ ):
+ logger.info(
+ f"Reorganizing findings with {refinement_count} refinements"
+ )
+ # Split to get the last refinement
+ parts = results["formatted_findings"].rsplit(
+ "---\n## Refinement:", 1
+ )
+ if len(parts) == 2:
+ last_refinement = "## Refinement:" + parts[1]
+ # Extract just the content after the query line
+ if "\n" in last_refinement:
+ _, final_content = last_refinement.split("\n", 1)
+
+ # Build confidence progression
+ confidence_progression = []
+ for hist in self.refinement_history:
+ conf = hist["evaluation"].get("confidence", 0)
+ confidence_progression.append(f"{conf:.0%}")
+
+ confidence_str = (
+ " → ".join(confidence_progression)
+ if confidence_progression
+ else "N/A"
+ )
+
+ # Prepend final answer at top
+ results["formatted_findings"] = f"""## Final Answer
+*After {refinement_count} refinement{"s" if refinement_count != 1 else ""} | Confidence progression: {confidence_str}*
+
+{final_content}
+
+---
+
+## Research Progression
+
+{results["formatted_findings"]}"""
+
# Add refinement metadata to results
results["refinement_metadata"] = {
"strategy": "iterative_refinement",
@@ -294,13 +385,19 @@ Consider:
- Specificity: Are there vague areas that need more detail?
- Evidence: Is there sufficient supporting evidence?
+IMPORTANT CONSTRAINTS:
+- Keep refinement query under 50 words
+- Focus on ONE specific gap or aspect
+- Be concrete and specific
+- DO NOT create long, multi-part queries
+
Respond in JSON format:
{{
"action": "REFINE" or "COMPLETE",
"confidence": 0.0 to 1.0 (how confident you are the research is complete),
"gaps": ["list", "of", "identified", "gaps"],
"reasoning": "Brief explanation of your decision",
- "refinement_query": "Specific follow-up question if action is REFINE, otherwise null"
+ "refinement_query": "Specific follow-up question if action is REFINE (MAX 50 WORDS), otherwise null"
}}
Be selective with refinement - only suggest it if it would significantly improve the answer.
@@ -463,9 +560,11 @@ Focus refinement queries on the most critical gaps."""
if merged["questions_by_iteration"]
else 0
)
- for key, questions in followup_results[
- "questions_by_iteration"
- ].items():
+ # Create a list copy of items to avoid dictionary changed size during iteration error
+ followup_items = list(
+ followup_results["questions_by_iteration"].items()
+ )
+ for key, questions in followup_items:
merged["questions_by_iteration"][max_iter + key] = questions
# Merge links (handled in main loop to avoid duplicates)
diff --git a/src/local_deep_research/search_system_factory.py b/src/local_deep_research/search_system_factory.py
index 640375f474..dbcc403ea9 100644
--- a/src/local_deep_research/search_system_factory.py
+++ b/src/local_deep_research/search_system_factory.py
@@ -645,7 +645,9 @@ def create_strategy(
settings_snapshot=settings_snapshot,
evaluation_frequency=kwargs.get("evaluation_frequency", 1),
max_refinements=kwargs.get("max_refinements", 3),
- confidence_threshold=kwargs.get("confidence_threshold", 0.8),
+ confidence_threshold=kwargs.get(
+ "confidence_threshold", 0.95
+ ), # Increased from 0.8
)
# Standard strategy