~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.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
feat: disable relevance filtering in TopicOrganizationStrategy
LearningCircuite4e724e27da5
diff --git a/src/local_deep_research/advanced_search_system/strategies/base_strategy.py b/src/local_deep_research/advanced_search_system/strategies/base_strategy.pyindex 8ed4ee5f66..bf34c5101b 100644--- a/src/local_deep_research/advanced_search_system/strategies/base_strategy.py+++ b/src/local_deep_research/advanced_search_system/strategies/base_strategy.py@@ -27,6 +27,13 @@ class BaseSearchStrategy(ABC):questions_by_iteration: Dictionary of questions by iterationsearch_original_query: Whether to include the original query in the first iteration"""+ # Log strategy initialization+ strategy_name = self.__class__.__name__+ logger.info(f"Initializing strategy: {strategy_name}")+ logger.debug(+ f"Strategy {strategy_name} - search_original_query: {search_original_query}"+ )+self.progress_callback = None# Create a new dict if None is provided (avoiding mutable default argument)self.questions_by_iteration = (diff --git a/src/local_deep_research/advanced_search_system/strategies/focused_iteration_strategy.py b/src/local_deep_research/advanced_search_system/strategies/focused_iteration_strategy.pyindex ae2696dbe1..14d39925cc 100644--- a/src/local_deep_research/advanced_search_system/strategies/focused_iteration_strategy.py+++ b/src/local_deep_research/advanced_search_system/strategies/focused_iteration_strategy.py@@ -78,6 +78,13 @@ class FocusedIterationStrategy(BaseSearchStrategy):)self.use_browsecomp_optimization = use_browsecomp_optimization+ logger.info(+ f"FocusedIterationStrategy configuration - max_iterations: {self.max_iterations}, questions_per_iteration: {self.questions_per_iteration}"+ )+ logger.debug(+ f"FocusedIterationStrategy - use_browsecomp_optimization: {self.use_browsecomp_optimization}"+ )+# Initialize specialized componentsif use_browsecomp_optimization:self.question_generator = BrowseCompQuestionGenerator(self.model)diff --git a/src/local_deep_research/advanced_search_system/strategies/source_based_strategy.py b/src/local_deep_research/advanced_search_system/strategies/source_based_strategy.pyindex bf7f7f7e67..522588a2e0 100644--- a/src/local_deep_research/advanced_search_system/strategies/source_based_strategy.py+++ b/src/local_deep_research/advanced_search_system/strategies/source_based_strategy.py@@ -131,8 +131,15 @@ class SourceBasedSearchStrategy(BaseSearchStrategy):# Determine number of iterations to runiterations_to_run = self.get_setting("search.iterations", 2)- logger.debug("Selected amount of iterations: " + str(iterations_to_run))iterations_to_run = int(iterations_to_run)+ questions_per_iteration = self.get_setting("search.questions", 3)++ logger.info(+ f"SourceBasedStrategy configuration - iterations: {iterations_to_run}, questions_per_iteration: {questions_per_iteration}"+ )+ logger.debug(+ f"SourceBasedStrategy settings - include_text_content: {self.include_text_content}, use_cross_engine_filter: {self.use_cross_engine_filter}"+ )try:filtered_search_results = []total_citation_count_before_this_search = len(diff --git a/src/local_deep_research/advanced_search_system/strategies/topic_organization_strategy.py b/src/local_deep_research/advanced_search_system/strategies/topic_organization_strategy.pyindex b0edb56bda..53f20dbb2c 100644--- a/src/local_deep_research/advanced_search_system/strategies/topic_organization_strategy.py+++ b/src/local_deep_research/advanced_search_system/strategies/topic_organization_strategy.py@@ -5,6 +5,7 @@ from loguru import loggerfrom .base_strategy import BaseSearchStrategyfrom .source_based_strategy import SourceBasedSearchStrategy+from .focused_iteration_strategy import FocusedIterationStrategyfrom ..findings.topic import Topic, TopicGraphfrom ..findings.repository import FindingsRepositoryfrom ...utilities.thread_context import preserve_research_context@@ -31,12 +32,13 @@ class TopicOrganizationStrategy(BaseSearchStrategy):all_links_of_system=None,settings_snapshot=None,search_original_query: bool = True,- min_sources_per_topic: int = 2,+ min_sources_per_topic: int = 1,max_topics: int = 5,similarity_threshold: float = 0.7,enable_refinement: bool = False,max_refinement_iterations: int = 2,generate_text: bool = True,+ use_focused_iteration: bool = False, # New parameter to use FocusedIteration):"""Initialize the topic organization strategy."""super().__init__(@@ -48,6 +50,12 @@ class TopicOrganizationStrategy(BaseSearchStrategy):self.model = modelself.search = search+ # Store cross-engine filter parameters for refinement+ self.use_cross_engine_filter = use_cross_engine_filter+ self.filter_reorder = filter_reorder+ self.filter_reindex = filter_reindex+ self.cross_engine_max_results = cross_engine_max_results+# Create citation handler if not provided (needed for text generation)if citation_handler is None and generate_text:from local_deep_research.citation_handler import CitationHandler@@ -78,22 +86,63 @@ class TopicOrganizationStrategy(BaseSearchStrategy):):return {"content": "", "documents": []}- # Initialize source-based strategy without text generation- # Pass our own all_links_of_system so it accumulates properly- self.source_strategy = SourceBasedSearchStrategy(- search=search,- model=model,- citation_handler=NoTextCitationHandler(), # Use no-op handler to skip text generation- include_text_content=False, # Also disable text content retrieval- use_cross_engine_filter=use_cross_engine_filter,- filter_reorder=filter_reorder,- filter_reindex=filter_reindex,- cross_engine_max_results=cross_engine_max_results,- all_links_of_system=self.all_links_of_system, # Share our list with source strategy- settings_snapshot=settings_snapshot,- search_original_query=search_original_query,+ # Store which strategy we're using+ self.use_focused_iteration = use_focused_iteration++ # Log TopicOrganizationStrategy configuration+ logger.info(+ f"TopicOrganizationStrategy configuration - use_focused_iteration: {use_focused_iteration}, enable_refinement: {enable_refinement}"+ )+ logger.debug(+ f"TopicOrganizationStrategy - min_sources_per_topic: {min_sources_per_topic}, max_topics: {max_topics}, generate_text: {generate_text}")+ # NOTE: Relevance filtering is DISABLED for TopicOrganizationStrategy+ # Testing showed that pre-filtering sources reduced accuracy from 88.9% to 66.7%+ # because it filtered out sources containing correct answers.+ # The topic organization process itself does a good job of selecting relevant sources.+ logger.info(+ "TopicOrganizationStrategy: Relevance filtering is disabled (improves accuracy)"+ )++ # Initialize source gathering strategy based on configuration+ # Pass our own all_links_of_system so it accumulates properly+ # For topic organization, we typically want more sources, so disable cross-engine filter by default+ # unless explicitly enabled+ if use_focused_iteration:+ # Use FocusedIterationStrategy for better factual question performance+ logger.info(+ "TopicOrganizationStrategy using FocusedIterationStrategy for source gathering (optimized for factual questions)"+ )+ self.source_strategy = FocusedIterationStrategy(+ search=search,+ model=model,+ citation_handler=NoTextCitationHandler(), # Use no-op handler to skip text generation+ all_links_of_system=self.all_links_of_system, # Share our list with source strategy+ max_iterations=8, # HARDCODED: Force optimal settings+ questions_per_iteration=5, # HARDCODED: Force optimal settings+ use_browsecomp_optimization=True, # Enable browsecomp optimization for better question generation+ settings_snapshot=settings_snapshot,+ )+ else:+ # Default to SourceBasedSearchStrategy+ logger.info(+ "TopicOrganizationStrategy using SourceBasedSearchStrategy for source gathering (default)"+ )+ self.source_strategy = SourceBasedSearchStrategy(+ search=search,+ model=model,+ citation_handler=NoTextCitationHandler(), # Use no-op handler to skip text generation+ include_text_content=False, # Also disable text content retrieval+ use_cross_engine_filter=False, # Disable cross-engine filter to get more sources for topic organization+ filter_reorder=filter_reorder,+ filter_reindex=filter_reindex,+ cross_engine_max_results=cross_engine_max_results,+ all_links_of_system=self.all_links_of_system, # Share our list with source strategy+ settings_snapshot=settings_snapshot,+ search_original_query=search_original_query,+ )+# Topic graph to store organized topicsself.topic_graph = TopicGraph()@@ -104,162 +153,360 @@ class TopicOrganizationStrategy(BaseSearchStrategy):existing_topics: List[Topic] = None,) -> List[Topic]:"""- Use LLM to identify topics from search results and select lead sources.- Can add new sources to existing topics or create new topics.+ Use LLM to identify topics from search results by iterating through each source."""if not sources:return []+ import uuid+existing_topics = existing_topics or []+ topics = list(existing_topics) # Start with existing topics- # Format existing topics (only show lead sources to LLM)- existing_topics_info = []- if existing_topics:- for i, topic in enumerate(existing_topics):+ # Process all sources - removed artificial limit+ sources_to_process = sources++ # Shuffle sources to avoid ordering bias+ import random++ sources_to_process = (+ sources_to_process.copy()+ ) # Create a copy to avoid modifying original+ random.shuffle(sources_to_process)++ logger.info(+ f"Processing {len(sources_to_process)} sources iteratively for topic organization (shuffled)"+ )+ sources_organized = 0+ sources_deleted = 0+ sources_failed = 0++ for i, source in enumerate(sources_to_process):+ # Log progress periodically (every 5 sources for better feedback)+ if i > 0 and i % 5 == 0:+ progress_msg = f"Processing source {i}/{len(sources_to_process)}: {sources_organized} organized, {sources_deleted} deleted, {sources_failed} failed, {len(topics)} topics"+ logger.log("MILESTONE", progress_msg)+ # Also update the progress callback+ if self.progress_callback:+ progress_percent = 40 + int(+ (i / len(sources_to_process)) * 20+ ) # Progress from 40% to 60%+ self.progress_callback(+ progress_msg,+ progress_percent,+ {"phase": "topic_extraction"},+ )+ logger.info(+ f"Topic organization progress: {i}/{len(sources_to_process)} sources processed, {len(topics)} topics created"+ )++ # Format existing topics for context with source counts+ topics_info = []+ for j, topic in enumerate(topics):lead = topic.lead_source- existing_topics_info.append(- f"Topic {i}: {topic.title}\nLead: {lead.get('title', 'Untitled')}\nSnippet: {lead.get('snippet', '')[:200]}"+ source_count = len(topic.get_all_sources())+ topics_info.append(+ f"Topic {j}: {topic.title} ({source_count} sources)\nLead snippet: {lead.get('snippet', '')}")- # Format sources for LLM analysis- source_texts = []- for i, source in enumerate(- sources[:30]- ): # Limit to prevent context overflow- title = source.get("title", "Untitled")- snippet = source.get("snippet", "")- url = source.get("link", "")- source_texts.append(- f"Source {i + 1}:\nTitle: {title}\nURL: {url}\nSnippet: {snippet[:500]}"+ existing_topics_str = (+ "\n\n".join(topics_info) if topics_info else "No topics yet")- sources_context = "\n\n".join(source_texts)+ # Calculate progress+ sources_remaining = len(sources_to_process) - i - 1- # Unified prompt that works for both initial and refinement iterations- existing_topics_str = (- "\n\n".join(existing_topics_info)- if existing_topics_info- else "None yet"- )+ # Calculate ideal number of topics based on total sources+ # Using square root: if we have N sources, we want roughly sqrt(N) topics with sqrt(N) sources each+ import math- topic_prompt = f"""-Organize search results into topics for the query: "{query}"+ total_sources = len(sources_to_process)+ ideal_topics = int(math.sqrt(total_sources))+ ideal_sources_per_topic = ideal_topics # Same as number of topics+ current_topics = len(topics)-EXISTING TOPICS:-{existing_topics_str}+ # Create prompt for this specific source+ source_prompt = f"""+For the research query: "{query}"-NEW SOURCES TO ORGANIZE:-{sources_context}+PROGRESS: Processing source {i + 1} of {total_sources} ({sources_remaining} remaining)-Group these sources into topics by:-1. Adding sources to existing topics if they match (use existing_topic_index)-2. Creating new topics from sources that form distinct groups+TARGET DISTRIBUTION:+- Total sources to organize: {total_sources}+- Current topics: {current_topics}+- Ideal balance: ~{ideal_topics} topics with ~{ideal_sources_per_topic} sources each+- {"Too many topics - prefer adding to existing ones" if current_topics > ideal_topics else "Can create new topics if needed" if current_topics < ideal_topics else "Good balance - be selective"}-Format your response as a JSON list:-[- {{- "existing_topic_index": -1, // -1 for new topic, 0+ for existing topic- "lead_source_index": 0, // required for new topics- "supporting_source_indices": [1, 2, 3] // sources to add- }}-]+GUIDELINES:+- Aim for balanced distribution: ~{ideal_sources_per_topic} sources per topic+- Prefer adding to existing topics when there's a clear thematic match+- Only create a new topic if the source represents a distinctly different aspect+- Consider: Topics with <{ideal_sources_per_topic // 2} sources need more sources; topics with >{ideal_sources_per_topic * 1.5:.0f} sources are getting full+- Avoid overloading: If a topic has {ideal_sources_per_topic * 2}+ sources, consider if the new source might better start a related subtopic+- Use "d" ONLY for sources that are COMPLETELY UNRELATED (e.g., wrong topic entirely, spam, or error pages). When in doubt, keep the source!-Important:-- Each source should belong to at most one topic-- New topics need at least {self.min_sources_per_topic} sources-- For existing topics, just add supporting sources (no lead needed)-- Select the most informative source as lead for new topics-"""+CURRENT SOURCE TO CATEGORIZE:+Title: {source.get("title", "Untitled")}+URL: {source.get("link", "")}+Snippet: {source.get("snippet", "")}- try:- # Call model directly with prompt- response = self.model.invoke(topic_prompt)- response_text = str(- response.content if hasattr(response, "content") else response- )+EXISTING TOPICS (showing source count):+{existing_topics_str}- # Parse JSON response- import json+Respond with:+- Use "-" to create a new topic+- Use topic number (0-{len(topics) - 1}) to add to existing topic+- Add "+" after the number if this source could be a better lead for that topic+- Use "d" to delete ONLY if source is about a completely different subject (not just low relevance)- # Try to extract JSON from the response- import re+Examples: 0, 2+, -, 1, 3+, d- json_match = re.search(r"\[.*\]", response_text, re.DOTALL)- if json_match:- topic_data = json.loads(json_match.group())- else:- topic_data = json.loads(response_text)+Response:"""- # Start with existing topics or empty list- topics = list(existing_topics) if existing_topics else []- used_indices = set()+ # Debug log the full prompt for the first few sources and periodically+ if i < 3 or i % 50 == 0:+ logger.debug(+ f"Topic organization prompt for source {i + 1}:\n{source_prompt}"+ ) # Log full prompt- for i, topic_info in enumerate(topic_data):- existing_topic_idx = topic_info.get("existing_topic_index", -1)- supporting_indices = topic_info.get(- "supporting_source_indices", []- )+ try:+ response = self.model.invoke(source_prompt)+ response_text = str(+ response.content+ if hasattr(response, "content")+ else response+ ).strip()- if existing_topic_idx >= 0 and existing_topic_idx < len(- existing_topics- ):- # Add sources to existing topic- topic = existing_topics[existing_topic_idx]- for idx in supporting_indices:- if idx not in used_indices and idx < len(sources):- topic.add_supporting_source(sources[idx])- used_indices.add(idx)- logger.info(- f"Added source {idx} to existing topic '{topic.title}'"- )- else:+ # Parse the compact response format+ is_potential_lead = False++ if response_text.lower() == "d":+ # Delete/skip this source as irrelevant+ sources_deleted += 1+ logger.info(+ f"Skipping source {i} '{source.get('title', '')}' - marked as irrelevant"+ )+ continue # Skip to next source+ elif response_text == "-":# Create new topic- lead_idx = topic_info.get("lead_source_index")+ topic_index = -1+ else:+ # Check if ends with + (potential lead)+ is_potential_lead = response_text.endswith("+")++ # Remove the + if present and parse the number+ if is_potential_lead:+ topic_index = int(response_text[:-1])+ else:+ topic_index = int(response_text)++ if topic_index >= 0 and topic_index < len(topics):+ # Add to existing topic+ topics[topic_index].add_supporting_source(source)+ sources_organized += 1+ logger.info(+ f"Added source {i} '{source.get('title', '')}' to topic '{topics[topic_index].title}'"+ )- # Skip if lead source already used or invalid+ # If marked as potential lead, re-evaluate this topic's leadif (- lead_idx in used_indices- or lead_idx >= len(sources)- or lead_idx is None+ is_potential_lead+ and len(topics[topic_index].get_all_sources()) >= 2):- continue-- # Use lead source title as topic title- lead_source = sources[lead_idx]- topic_title = lead_source.get(- "title", f"Topic {len(topics) + 1}"+ logger.debug(+ f"Source {i} marked as potential lead for topic {topic_index}"+ )+ lead_changed = self._reselect_lead_for_single_topic(+ topics[topic_index], topics+ )+ if lead_changed:+ logger.info(+ f"Updated lead source for topic '{topics[topic_index].title}'"+ )+ elif topic_index == -1:+ # Create new topic with this source as lead+ new_topic = Topic(+ id=str(uuid.uuid4()),+ title=source.get("title", f"Topic {len(topics) + 1}"),+ lead_source=source,+ )+ topics.append(new_topic)+ sources_organized += 1+ logger.info(+ f"Created new topic '{new_topic.title}' with source {i} as lead"+ )+ else:+ logger.warning(+ f"Invalid topic index {topic_index} for source {i}")+ sources_failed += 1- # Create topic- topic = Topic(+ except Exception as e:+ logger.warning(+ f"Error processing source {i} '{source.get('title', '')}': {e}. Creating as new topic."+ )+ # On error, create as new topic to ensure all sources are organized+ try:+ new_topic = Topic(id=str(uuid.uuid4()),- title=topic_title,- lead_source=sources[lead_idx],+ title=source.get("title", f"Topic {len(topics) + 1}"),+ lead_source=source,+ )+ topics.append(new_topic)+ sources_organized += 1+ logger.info(+ f"Created recovery topic for source {i} after error"+ )+ except Exception as recovery_error:+ logger.exception(+ f"Failed to create recovery topic for source {i}: {recovery_error}")+ sources_failed += 1- used_indices.add(lead_idx)+ # Count sources in new topics before removing existing topics+ new_topics_count = (+ len(topics) - len(existing_topics)+ if existing_topics+ else len(topics)+ )- # Add supporting sources- for idx in supporting_indices:- if idx not in used_indices and idx < len(sources):- topic.add_supporting_source(sources[idx])- used_indices.add(idx)+ # Remove original existing topics from the result (we only want the newly organized ones)+ if existing_topics:+ topics = topics[len(existing_topics) :]- # Only add new topic if it has enough sources- if (- len(topic.get_all_sources())- >= self.min_sources_per_topic- ):- topics.append(topic)- logger.info(- f"Created new topic '{topic.title}' with {len(topic.get_all_sources())} sources"- )+ # Final summary+ total_processed = sources_organized + sources_deleted + sources_failed+ logger.info(+ f"Topic organization complete: {sources_organized}/{len(sources_to_process)} sources organized into {new_topics_count} new topics, {sources_deleted} deleted, {sources_failed} failed"+ )+ logger.info(+ f"Source accounting: organized={sources_organized}, deleted={sources_deleted}, failed={sources_failed}, total_processed={total_processed}, total_input={len(sources_to_process)}"+ )- return topics+ # Log topic distribution for new topics only+ topic_sizes = [len(topic.get_all_sources()) for topic in topics]+ if topic_sizes:+ avg_size = sum(topic_sizes) / len(topic_sizes)+ logger.info(+ f"New topics distribution: min={min(topic_sizes)}, max={max(topic_sizes)}, avg={avg_size:.1f} sources per topic"+ )- except Exception:- logger.exception("Error extracting topics")- return []+ # Log final milestone+ logger.log(+ "MILESTONE",+ f"Organized {sources_organized} sources into {len(topics)} topics ({sources_deleted} deleted, {sources_failed} failed)",+ )++ # Log any sources that weren't accounted for+ total_accounted = sources_organized + sources_deleted + sources_failed+ if total_accounted != len(sources_to_process):+ logger.error(+ f"SOURCE ACCOUNTING ERROR: {len(sources_to_process) - total_accounted} sources unaccounted for!"+ )+ logger.error(+ f"Input: {len(sources_to_process)}, Organized: {sources_organized}, Deleted: {sources_deleted}, Failed: {sources_failed}"+ )++ return topics++ def _reselect_lead_for_single_topic(+ self, topic: Topic, all_topics: List[Topic]+ ) -> bool:+ """+ Re-evaluate and potentially update the lead source for a single topic.+ Returns True if the lead was changed, False otherwise.+ """+ # Need at least 2 sources to consider changing lead+ if len(topic.get_all_sources()) < 2:+ return False++ # Get context from other topics (just titles for efficiency)+ other_topic_context = []+ for other_topic in all_topics:+ if other_topic.id != topic.id:+ other_topic_context.append(f"- {other_topic.title}")++ # Get all sources from this topic+ all_sources = topic.get_all_sources()+ sources_list = []+ current_lead_idx = 0++ for idx, source in enumerate(all_sources):+ title = source.get("title", "Untitled")+ snippet = source.get("snippet", "")+ url = source.get("url", "")++ # Extract domain from URL+ from urllib.parse import urlparse++ try:+ domain = urlparse(url).netloc if url else ""+ except:+ domain = ""++ # Format source info without restrictions+ source_info = f"{idx}: [{domain}] {title}"+ if snippet:+ source_info += f"\n {snippet}"++ if source == topic.lead_source:+ current_lead_idx = idx+ source_info += " [CURRENT LEAD]"++ sources_list.append(source_info)++ # Create minimal prompt for lead selection+ selection_prompt = f"""+Topic: {topic.title}++Sources in this topic:+{chr(10).join(sources_list)}++Other topics (context):+{chr(10).join(other_topic_context) if other_topic_context else "None"}++Which source number (0-{len(all_sources) - 1}) should be the lead? (most comprehensive/representative)++Response (just the number):"""++ try:+ response = self.model.invoke(selection_prompt)+ response_text = str(+ response.content if hasattr(response, "content") else response+ ).strip()++ # Parse the number+ new_lead_idx = int(response_text)++ if (+ 0 <= new_lead_idx < len(all_sources)+ and new_lead_idx != current_lead_idx+ ):+ new_lead = all_sources[new_lead_idx]+ old_title = topic.lead_source.get("title", "Unknown")+ new_title = new_lead.get("title", "Unknown")++ logger.info(+ f"Updating lead for topic: '{old_title}' -> '{new_title}'"+ )++ topic.update_lead_source(new_lead)+ # Update topic title to match new lead+ topic.title = new_lead.get("title", topic.title)+ return True++ return False++ except (ValueError, IndexError) as e:+ logger.debug(+ f"Could not parse lead selection for topic '{topic.title}': {e}"+ )+ return False+ except Exception as e:+ logger.warning(+ f"Error in lead re-selection for topic '{topic.title}': {e}"+ )+ return Falsedef _find_topic_relationships(self, topics: List[Topic]) -> None:"""@@ -291,7 +538,7 @@ Important:if other_topic.id != topic.id:lead = other_topic.lead_sourceother_topic_leads.append(- f"- {lead.get('title', 'Untitled')}: {lead.get('snippet', '')[:200]}"+ f"- {lead.get('title', 'Untitled')}: {lead.get('snippet', '')}")# 2. Get ALL sources from THIS topic@@ -299,12 +546,12 @@ Important:# Lead sourcelead = topic.lead_sourcetopic_sources.append(- f"LEAD: {lead.get('title', 'Untitled')}\nSnippet: {lead.get('snippet', '')[:300]}"+ f"LEAD: {lead.get('title', 'Untitled')}\nSnippet: {lead.get('snippet', '')}")# Supporting sourcesfor source in topic.supporting_sources:topic_sources.append(- f"- {source.get('title', 'Untitled')}: {source.get('snippet', '')[:200]}"+ f"- {source.get('title', 'Untitled')}: {source.get('snippet', '')}")# 3. Create relevance evaluation prompt@@ -379,14 +626,14 @@ Respond with only "yes" or "no".if i != j:lead = other_topic.lead_sourceother_leads.append(- f"Topic {j + 1}: {lead.get('title', 'Untitled')}\nSnippet: {lead.get('snippet', '')[:150]}"+ f"Topic {j + 1}: {lead.get('title', 'Untitled')}\nSnippet: {lead.get('snippet', '')}")# Get ALL sources from THIS topictopic_sources = []all_sources = topic.get_all_sources()for idx, source in enumerate(all_sources):- source_info = f"Source {idx}:\nTitle: {source.get('title', 'Untitled')}\nSnippet: {source.get('snippet', '')[:200]}"+ source_info = f"Source {idx}:\nTitle: {source.get('title', 'Untitled')}\nSnippet: {source.get('snippet', '')}"if source == topic.lead_source:source_info += " [CURRENT LEAD]"topic_sources.append(source_info)@@ -461,7 +708,7 @@ Respond with only the number.for j, t in enumerate(topics):lead = t.lead_sourceall_leads.append(- f"Topic {j}: {lead.get('title', 'Untitled')}\nSnippet: {lead.get('snippet', '')[:150]}"+ f"Topic {j}: {lead.get('title', 'Untitled')}\nSnippet: {lead.get('snippet', '')}")# Get all supporting sources from THIS topic@@ -471,7 +718,7 @@ Respond with only the number.{"index": idx,"source": source,- "info": f"Source {idx}:\nTitle: {source.get('title', 'Untitled')}\nSnippet: {source.get('snippet', '')[:200]}",+ "info": f"Source {idx}:\nTitle: {source.get('title', 'Untitled')}\nSnippet: {source.get('snippet', '')}",})@@ -629,7 +876,7 @@ Consider similarity to lead sources when deciding.source_count = len(topic.get_all_sources())lead = topic.lead_sourcetopic_summary.append(- f"- {topic.title} ({source_count} sources)\n Lead: {lead.get('snippet', '')[:200]}"+ f"- {topic.title} ({source_count} sources)\n Lead: {lead.get('snippet', '')}")# Format previous questions to avoid repetition@@ -680,7 +927,7 @@ Otherwise, respond with only the follow-up question, nothing else.# Clean up the questionquestion = response_text.strip('"').strip("'").strip()if len(question.split()) > 20: # Sanity check- question = " ".join(question.split()[:15]) + "?"+ question = question # Don't truncate the questionlogger.info(f"Generated refinement question: {question}")self.refinement_questions.append(question)@@ -696,8 +943,13 @@ Otherwise, respond with only the follow-up question, nothing else."""logger.info(f"Starting topic organization for: {query}")+ strategy_name = (+ "focused iteration"+ if self.use_focused_iteration+ else "source-based"+ )self._update_progress(- "Gathering sources using source-based strategy",+ f"Gathering sources using {strategy_name} strategy",10,{"phase": "source_gathering"},)@@ -710,14 +962,38 @@ Otherwise, respond with only the follow-up question, nothing else.source_results = _call_source_strategy(query)+ strategy_name = (+ "FocusedIteration" if self.use_focused_iteration else "Source-based"+ )+ logger.info(+ f"{strategy_name} strategy returned {len(source_results.get('all_links_of_system', []))} total sources"+ )+ logger.info(+ f"{strategy_name} iterations: {source_results.get('iterations', 0)}"+ )++ # Log questions by iteration+ questions = source_results.get("questions_by_iteration", {})+ for iter_num, iter_questions in questions.items():+ logger.info(+ f"Iteration {iter_num}: {len(iter_questions)} questions"+ )+# The source_based_strategy already added sources to its all_links_of_system with proper indices# We need to use those sources which already have sequential indicesall_sources = source_results.get("all_links_of_system", [])+ logger.info(f"Using {len(all_sources)} sources for topic extraction")+# Update our all_links_of_system to match what source_based_strategy collected# This ensures continuity of numberingself.all_links_of_system = source_results.get("all_links_of_system", [])+ # Add indices to all sources in all_links_of_system if they don't have them+ for i, source in enumerate(self.all_links_of_system, 1):+ if "index" not in source:+ source["index"] = str(i)+if not all_sources:logger.warning("No sources found to organize into topics")return {@@ -737,13 +1013,37 @@ Otherwise, respond with only the follow-up question, nothing else.}self._update_progress(- f"Organizing {len(all_sources)} sources into topics",+ f"Starting to organize {len(all_sources)} sources into topics...",40,{"phase": "topic_extraction", "source_count": len(all_sources)},)# Extract topics from sources+ logger.info(+ f"Starting topic extraction with {len(all_sources)} sources"+ )topics = self._extract_topics_from_sources(all_sources, query)+ logger.info(+ f"Extracted {len(topics)} topics from {len(all_sources)} sources"+ )++ # Log topic details+ total_organized = 0+ for topic in topics:+ topic_sources = len(topic.get_all_sources())+ total_organized += topic_sources+ logger.info(f"Topic '{topic.title}' has {topic_sources} sources")++ # Calculate sources lost/deleted during organization+ sources_lost = len(all_sources) - total_organized+ if sources_lost > 0:+ logger.warning(+ f"SOURCE LOSS: {sources_lost} sources not organized ({total_organized}/{len(all_sources)}) - likely deleted as irrelevant"+ )+ else:+ logger.info(+ f"All sources organized successfully: {total_organized} sources in {len(topics)} topics"+ )# Track initial state (not included in LLM context)self.iteration_history.append(@@ -753,6 +1053,10 @@ Otherwise, respond with only the follow-up question, nothing else."question": query,"topics_count": len(topics),"sources_count": len(all_sources),+ "source_based_iterations": source_results.get("iterations", 0),+ "questions_by_iteration": source_results.get(+ "questions_by_iteration", {}+ ),"topics": [{"title": t.title, "sources": len(t.get_all_sources())}for t in topics@@ -817,9 +1121,11 @@ Otherwise, respond with only the follow-up question, nothing else.)# Filter topics by relevance to the original query- relevant_topics = self._filter_topics_by_relevance(- validated_topics, query- )+ # DISABLED: Too aggressive filtering removing valid topics+ # relevant_topics = self._filter_topics_by_relevance(+ # validated_topics, query+ # )+ relevant_topics = validated_topics # Skip relevance filteringlogger.info(f"Filtered from {len(validated_topics)} to {len(relevant_topics)} relevant topics")@@ -858,30 +1164,59 @@ Otherwise, respond with only the follow-up question, nothing else.# Search for more sources using the refinement questioncombined_query = f"{query} {refinement_question}"+ logger.info(+ f"Before refinement: {len(self.all_links_of_system)} sources in all_links_of_system"+ )++ # Create a no-op citation handler for refinement strategy+ class NoTextCitationHandler:+ def analyze_followup(+ self, query, results, previous_knowledge, nr_of_links+ ):+ return {"content": "", "documents": []}++ # Create a NEW source-based strategy with an empty list for isolation+ # This ensures it starts fresh and doesn't interfere with our existing sources+ refinement_source_strategy = SourceBasedSearchStrategy(+ search=self.search,+ model=self.model,+ citation_handler=NoTextCitationHandler(), # Use no-op handler to skip text generation+ include_text_content=False, # Also disable text content retrieval+ use_cross_engine_filter=False, # Keep same settings as main strategy+ filter_reorder=self.filter_reorder,+ filter_reindex=self.filter_reindex,+ cross_engine_max_results=self.cross_engine_max_results,+ all_links_of_system=[], # Start with EMPTY list for isolation+ settings_snapshot=self.settings_snapshot,+ search_original_query=self.search_original_query,+ )+# Wrap in preserve_research_context to maintain context@preserve_research_contextdef _call_refinement_search(q):- return self.source_strategy.analyze_topic(q)+ return refinement_source_strategy.analyze_topic(q)refinement_results = _call_refinement_search(combined_query)- # Get ALL sources from the refinement search (includes previous + new)- all_sources_after_refinement = refinement_results.get(- "all_links_of_system", []- )-- # Extract only the NEW sources added in this refinement- # The new sources are those beyond what we already had- existing_count = len(self.all_links_of_system)- new_sources = (- all_sources_after_refinement[existing_count:]- if len(all_sources_after_refinement) > existing_count- else []+ # Get the new sources from the isolated refinement search+ new_sources = refinement_results.get("all_links_of_system", [])+ logger.info(f"Refinement search found {len(new_sources)} sources")++ # Update indices for new sources to continue from our existing count+ existing_count = len(+ self.all_links_of_system+ ) # Track count before adding new sources+ start_index = existing_count+ for i, source in enumerate(new_sources):+ source["index"] = str(start_index + i + 1)++ # Add ALL sources from refinement to our all_links_of_system+ # The citation handler will handle any duplicates properly+ self.all_links_of_system.extend(new_sources)+ logger.info(+ f"After refinement: {len(self.all_links_of_system)} total sources")- # Update our all_links_of_system to include the new sources- self.all_links_of_system = all_sources_after_refinement-if new_sources:logger.info(f"Found {len(new_sources)} new sources from refinement iteration {len(self.refinement_questions)}"@@ -900,8 +1235,14 @@ Otherwise, respond with only the follow-up question, nothing else."question": refinement_question,"new_sources": len(new_sources),"total_sources": len(self.all_links_of_system),+ "sources_before_refinement": existing_count,+ "sources_after_refinement": len(+ self.all_links_of_system+ ),"topics_before": len(relevant_topics),"topics_after": len(updated_topics),+ "new_topics_added": len(updated_topics)+ - len(relevant_topics),"topics": [{"title": t.title,@@ -909,6 +1250,13 @@ Otherwise, respond with only the follow-up question, nothing else.}for t in updated_topics],+ "source_based_iterations": refinement_results.get(+ "iterations", 0+ ),+ "refinement_found_sources": refinement_results.get(+ "all_links_of_system", []+ )+ is not None,})@@ -929,9 +1277,11 @@ Otherwise, respond with only the follow-up question, nothing else.):validated_refined.append(topic)- relevant_topics = self._filter_topics_by_relevance(- validated_refined, query- )+ # DISABLED: Too aggressive filtering removing valid topics+ # relevant_topics = self._filter_topics_by_relevance(+ # validated_refined, query+ # )+ relevant_topics = validated_refined # Skip relevance filteringlogger.info(f"After refinement iteration {len(self.refinement_questions)}: {len(relevant_topics)} relevant topics")@@ -952,16 +1302,10 @@ Otherwise, respond with only the follow-up question, nothing else.for topic in relevant_topics:topic_sources.extend(topic.get_all_sources())- # IMPORTANT: Update all_links_of_system to only include sources that made it into topics- # This ensures citations only reference sources we actually use- self.all_links_of_system = topic_sources-- # Ensure all sources have both 'link' and 'index' fields for proper formatting- for i, source in enumerate(topic_sources, 1):+ # Ensure all sources have 'link' field for compatibility+ for source in topic_sources:if "url" in source and "link" not in source:source["link"] = source["url"]- # Re-index sources to be sequential for the final output- source["index"] = str(i)# Generate text AFTER re-indexing sourcesif self.generate_text and self.citation_handler:@@ -1079,6 +1423,12 @@ Otherwise, respond with only the follow-up question, nothing else.findings, synthesized_content)+ # Use generated text as current_knowledge if available (for benchmarks/API compatibility)+ # Otherwise fall back to topic graph structure+ current_knowledge = (+ generated_text if generated_text else topic_graph_str+ )+return {# Standard fields expected by the system"findings": findings,@@ -1086,11 +1436,12 @@ Otherwise, respond with only the follow-up question, nothing else.+ len(self.refinement_questions),"questions_by_iteration": questions_by_iteration, # Now includes refinement questions"formatted_findings": formatted_findings,- "current_knowledge": topic_graph_str, # The topic graph structure as knowledge+ "current_knowledge": current_knowledge, # Generated answer or topic graph"all_links_of_system": self.all_links_of_system,# Topic-specific fields"topics": [topic.to_dict() for topic in relevant_topics],"topic_graph": self.topic_graph.to_dict(),+ "topic_graph_str": topic_graph_str, # Keep topic graph available separately"source_count": len(topic_sources),# Iteration history (not included in LLM context, just for display)"iteration_history": self.iteration_history,@@ -1110,7 +1461,7 @@ Otherwise, respond with only the follow-up question, nothing else.# Add snippet from lead source if availableif topic.lead_source.get("snippet"):- lines.append(f" > {topic.lead_source.get('snippet', '')[:300]}...")+ lines.append(f" > {topic.lead_source.get('snippet', '')}")if topic.supporting_sources:lines.append("")@@ -1123,7 +1474,7 @@ Otherwise, respond with only the follow-up question, nothing else.lines.append(f"- [{title}]({link})")# Add snippet for supporting sources tooif source.get("snippet"):- lines.append(f" > {source.get('snippet', '')[:200]}...")+ lines.append(f" > {source.get('snippet', '')}")return "\n".join(lines)@@ -1156,7 +1507,7 @@ Otherwise, respond with only the follow-up question, nothing else.f"[{lead.get('title', 'Untitled')}]({lead.get('link', '')})")if lead.get("snippet"):- lines.append(f"> {lead.get('snippet', '')[:200]}...")+ lines.append(f"> {lead.get('snippet', '')}")# Relationshipsif topic.related_topic_ids:@@ -1212,7 +1563,7 @@ Otherwise, respond with only the follow-up question, nothing else.f"- [{lead.get('title', 'Untitled')}]({lead.get('link', '')})")if lead.get("snippet"):- lines.append(f" > {lead.get('snippet', '')[:300]}...")+ lines.append(f" > {lead.get('snippet', '')}")# All supporting sources with links and snippetsif topic.supporting_sources:@@ -1224,9 +1575,7 @@ Otherwise, respond with only the follow-up question, nothing else.f"- [{source.get('title', 'Untitled')}]({source.get('link', '')})")if source.get("snippet"):- lines.append(- f" > {source.get('snippet', '')[:200]}..."- )+ lines.append(f" > {source.get('snippet', '')}")# Related topicsif topic.related_topic_ids:@@ -1270,52 +1619,15 @@ Otherwise, respond with only the follow-up question, nothing else.) -> str:"""Generate text synthesis from topics using citation handler with custom prompts.- Two-part generation: lead sources synthesis + individual topic paragraphs.+ Two-part generation: individual topic paragraphs first, then summary based on them."""if not topics or not self.citation_handler:return ""generated_texts = []+ topic_sections = [] # Store generated topic sections for summary- # Part 1: Generate comprehensive paragraph from lead sources- lead_sources = []- for topic in topics:- # Sources now have correct sequential indices- lead_sources.append(topic.lead_source)-- # Create documents for citation handler using re-indexed sources- # Pass nr_of_links=0 since we just re-indexed all sources- lead_documents = self.citation_handler._create_documents(- lead_sources, nr_of_links=0- )- formatted_lead_sources = self.citation_handler._format_sources(- lead_documents- )-- lead_prompt = f"""Answer this research question using ONLY the lead sources provided:-QUESTION: {query}--LEAD SOURCES:-{formatted_lead_sources}--Write ONE comprehensive paragraph that synthesizes the main findings from these lead sources.-Focus on directly answering the research question.-Use citations [1], [2], etc. for specific claims.-Make the paragraph substantive and informative."""-- try:- lead_response = self.model.invoke(lead_prompt)- lead_text = (- lead_response.content- if hasattr(lead_response, "content")- else str(lead_response)- )- generated_texts.append(lead_text)- except Exception:- logger.exception("Error generating lead synthesis")- lead_text = ""-- # Part 2: Generate individual topic paragraphs with context awareness+ # Part 1: Generate individual topic paragraphs firstfor i, topic in enumerate(topics):# Get lead sources from OTHER topics for contextother_leads_info = []@@ -1323,10 +1635,10 @@ Make the paragraph substantive and informative."""if i != j:other_lead = other_topic.lead_sourceother_leads_info.append(- f"- Topic {j + 1} ({other_topic.title}): {other_lead.get('snippet', '')[:150]}"+ f"- Topic {j + 1} ({other_topic.title}): {other_lead.get('snippet', '')}")- # Get ALL sources from this topic (now with sequential re-indexed numbers)+ # Get ALL sources from this topictopic_sources = []topic_sources.append(topic.lead_source)topic_sources.extend(topic.supporting_sources)@@ -1340,22 +1652,24 @@ Make the paragraph substantive and informative."""topic_documents)- topic_prompt = f"""RESEARCH QUESTION: {query}--CURRENT TOPIC: {topic.title}-SOURCES FOR THIS TOPIC:+ topic_prompt = f"""CURRENT TOPIC: {topic.title}+SOURCE SNIPPETS FOR THIS TOPIC (partial content):{formatted_topic_sources}OTHER TOPICS BEING COVERED (for context - avoid repeating):{chr(10).join(other_leads_info) if other_leads_info else "None"}-Write a SHORT paragraph (2-3 sentences) that:-1. Focuses on what's UNIQUE about this topic's contribution to answering the research question-2. Avoids repeating information covered by other topics-3. Synthesizes information from all sources in this topic-4. Uses citations for specific claims+Write a SHORT paragraph (2-3 sentences) based on these SNIPPETS that:+1. DIRECTLY ANSWERS aspects of the research question using this topic's sources+2. Focuses on what's UNIQUE about this topic's contribution+3. Works with the partial information available in the snippets+4. Uses citations for claims found in the snippets+5. Acknowledges if key details are not in the snippets but describes what the sources appear to cover++Stay anchored to the research question and highlight this topic's specific insights.+Remember: These are snippets, not full articles.-Stay anchored to the research question and highlight this topic's specific insights."""+RESEARCH QUESTION TO ANSWER: {query}"""try:topic_response = self.model.invoke(topic_prompt)@@ -1365,7 +1679,9 @@ Stay anchored to the research question and highlight this topic's specific insigelse str(topic_response))- # Add topic header+ # Store section for summary generation+ topic_sections.append(f"{topic.title}: {topic_text}")+ # Add topic header for outputgenerated_texts.append(f"\n**{topic.title}:**")generated_texts.append(topic_text)except Exception:@@ -1373,4 +1689,46 @@ Stay anchored to the research question and highlight this topic's specific insigf"Error generating text for topic '{topic.title}'")+ # Part 2: Generate comprehensive summary based on the topic sections+ if topic_sections:+ # Get lead sources for citations+ lead_sources = []+ for topic in topics:+ lead_sources.append(topic.lead_source)++ lead_documents = self.citation_handler._create_documents(+ lead_sources+ )+ formatted_lead_sources = self.citation_handler._format_sources(+ lead_documents+ )++ summary_prompt = f"""SOURCE SNIPPETS (for citation reference):+{formatted_lead_sources}++TOPIC SECTIONS ALREADY GENERATED:+{chr(10).join(topic_sections)}++Based on the topic sections above, write ONE comprehensive introductory paragraph that:+1. DIRECTLY ANSWERS the research question by synthesizing insights from ALL topic sections+2. Focuses on the main answer to the question first+3. Integrates key findings from the different topics into a coherent response+4. Uses citations [1], [2], etc. for specific claims++IMPORTANT: Focus on ANSWERING THE QUESTION directly using the information from the topic sections.++RESEARCH QUESTION TO ANSWER: {query}"""++ try:+ summary_response = self.model.invoke(summary_prompt)+ summary_text = (+ summary_response.content+ if hasattr(summary_response, "content")+ else str(summary_response)+ )+ # Insert summary at the beginning+ generated_texts.insert(0, summary_text)+ except Exception:+ logger.exception("Error generating summary synthesis")+return "\n".join(generated_texts)diff --git a/src/local_deep_research/citation_handlers/base_citation_handler.py b/src/local_deep_research/citation_handlers/base_citation_handler.pyindex f9d16e0959..0fd961196f 100644--- a/src/local_deep_research/citation_handlers/base_citation_handler.py+++ b/src/local_deep_research/citation_handlers/base_citation_handler.py@@ -38,17 +38,21 @@ class BaseCitationHandler(ABC):for i, result in enumerate(search_results):if isinstance(result, dict):- # Add index to the original search result dictionary- result["index"] = str(i + nr_of_links + 1)+ # Add index to the original search result dictionary if it doesn't exist+ # This preserves indices that were already set (e.g., for topic organization)+ if "index" not in result:+ result["index"] = str(i + nr_of_links + 1)content = result.get("full_content", result.get("snippet", ""))+ # Use the index from the result if it exists, otherwise calculate it+ doc_index = int(result.get("index", i + nr_of_links + 1))documents.append(Document(page_content=content,metadata={"source": result.get("link", f"source_{i + 1}"),"title": result.get("title", f"Source {i + 1}"),- "index": i + nr_of_links + 1,+ "index": doc_index,},))diff --git a/src/local_deep_research/config/llm_config.py b/src/local_deep_research/config/llm_config.pyindex 8922888a7c..5a415bab71 100644--- a/src/local_deep_research/config/llm_config.py+++ b/src/local_deep_research/config/llm_config.py@@ -964,38 +964,15 @@ def wrap_llm_without_think_tags(self.base_llm = base_llmdef invoke(self, *args, **kwargs):- # Log detailed request information for Ollama models- if hasattr(self.base_llm, "base_url"):- logger.debug(- f"LLM Request - Base URL: {self.base_llm.base_url}"- )- logger.debug(- f"LLM Request - Model: {getattr(self.base_llm, 'model', 'unknown')}"- )- logger.debug(f"LLM Request - Args count: {len(args)}")-- # Log the prompt if it's in args- if args and len(args) > 0:- prompt_text = (- str(args[0])[:200] + "..."- if len(str(args[0])) > 200- else str(args[0])- )- logger.debug(f"LLM Request - Prompt preview: {prompt_text}")-- # Check if there's any client configuration- if hasattr(self.base_llm, "_client"):- client = self.base_llm._client- if hasattr(client, "_client") and hasattr(- client._client, "base_url"- ):- logger.debug(- f"LLM Request - Client base URL: {client._client.base_url}"- )+ # Removed verbose debug logging to reduce log clutter+ # Uncomment the lines below if you need to debug LLM requests+ # if hasattr(self.base_llm, "base_url"):+ # logger.debug(f"LLM Request - Base URL: {self.base_llm.base_url}")+ # logger.debug(f"LLM Request - Model: {getattr(self.base_llm, 'model', 'unknown')}")try:response = self.base_llm.invoke(*args, **kwargs)- logger.debug(f"LLM Response - Success, type: {type(response)}")+ # logger.debug(f"LLM Response - Success, type: {type(response)}")except Exception as e:logger.exception("LLM Request - Failed with error")# Log any URL information from the errordiff --git a/src/local_deep_research/database/thread_local_session.py b/src/local_deep_research/database/thread_local_session.pyindex b222c3cdd3..922b7d8c01 100644--- a/src/local_deep_research/database/thread_local_session.py+++ b/src/local_deep_research/database/thread_local_session.py@@ -78,9 +78,6 @@ class ThreadLocalSessionManager:with self._lock:self._thread_credentials[thread_id] = (username, password)- logger.info(- f"Thread {thread_id}: Created new session for user {username}"- )return sessiondef get_current_session(self) -> Optional[Session]:diff --git a/src/local_deep_research/database/thread_metrics.py b/src/local_deep_research/database/thread_metrics.pyindex 05eda4cb94..628130146f 100644--- a/src/local_deep_research/database/thread_metrics.py+++ b/src/local_deep_research/database/thread_metrics.py@@ -154,14 +154,6 @@ class ThreadSafeMetricsWriter:)session.add(token_usage)- logger.debug(- f"Saved token metrics for research {research_id}: "- f"{token_data.get('prompt_tokens', 0)} prompt, "- f"{token_data.get('completion_tokens', 0)} completion tokens, "- f"context_limit={token_data.get('context_limit')}, "- f"context_truncated={token_data.get('context_truncated')}"- )-# Global instance for thread-safe metricsmetrics_writer = ThreadSafeMetricsWriter()diff --git a/src/local_deep_research/defaults/default_settings.json b/src/local_deep_research/defaults/default_settings.jsonindex 2fd3102ce7..635d5e4629 100644--- a/src/local_deep_research/defaults/default_settings.json+++ b/src/local_deep_research/defaults/default_settings.json@@ -851,7 +851,7 @@"step": null,"type": "SEARCH","ui_element": "checkbox",- "value": false,+ "value": true,"visible": true},"search.journal_reputation.threshold": {diff --git a/src/local_deep_research/metrics/token_counter.py b/src/local_deep_research/metrics/token_counter.pyindex 7f54360204..454b01755f 100644--- a/src/local_deep_research/metrics/token_counter.py+++ b/src/local_deep_research/metrics/token_counter.py@@ -84,10 +84,6 @@ class TokenCountingCallback(BaseCallbackHandler):# Get context limit from research context (will be set from settings)self.context_limit = self.research_context.get("context_limit")- logger.debug(- f"TokenCounter initialized - context_limit from research_context: {self.context_limit}, "- f"research_context keys: {list(self.research_context.keys()) if self.research_context else 'None'}"- )# Phase 1 Enhancement: Capture call stack informationtry:@@ -144,12 +140,10 @@ class TokenCountingCallback(BaseCallbackHandler):logger.debug(f"Error capturing call stack: {e}")# Continue without call stack info if there's an error- # Debug logging- logger.debug(f"on_llm_start serialized: {serialized}")- # Don't log kwargs directly as it may contain sensitive data- logger.debug(- f"on_llm_start kwargs keys: {list(kwargs.keys()) if kwargs else []}"- )+ # Debug logging removed to reduce log clutter+ # Uncomment below if you need to debug token counting+ # logger.debug(f"on_llm_start serialized: {serialized}")+ # logger.debug(f"on_llm_start kwargs keys: {list(kwargs.keys()) if kwargs else []}")# First, use preset values if availableif self.preset_model:@@ -419,12 +413,6 @@ class TokenCountingCallback(BaseCallbackHandler):else None)- # Debug logging - use info level to ensure it shows- logger.info(- f"Thread metrics save attempt - Thread: {threading.current_thread().name}, "- f"Username: {username}, Research context keys: {list(self.research_context.keys()) if self.research_context else 'None'}"- )-if not username:logger.warning(f"Cannot save token metrics - no username in research context. "@@ -489,11 +477,6 @@ class TokenCountingCallback(BaseCallbackHandler):metrics_writer.write_token_metrics(username, self.research_id, token_data)- logger.debug(- f"Saved token metrics to encrypted DB for user {username} - "- f"research {self.research_id} in thread {threading.current_thread().name}. "- f"Token usage: prompt={prompt_tokens}, completion={completion_tokens}"- )except Exception:logger.exception("Failed to write metrics from thread")returndiff --git a/src/local_deep_research/search_system_factory.py b/src/local_deep_research/search_system_factory.pyindex 09728e15c0..ccdf70b63c 100644--- a/src/local_deep_research/search_system_factory.py+++ b/src/local_deep_research/search_system_factory.py@@ -69,6 +69,8 @@ def create_strategy(model=model,search=search,all_links_of_system=all_links_of_system,+ max_iterations=kwargs.get("max_iterations", 8),+ questions_per_iteration=kwargs.get("questions_per_iteration", 5),settings_snapshot=settings_snapshot,)@@ -630,6 +632,7 @@ def create_strategy(search=search,all_links_of_system=all_links_of_system,settings_snapshot=settings_snapshot,+ min_sources_per_topic=1, # Allow single-source topicsuse_cross_engine_filter=kwargs.get("use_cross_engine_filter", True),filter_reorder=kwargs.get("filter_reorder", True),filter_reindex=kwargs.get("filter_reindex", True),@@ -637,13 +640,15 @@ def create_strategy("cross_engine_max_results", None),search_original_query=kwargs.get("search_original_query", True),- min_sources_per_topic=kwargs.get("min_sources_per_topic", 2),max_topics=kwargs.get("max_topics", 5),similarity_threshold=kwargs.get("similarity_threshold", 0.3),- enable_refinement=kwargs.get("enable_refinement", True),+ use_focused_iteration=True, # HARDCODED TO TRUE for testing - original: kwargs.get("use_focused_iteration", False)+ enable_refinement=kwargs.get(+ "enable_refinement", False+ ), # Disable refinement iterations for nowmax_refinement_iterations=kwargs.get("max_refinement_iterations",- 2, # Reduced from 5 to 2+ 1, # Set to 1 iteration for faster results),generate_text=kwargs.get("generate_text", True),)diff --git a/src/local_deep_research/web/queue/processor.py b/src/local_deep_research/web/queue/processor.pyindex eafe9127a8..2f703f8500 100644--- a/src/local_deep_research/web/queue/processor.py+++ b/src/local_deep_research/web/queue/processor.py@@ -694,10 +694,7 @@ class QueueProcessor:db_session.commit()- logger.info(- f"Saved token metrics for research {research_id}: "- f"{prompt_tokens} prompt, {completion_tokens} completion tokens"- )+ # Debug log removed to reduce clutterexcept Exception:logger.exception(diff --git a/src/local_deep_research/web/templates/pages/research.html b/src/local_deep_research/web/templates/pages/research.htmlindex f2770cb9de..4fd67fb8b9 100644--- a/src/local_deep_research/web/templates/pages/research.html+++ b/src/local_deep_research/web/templates/pages/research.html@@ -130,14 +130,14 @@<!-- Search Iterations --><div class="form-group ldr-half"><label for="iterations">Search Iterations</label>- <input type="number" id="iterations" name="iterations" class="ldr-form-control" min="1" max="5" value="{{ settings.search_iterations }}">+ <input type="number" id="iterations" name="iterations" class="ldr-form-control" min="1" value="{{ settings.search_iterations }}"><span class="ldr-input-help">Number of research cycles to perform</span></div><!-- Questions Per Iteration --><div class="form-group ldr-half"><label for="questions_per_iteration">Questions Per Iteration</label>- <input type="number" id="questions_per_iteration" name="questions_per_iteration" class="ldr-form-control" min="1" max="10" value="{{ settings.search_questions_per_iteration }}">+ <input type="number" id="questions_per_iteration" name="questions_per_iteration" class="ldr-form-control" min="1" value="{{ settings.search_questions_per_iteration }}"><span class="ldr-input-help">Follow-up questions in each cycle</span></div></div>diff --git a/src/local_deep_research/web_search_engines/engines/parallel_search_engine.py b/src/local_deep_research/web_search_engines/engines/parallel_search_engine.pyindex e395b7d714..43051decbe 100644--- a/src/local_deep_research/web_search_engines/engines/parallel_search_engine.py+++ b/src/local_deep_research/web_search_engines/engines/parallel_search_engine.py@@ -72,10 +72,11 @@ class ParallelSearchEngine(BaseSearchEngine):self.search_mode = search_modeself.settings_snapshot = settings_snapshot- # Keep LLM relevance filtering but with a higher limit for parallel search- # since we're aggregating from multiple engines+ # Disable LLM relevance filtering at the parallel level by default+ # Individual engines can still filter their own results+ # Double filtering (engines + parallel) is too aggressiveself.enable_llm_relevance_filter = kwargs.get(- "enable_llm_relevance_filter", True+ "enable_llm_relevance_filter", False)# Cache for engine instances@@ -392,9 +393,11 @@ Select up to {self.max_engines_to_select} engines that would best answer this qu**common_params,)- # Enable filtering at child level to reduce results from each engine- # We'll also filter at parallel level for combined results- if engine:+ # Let individual engines use their default filtering settings+ # We don't force filtering since parallel-level filtering is disabled+ if (+ engine and False+ ): # Disabled: don't force filtering on child enginesengine.enable_llm_relevance_filter = Truelogger.debug(f"Enabled LLM relevance filter for child engine: {engine_name}"diff --git a/src/local_deep_research/web_search_engines/search_engine_base.py b/src/local_deep_research/web_search_engines/search_engine_base.pyindex 16b824f998..06e16be14a 100644--- a/src/local_deep_research/web_search_engines/search_engine_base.py+++ b/src/local_deep_research/web_search_engines/search_engine_base.py@@ -325,6 +325,10 @@ class BaseSearchEngine(ABC):self, "enable_llm_relevance_filter", False)+ logger.info(+ f"BaseSearchEngine: Relevance filter check - enable_llm_relevance_filter={enable_llm_filter}, has_llm={self.llm is not None}, engine_type={type(self).__name__}"+ )+if enable_llm_filter and self.llm:logger.info(f"Applying LLM relevance filter to {len(previews)} previews"@@ -336,11 +340,11 @@ class BaseSearchEngine(ABC):else:filtered_items = previewsif not enable_llm_filter:- logger.debug(- f"LLM relevance filtering disabled - returning all {len(previews)} previews"+ logger.info(+ f"LLM relevance filtering disabled (enable_llm_relevance_filter={enable_llm_filter}) - returning all {len(previews)} previews")elif not self.llm:- logger.debug(+ logger.info(f"No LLM available for relevance filtering - returning all {len(previews)} previews")diff --git a/src/local_deep_research/web_search_engines/search_engine_factory.py b/src/local_deep_research/web_search_engines/search_engine_factory.pyindex 27502d7b01..07f8c527ef 100644--- a/src/local_deep_research/web_search_engines/search_engine_factory.py+++ b/src/local_deep_research/web_search_engines/search_engine_factory.py@@ -181,9 +181,19 @@ def create_search_engine(if "programmatic_mode" in engine_init_params[1:]:filtered_params["programmatic_mode"] = programmatic_mode- # Add LLM if required+ # Add LLM if required OR if provided and engine accepts itif engine_config.get("requires_llm", False):filtered_params["llm"] = llm+ elif (+ "llm" in engine_init_params[1:]+ and llm+ and "llm" not in filtered_params+ ):+ # If LLM was provided and engine accepts it, pass it through+ filtered_params["llm"] = llm+ logger.info(+ f"Passing LLM to {engine_name} (engine accepts it and LLM was provided)"+ )# Add API key if required and not already in filtered_paramsif (@@ -201,6 +211,36 @@ def create_search_engine(# Create the engine instance with filtered parametersengine = engine_class(**filtered_params)+ # Apply skip_relevance_filter setting if present+ # If skip_relevance_filter is False (default), enable LLM relevance filtering+ if (+ settings_snapshot+ and "search.skip_relevance_filter" in settings_snapshot+ ):+ skip_filter_setting = settings_snapshot[+ "search.skip_relevance_filter"+ ]+ skip_filter = (+ skip_filter_setting.get("value", False)+ if isinstance(skip_filter_setting, dict)+ else skip_filter_setting+ )+ # Enable filtering if we should NOT skip it (i.e., skip_filter is False)+ # and if the engine has an LLM+ if not skip_filter and hasattr(engine, "llm") and engine.llm:+ engine.enable_llm_relevance_filter = True+ logger.info(+ f"Enabled LLM relevance filtering for {engine_name} based on settings"+ )+ elif skip_filter:+ logger.debug(+ f"LLM relevance filtering disabled for {engine_name} (skip_relevance_filter=True)"+ )+ elif not hasattr(engine, "llm") or not engine.llm:+ logger.debug(+ f"LLM relevance filtering not available for {engine_name} (no LLM)"+ )+# Check if we need to wrap with full search capabilitiesif kwargs.get("use_full_search", False) and engine_config.get("supports_full_search", False