~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
fix: consolidate queue system and fix per-user settings (#1029, #596)
LearningCircuit911f30c5e06d
diff --git a/src/local_deep_research/benchmarks/web_api/benchmark_service.py b/src/local_deep_research/benchmarks/web_api/benchmark_service.pyindex 664996e59b..f238691268 100644--- a/src/local_deep_research/benchmarks/web_api/benchmark_service.py+++ b/src/local_deep_research/benchmarks/web_api/benchmark_service.py@@ -271,10 +271,10 @@ class BenchmarkService:def _run_benchmark_thread(self, benchmark_run_id: int):"""Main benchmark execution thread."""# IMPORTANT: This runs in a background thread, so we cannot access the user database- # Instead, we use the in-memory queue tracker which is thread-safe- from ...database.models.memory_queue import get_memory_queue_tracker-- memory_tracker = get_memory_queue_tracker()+ # Memory queue has been removed as part of queue consolidation (issue #596)+ # Benchmark tracking will need to be updated to use the new queue system+ # For now, we'll skip queue tracking in benchmarks+ memory_tracker = Nonetry:# Get the benchmark data that was passed to us@@ -345,14 +345,17 @@ class BenchmarkService:# Update task metadata in memory tracker# Use a unique task ID to avoid conflictstask_id = f"benchmark_{benchmark_run_id}_{int(datetime.now(UTC).timestamp())}"- memory_tracker.add_task(- task_id=task_id,- username=benchmark_data.get("username", "benchmark_user"),- task_type="benchmark",- )+ # Queue tracking temporarily disabled (memory_queue removed)+ if memory_tracker:+ memory_tracker.add_task(+ task_id=task_id,+ username=benchmark_data.get("username", "benchmark_user"),+ task_type="benchmark",+ )# Update status to processing- memory_tracker.update_task_status(task_id, "processing")+ if memory_tracker:+ memory_tracker.update_task_status(task_id, "processing")# Track progress in memoryprogress_info = {@@ -450,11 +453,13 @@ class BenchmarkService:)if was_cancelled:- memory_tracker.update_task_status(task_id, "cancelled")+ if memory_tracker:+ memory_tracker.update_task_status(task_id, "cancelled")status = BenchmarkStatus.CANCELLEDmessage = "Benchmark cancelled by user"else:- memory_tracker.update_task_status(task_id, "completed")+ if memory_tracker:+ memory_tracker.update_task_status(task_id, "completed")status = BenchmarkStatus.COMPLETEDmessage = "Benchmark completed successfully"@@ -489,7 +494,8 @@ class BenchmarkService:# Update memory trackertry:if "task_id" in locals():- memory_tracker.update_task_status(task_id, "failed")+ if memory_tracker:+ memory_tracker.update_task_status(task_id, "failed")except Exception:logger.exception("Failed to update task status in memory tracker"diff --git a/src/local_deep_research/config/queue_config.py b/src/local_deep_research/config/queue_config.pydeleted file mode 100644index 65892897b2..0000000000--- a/src/local_deep_research/config/queue_config.py+++ /dev/null@@ -1,19 +0,0 @@-"""-Queue configuration settings.-"""--import os--# Queue mode configuration-QUEUE_MODE = os.environ.get(- "LDR_QUEUE_MODE", "direct"-).lower() # "direct" or "queue"--# Maximum concurrent researches per user-MAX_CONCURRENT_PER_USER = int(os.environ.get("LDR_MAX_CONCURRENT", "3"))--# Whether to use queue processor at all-USE_QUEUE_PROCESSOR = QUEUE_MODE == "queue"--# Queue check interval (seconds) - only used if queuing is enabled-QUEUE_CHECK_INTERVAL = int(os.environ.get("LDR_QUEUE_INTERVAL", "10"))diff --git a/src/local_deep_research/database/models/memory_queue.py b/src/local_deep_research/database/models/memory_queue.pydeleted file mode 100644index 05ec3b18f0..0000000000--- a/src/local_deep_research/database/models/memory_queue.py+++ /dev/null@@ -1,285 +0,0 @@-"""-In-memory only queue tracking models.-These models are NOT persisted to disk and reset on application restart.-Used for tracking transient queue status without storing PII in unencrypted databases.-"""--from datetime import datetime, UTC-from functools import partial-from typing import Dict, Optional--from sqlalchemy import Column, DateTime, Integer, String, create_engine-from sqlalchemy.ext.declarative import declarative_base-from sqlalchemy.orm import Session, sessionmaker-from sqlalchemy.pool import StaticPool--from loguru import logger--Base = declarative_base()---class InMemoryQueueStatus(Base):- """- Track queue status for each user IN MEMORY ONLY.- This data is transient and will be lost on application restart.- """-- __tablename__ = "memory_queue_status"-- username = Column(String, primary_key=True)- active_tasks = Column(Integer, default=0)- queued_tasks = Column(Integer, default=0)- last_checked = Column(DateTime, default=partial(datetime.now, UTC))- last_task_id = Column(String, nullable=True)---class InMemoryTaskMetadata(Base):- """- Store task metadata for background processing IN MEMORY ONLY.- This data is transient and will be lost on application restart.- """-- __tablename__ = "memory_task_metadata"-- task_id = Column(String, primary_key=True)- username = Column(String, nullable=False, index=True)- status = Column(- String, nullable=False- ) # queued, processing, completed, failed- task_type = Column(String, nullable=False) # research, benchmark, etc- created_at = Column(DateTime, default=partial(datetime.now, UTC))- started_at = Column(DateTime, nullable=True)- completed_at = Column(DateTime, nullable=True)- # Note: We don't store error messages to avoid PII leakage---class InMemoryQueueTracker:- """- Manages in-memory only queue tracking.- All data is transient and resets on application restart.- """-- def __init__(self):- """Initialize in-memory SQLite database."""- # Create in-memory SQLite database- # Using StaticPool to ensure thread safety with a single connection- self.engine = create_engine(- "sqlite:///:memory:",- connect_args={"check_same_thread": False},- poolclass=StaticPool,- echo=False,- )-- # Create tables- Base.metadata.create_all(self.engine)-- # Create session factory- self.SessionLocal = sessionmaker(bind=self.engine)-- logger.info("In-memory queue tracker initialized (data is transient)")-- def get_session(self) -> Session:- """Get a new database session."""- return self.SessionLocal()-- def update_queue_status(- self, username: str, active_tasks: int, queued_tasks: int- ) -> None:- """Update queue status for a user."""- with self.get_session() as session:- status = (- session.query(InMemoryQueueStatus)- .filter_by(username=username)- .first()- )-- if status:- status.active_tasks = active_tasks- status.queued_tasks = queued_tasks- status.last_checked = datetime.now(UTC)- else:- status = InMemoryQueueStatus(- username=username,- active_tasks=active_tasks,- queued_tasks=queued_tasks,- )- session.add(status)-- session.commit()-- def get_queue_status(self, username: str) -> Optional[Dict[str, any]]:- """Get queue status for a user."""- with self.get_session() as session:- status = (- session.query(InMemoryQueueStatus)- .filter_by(username=username)- .first()- )-- if status:- return {- "active_tasks": status.active_tasks,- "queued_tasks": status.queued_tasks,- "last_checked": status.last_checked,- "last_task_id": status.last_task_id,- }- return None-- def add_task(- self,- task_id: str,- username: str,- task_type: str,- ) -> None:- """Add a new task to tracking."""- with self.get_session() as session:- task = InMemoryTaskMetadata(- task_id=task_id,- username=username,- status="queued",- task_type=task_type,- )- session.add(task)-- # Update queue status- status = (- session.query(InMemoryQueueStatus)- .filter_by(username=username)- .first()- )- if status:- status.queued_tasks += 1- status.last_task_id = task_id- else:- status = InMemoryQueueStatus(- username=username, queued_tasks=1, last_task_id=task_id- )- session.add(status)-- session.commit()-- def update_task_status(self, task_id: str, status: str) -> None:- """Update task status."""- with self.get_session() as session:- task = (- session.query(InMemoryTaskMetadata)- .filter_by(task_id=task_id)- .first()- )-- if task:- old_status = task.status- task.status = status-- if status == "processing" and old_status == "queued":- task.started_at = datetime.now(UTC)- # Update queue counts- queue_status = (- session.query(InMemoryQueueStatus)- .filter_by(username=task.username)- .first()- )- if queue_status:- queue_status.queued_tasks = max(- 0, queue_status.queued_tasks - 1- )- queue_status.active_tasks += 1-- elif status in ["completed", "failed"]:- task.completed_at = datetime.now(UTC)- # Update queue counts- queue_status = (- session.query(InMemoryQueueStatus)- .filter_by(username=task.username)- .first()- )- if queue_status:- queue_status.active_tasks = max(- 0, queue_status.active_tasks - 1- )-- session.commit()-- def get_user_tasks(- self, username: str, status: Optional[str] = None- ) -> list:- """Get tasks for a user, optionally filtered by status."""- with self.get_session() as session:- query = session.query(InMemoryTaskMetadata).filter_by(- username=username- )-- if status:- query = query.filter_by(status=status)-- tasks = query.order_by(InMemoryTaskMetadata.created_at).all()-- return [- {- "task_id": t.task_id,- "status": t.status,- "task_type": t.task_type,- "created_at": t.created_at,- "started_at": t.started_at,- "completed_at": t.completed_at,- }- for t in tasks- ]-- def cleanup_completed_tasks(self, username: Optional[str] = None) -> int:- """- Remove completed/failed tasks from memory.- Useful to prevent memory growth over time.- """- with self.get_session() as session:- query = session.query(InMemoryTaskMetadata).filter(- InMemoryTaskMetadata.status.in_(["completed", "failed"])- )-- if username:- query = query.filter_by(username=username)-- deleted = query.delete()- session.commit()-- if deleted > 0:- logger.debug(- f"Cleaned up {deleted} completed tasks from memory"- )-- return deleted-- def reset_user_queues(self, username: str) -> None:- """Reset all queue data for a user (useful for testing or cleanup)."""- with self.get_session() as session:- # Delete all tasks for user- session.query(InMemoryTaskMetadata).filter_by(- username=username- ).delete()-- # Reset or delete queue status- status = (- session.query(InMemoryQueueStatus)- .filter_by(username=username)- .first()- )- if status:- status.active_tasks = 0- status.queued_tasks = 0- status.last_task_id = None- status.last_checked = datetime.now(UTC)-- session.commit()- logger.info(f"Reset queue data for user: {username}")---# Global instance - this is in-memory only and will reset on restart-_memory_queue_tracker = None---def get_memory_queue_tracker() -> InMemoryQueueTracker:- """Get global in-memory queue tracker instance."""- global _memory_queue_tracker- if _memory_queue_tracker is None:- _memory_queue_tracker = InMemoryQueueTracker()- return _memory_queue_trackerdiff --git a/src/local_deep_research/web/app_factory.py b/src/local_deep_research/web/app_factory.pyindex 8e60303fdf..70f894532a 100644--- a/src/local_deep_research/web/app_factory.py+++ b/src/local_deep_research/web/app_factory.py@@ -236,16 +236,12 @@ def create_app():logger.info("Error handlers registered successfully")# Start the queue processor v2 (uses encrypted databases)- from ..config.queue_config import USE_QUEUE_PROCESSOR+ # Always start the processor - it will handle per-user queue modes+ logger.info("Starting queue processor v2...")+ from .queue.processor_v2 import queue_processor- if USE_QUEUE_PROCESSOR:- logger.info("Starting queue processor v2...")- from .queue.processor_v2 import queue_processor-- queue_processor.start()- logger.info("Started research queue processor v2")- else:- logger.info("Queue processor disabled - using direct mode")+ queue_processor.start()+ logger.info("Started research queue processor v2")logger.info("App factory completed successfully")diff --git a/src/local_deep_research/web/auth/queue_middleware.py b/src/local_deep_research/web/auth/queue_middleware.pyindex fc3865a9b4..e2f12d31c5 100644--- a/src/local_deep_research/web/auth/queue_middleware.py+++ b/src/local_deep_research/web/auth/queue_middleware.py@@ -7,7 +7,7 @@ from loguru import loggerfrom ...database.encrypted_db import db_managerfrom ...database.session_context import get_user_db_session-from ..queue.processor import queue_processor+from ..queue.processor_v2 import queue_processordef process_pending_queue_operations():diff --git a/src/local_deep_research/web/queue/__init__.py b/src/local_deep_research/web/queue/__init__.pyindex be79a0282e..e32eff2db5 100644--- a/src/local_deep_research/web/queue/__init__.py+++ b/src/local_deep_research/web/queue/__init__.py@@ -1,6 +1,6 @@"""Queue management for research tasks"""from .manager import QueueManager-from .processor import QueueProcessor+from .processor_v2 import QueueProcessorV2-__all__ = ["QueueManager", "QueueProcessor"]+__all__ = ["QueueManager", "QueueProcessorV2"]diff --git a/src/local_deep_research/web/queue/processor.py b/src/local_deep_research/web/queue/processor.pydeleted file mode 100644index 2f703f8500..0000000000--- a/src/local_deep_research/web/queue/processor.py+++ /dev/null@@ -1,707 +0,0 @@-"""Service for managing the research queue"""--import threading-import time-import uuid-from typing import Any, Dict, Optional--from loguru import logger--from ...database.models import (- QueuedResearch,- ResearchHistory,- UserActiveResearch,-)-from ...database.models.memory_queue import get_memory_queue_tracker-from ..routes.globals import active_research, termination_flags-from ..services.research_service import (- run_research_process,- start_research_process,-)---class QueueProcessor:- """Processes queued researches when slots become available"""-- def __init__(self, check_interval=5):- """- Initialize the queue processor-- Args:- check_interval: How often to check for available slots (seconds)- """- self.check_interval = check_interval- self.running = False- self.thread = None- self.max_concurrent_per_user = 3- self.pending_operations = {} # Store operations that need DB access-- def start(self):- """Start the queue processor thread"""- if self.running:- logger.warning("Queue processor already running")- return-- self.running = True- self.thread = threading.Thread(- target=self._process_queue_loop, daemon=True- )- self.thread.start()- logger.info("Queue processor started")-- def stop(self):- """Stop the queue processor thread"""- self.running = False- if self.thread:- self.thread.join(timeout=10)- logger.info("Queue processor stopped")-- def _process_queue_loop(self):- """Main loop that processes the queue"""- while self.running:- try:- self._check_and_process_queue()- except Exception:- logger.exception("Error in queue processor loop")-- time.sleep(self.check_interval)-- def _check_and_process_queue(self):- """Check for available slots and process queued researches"""- # Get memory tracker- memory_tracker = get_memory_queue_tracker()-- # Get all users with queued tasks from memory tracker- users = set()- with memory_tracker.get_session() as session:- from ...database.models.memory_queue import InMemoryTaskMetadata-- usernames = (- session.query(InMemoryTaskMetadata.username)- .filter(InMemoryTaskMetadata.status == "queued")- .distinct()- .all()- )- users = [(username,) for (username,) in usernames]-- for (username,) in users:- try:- self._process_user_queue(username)- except Exception:- logger.exception(f"Error processing queue for user {username}")-- def _process_user_queue(self, username: str):- """Process the queue for a specific user using memory tracker"""- # Get memory tracker- memory_tracker = get_memory_queue_tracker()-- # Get queue status from memory tracker- queue_status = memory_tracker.get_queue_status(username) or {- "active_tasks": 0,- "queued_tasks": 0,- }-- # Calculate available slots- available_slots = (- self.max_concurrent_per_user - queue_status["active_tasks"]- )-- if available_slots <= 0:- return-- # Get pending tasks from memory tracker- pending_tasks = memory_tracker.get_user_tasks(- username, status="queued"- )[:available_slots]-- # For tasks requiring DB access, we need to use a different approach- if queue_status["queued_tasks"] > len(pending_tasks):- # There are tasks that require DB access- # These will be processed when the user makes a request (event-driven)- self._register_pending_db_operation(username, available_slots)-- def _register_pending_db_operation(- self, username: str, available_slots: int- ):- """Register that we need to access user's encrypted database"""- operation_id = str(uuid.uuid4())- self.pending_operations[operation_id] = {- "username": username,- "available_slots": available_slots,- "timestamp": time.time(),- }-- # This will be processed when user makes a request- logger.debug(- f"Registered pending operation {operation_id} for {username}"- )-- def process_pending_operations_for_user(- self, username: str, db_session- ) -> int:- """- Process pending operations for a user when we have database access.- Called from request context where encrypted database is accessible.-- Returns number of researches started.- """- started_count = 0-- # Find pending operations for this user- operations_to_process = []- for op_id, op_data in list(self.pending_operations.items()):- if op_data["username"] == username:- operations_to_process.append((op_id, op_data))-- if not operations_to_process:- return 0-- for op_id, op_data in operations_to_process:- try:- operation_type = op_data.get("operation_type", "queue_process")-- if operation_type == "queue_process":- # Process queued researches with database access- count = self._process_queued_researches_with_db(- db_session, username, op_data["available_slots"]- )- started_count += count-- elif operation_type == "cleanup":- # Process cleanup operation- self._process_cleanup_operation(db_session, op_data)-- elif operation_type == "error_update":- # Process error update- self._process_error_update(db_session, op_data)-- elif operation_type == "progress_update":- # Process progress update- self._process_progress_update(db_session, op_data)-- elif operation_type == "token_metrics":- # Process token metrics- self._process_token_metrics(db_session, op_data)-- # Remove processed operation- del self.pending_operations[op_id]-- except Exception:- logger.exception(f"Error processing operation {op_id}")-- return started_count-- def _process_queued_researches_with_db(- self, session, username: str, available_slots: int- ) -> int:- """Process queued researches when we have database access"""- started_count = 0-- try:- # Get queued researches ordered by position- queued = (- session.query(QueuedResearch)- .filter_by(username=username, is_processing=False)- .order_by(QueuedResearch.position)- .limit(available_slots)- .all()- )-- for queued_research in queued:- try:- # Mark as processing to avoid race conditions- queued_research.is_processing = True- session.commit()-- # Start the research- self._start_queued_research(- session, username, queued_research- )-- # Remove from queue- session.delete(queued_research)- session.commit()-- # Update memory tracker- memory_tracker = get_memory_queue_tracker()- memory_tracker.update_task_status(- queued_research.research_id, "processing"- )-- logger.info(- f"Started queued research {queued_research.research_id} for user {username}"- )- started_count += 1-- except Exception:- logger.exception(- f"Error starting queued research {queued_research.research_id}"- )- # Reset processing flag on error- queued_research.is_processing = False- session.commit()-- # Update memory tracker- memory_tracker = get_memory_queue_tracker()- memory_tracker.update_task_status(- queued_research.research_id,- "failed",- )-- except Exception:- logger.exception(- f"Error accessing encrypted database for {username}"- )-- return started_count-- def _start_queued_research(self, session, username, queued_research):- """Start a queued research"""- # Update research status- research = (- session.query(ResearchHistory)- .filter_by(id=queued_research.research_id)- .first()- )-- if not research:- logger.error(- f"Research {queued_research.research_id} not found in history"- )- return-- research.status = "in_progress"- session.commit()-- # Create active research record- active_record = UserActiveResearch(- username=username,- research_id=queued_research.research_id,- status="in_progress",- thread_id="pending", # Will be updated when thread starts- settings_snapshot=queued_research.settings_snapshot,- )- session.add(active_record)- session.commit()-- # Extract settings- settings_snapshot = queued_research.settings_snapshot or {}-- # Extract submission parameters from the new structure- submission_params = {}- if isinstance(settings_snapshot, dict):- # Check if it's the new structure with 'submission' key- if "submission" in settings_snapshot:- submission_params = settings_snapshot.get("submission", {})- complete_settings = settings_snapshot.get(- "settings_snapshot", {}- )- else:- # Legacy structure - use the whole object as submission params- submission_params = settings_snapshot- complete_settings = {}-- # Start the research process- research_thread = start_research_process(- queued_research.research_id,- queued_research.query,- queued_research.mode,- active_research,- termination_flags,- run_research_process,- username=username,- model_provider=submission_params.get("model_provider"),- model=submission_params.get("model"),- custom_endpoint=submission_params.get("custom_endpoint"),- search_engine=submission_params.get("search_engine"),- max_results=submission_params.get("max_results"),- time_period=submission_params.get("time_period"),- iterations=submission_params.get("iterations"),- questions_per_iteration=submission_params.get(- "questions_per_iteration"- ),- strategy=submission_params.get("strategy", "source-based"),- settings_snapshot=complete_settings, # Pass complete settings- )-- # Update thread ID- active_record.thread_id = str(research_thread.ident)- session.commit()-- logger.info(- f"Research {queued_research.research_id} started from queue"- )-- def notify_research_queued(self, username: str, research_id: str):- """- Notify the queue processor that a new research was queued.- Updates the memory tracker.- """- memory_tracker = get_memory_queue_tracker()- memory_tracker.add_task(- task_id=research_id,- username=username,- task_type="research",- )-- def notify_research_completed(self, username: str, research_id: str):- """- Notify the queue processor that a research completed.- Updates the memory tracker.- """- memory_tracker = get_memory_queue_tracker()- memory_tracker.update_task_status(research_id, "completed")-- def queue_cleanup_operation(- self, username: str, research_id: str, operation_type: str = "cleanup"- ):- """- Queue a cleanup operation that needs database access.- This will be processed when the user makes their next request.-- Args:- username: The username- research_id: The research ID- operation_type: Type of operation (cleanup, error_update, etc.)- """- operation_id = str(uuid.uuid4())- self.pending_operations[operation_id] = {- "username": username,- "operation_type": operation_type,- "research_id": research_id,- "timestamp": time.time(),- }- logger.info(- f"Queued {operation_type} operation for research {research_id}"- )-- def queue_error_update(- self,- username: str,- research_id: str,- status: str,- error_message: str,- metadata: Dict[str, Any],- completed_at: str,- report_path: Optional[str] = None,- ):- """- Queue an error status update that needs database access.-- Args:- username: The username- research_id: The research ID- status: The status to set (failed, suspended, etc.)- error_message: The error message- metadata: Research metadata- completed_at: Completion timestamp- report_path: Optional path to error report- """- operation_id = str(uuid.uuid4())- self.pending_operations[operation_id] = {- "username": username,- "operation_type": "error_update",- "research_id": research_id,- "status": status,- "error_message": error_message,- "metadata": metadata,- "completed_at": completed_at,- "report_path": report_path,- "timestamp": time.time(),- }- logger.info(- f"Queued error update for research {research_id} with status {status}"- )-- def queue_progress_update(- self, username: str, research_id: str, progress: float- ):- """- Queue a progress update that needs database access.-- Args:- username: The username- research_id: The research ID- progress: The progress value (0-100)- """- operation_id = str(uuid.uuid4())- self.pending_operations[operation_id] = {- "username": username,- "operation_type": "progress_update",- "research_id": research_id,- "progress": progress,- "timestamp": time.time(),- }- logger.debug(- f"Queued progress update for research {research_id}: {progress}%"- )-- def queue_token_metrics(- self,- username: str,- research_id: Optional[int],- token_data: Dict[str, Any],- ):- """- Queue token metrics that need database access.-- Args:- username: The username- research_id: The research ID (optional)- token_data: Dictionary containing all token metric data- """- operation_id = str(uuid.uuid4())- self.pending_operations[operation_id] = {- "username": username,- "operation_type": "token_metrics",- "research_id": research_id,- "token_data": token_data,- "timestamp": time.time(),- }- logger.debug(- f"Queued token metrics for research {research_id} - "- f"tokens: {token_data.get('prompt_tokens', 0)} prompt, "- f"{token_data.get('completion_tokens', 0)} completion"- )-- def _process_cleanup_operation(self, db_session, op_data: Dict[str, Any]):- """- Process a cleanup operation with database access.-- Args:- db_session: The database session- op_data: The operation data- """- research_id = op_data["research_id"]- username = op_data["username"]-- try:- # Get the current status from the database- research = (- db_session.query(ResearchHistory)- .filter(ResearchHistory.id == research_id)- .first()- )-- if not research:- logger.error(- f"Research with ID {research_id} not found during cleanup"- )- return-- # Clean up UserActiveResearch record- from ...database.models import UserActiveResearch-- active_record = (- db_session.query(UserActiveResearch)- .filter_by(username=username, research_id=research_id)- .first()- )-- if active_record:- logger.info(- f"Cleaning up active research {research_id} for user {username} "- f"(was started at {active_record.started_at})"- )- db_session.delete(active_record)- db_session.commit()- logger.info(- f"Cleaned up active research record for user {username}"- )- else:- logger.warning(- f"No active research record found to clean up for {research_id} / {username}"- )-- # Notify service database that research completed- self.notify_research_completed(username, research_id)-- except Exception:- logger.exception(- f"Error processing cleanup operation for research {research_id}"- )- db_session.rollback()-- def _process_error_update(self, db_session, op_data: Dict[str, Any]):- """- Process an error update operation with database access.-- Args:- db_session: The database session- op_data: The operation data- """- research_id = op_data["research_id"]-- try:- research = (- db_session.query(ResearchHistory)- .filter_by(id=research_id)- .first()- )-- if not research:- logger.error(- f"Research with ID {research_id} not found for error update"- )- return-- # Calculate duration- from ..models.database import calculate_duration-- duration_seconds = calculate_duration(research.created_at)-- # Update the ResearchHistory object- research.status = op_data["status"]- research.completed_at = op_data["completed_at"]- research.duration_seconds = duration_seconds- research.research_meta = op_data["metadata"]-- # Add error report path if available- if op_data.get("report_path"):- research.report_path = op_data["report_path"]-- db_session.commit()-- logger.info(- f"Updated research {research_id} with status '{op_data['status']}' "- f"and error: {op_data['error_message']}"- )-- # Update memory tracker- memory_tracker = get_memory_queue_tracker()- memory_tracker.update_task_status(- research_id,- op_data["status"],- )-- except Exception:- logger.exception(- f"Error processing error update for research {research_id}"- )- db_session.rollback()-- def _process_progress_update(self, db_session, op_data: Dict[str, Any]):- """- Process a progress update operation with database access.-- Args:- db_session: The database session- op_data: The operation data- """- research_id = op_data["research_id"]- progress = op_data["progress"]-- try:- research = (- db_session.query(ResearchHistory)- .filter_by(id=research_id)- .first()- )-- if not research:- logger.error(- f"Research with ID {research_id} not found for progress update"- )- return-- # Update progress- research.progress = progress- db_session.commit()-- logger.debug(- f"Updated research {research_id} progress to {progress}%"- )-- except Exception:- logger.exception(- f"Error processing progress update for research {research_id}"- )- db_session.rollback()-- def _process_token_metrics(self, db_session, op_data: Dict[str, Any]):- """- Process token metrics operation with database access.-- Args:- db_session: The database session- op_data: The operation data containing token metrics- """- research_id = op_data.get("research_id")- token_data = op_data.get("token_data", {})-- try:- from ...database.models import ModelUsage, TokenUsage-- # Extract token data- prompt_tokens = token_data.get("prompt_tokens", 0)- completion_tokens = token_data.get("completion_tokens", 0)- total_tokens = prompt_tokens + completion_tokens-- # Create TokenUsage record- token_usage = TokenUsage(- research_id=research_id,- model_name=token_data.get("model_name"),- provider=token_data.get("provider"),- prompt_tokens=prompt_tokens,- completion_tokens=completion_tokens,- total_tokens=total_tokens,- # Research context- research_query=token_data.get("research_query"),- research_mode=token_data.get("research_mode"),- research_phase=token_data.get("research_phase"),- search_iteration=token_data.get("search_iteration"),- # Performance metrics- response_time_ms=token_data.get("response_time_ms"),- success_status=token_data.get("success_status", "success"),- error_type=token_data.get("error_type"),- # Search engine context- search_engines_planned=token_data.get("search_engines_planned"),- search_engine_selected=token_data.get("search_engine_selected"),- # Call stack tracking- calling_file=token_data.get("calling_file"),- calling_function=token_data.get("calling_function"),- call_stack=token_data.get("call_stack"),- )- db_session.add(token_usage)-- # Update or create ModelUsage statistics- if research_id and token_data.get("model_name"):- model_usage = (- db_session.query(ModelUsage)- .filter_by(- research_id=research_id,- model_name=token_data.get("model_name"),- )- .first()- )-- if model_usage:- model_usage.prompt_tokens += prompt_tokens- model_usage.completion_tokens += completion_tokens- model_usage.total_tokens += total_tokens- model_usage.calls += 1- else:- model_usage = ModelUsage(- research_id=research_id,- model_name=token_data.get("model_name"),- provider=token_data.get("provider"),- prompt_tokens=prompt_tokens,- completion_tokens=completion_tokens,- total_tokens=total_tokens,- calls=1,- )- db_session.add(model_usage)-- db_session.commit()-- # Debug log removed to reduce clutter-- except Exception:- logger.exception(- f"Error processing token metrics for research {research_id}"- )- db_session.rollback()---# Global queue processor instance-queue_processor = QueueProcessor()diff --git a/src/local_deep_research/web/queue/processor_v2.py b/src/local_deep_research/web/queue/processor_v2.pyindex b4cd691fd6..0cadb8147c 100644--- a/src/local_deep_research/web/queue/processor_v2.py+++ b/src/local_deep_research/web/queue/processor_v2.py@@ -5,7 +5,8 @@ Supports both direct execution and queue modes.import threadingimport time-from typing import Set+import uuid+from typing import Any, Dict, Optional, Setfrom loguru import logger@@ -42,22 +43,19 @@ class QueueProcessorV2:self.running = Falseself.thread = None- # In the new per-user encrypted database architecture,- # there's no system database for global settings.- # Use defaults for queue processor configuration.- self.max_concurrent_per_user = 3- self.direct_mode = True-- # Log configuration+ # Per-user settings will be retrieved from each user's database+ # when processing their queue using SettingsManagerlogger.info(- "Queue processor using default configuration "- "(per-user encrypted databases don't have system-wide settings)"+ "Queue processor v2 initialized - will use per-user settings from SettingsManager")# Track which users we should checkself._users_to_check: Set[str] = set()self._users_lock = threading.Lock()+ # Track pending operations from background threads+ self.pending_operations = {}+def start(self):"""Start the queue processor thread."""if self.running:@@ -101,8 +99,8 @@ class QueueProcessorV2:research_id: The research ID**kwargs: Additional parameters for direct execution (query, mode, etc.)"""- if self.direct_mode and kwargs:- # Direct mode with all required parameters - try to start immediately+ # Check user's queue_mode setting when we have database access+ if kwargs:session_id = kwargs.get("session_id")if session_id:# Check if we can start it directly@@ -111,40 +109,68 @@ class QueueProcessorV2:)if password:try:- # Open database and check active count+ # Open database and check settings + active countengine = db_manager.open_user_database(username, password)if engine:with get_user_db_session(username) as db_session:- # Count active researches- active_count = (- db_session.query(UserActiveResearch)- .filter_by(- username=username, status="in_progress"- )- .count()+ # Get user's settings using SettingsManager+ from ...settings.manager import SettingsManager++ settings_manager = SettingsManager(db_session)++ # Get user's queue_mode setting (env > DB > default)+ queue_mode = settings_manager.get_setting(+ "app.queue_mode", "direct")- if active_count < self.max_concurrent_per_user:- # We have slots - start directly!- logger.info(- f"Direct mode: Starting research {research_id} immediately "- f"(active: {active_count}/{self.max_concurrent_per_user})"- )+ # Get user's max concurrent setting (env > DB > default)+ max_concurrent = settings_manager.get_setting(+ "app.max_concurrent_researches", 3+ )++ logger.debug(+ f"User {username} settings: queue_mode={queue_mode}, "+ f"max_concurrent={max_concurrent}"+ )- # Start the research directly- self._start_research_directly(- username,- research_id,- password,- **kwargs,+ # Only try direct execution if user has queue_mode="direct"+ if queue_mode == "direct":+ # Count active researches+ active_count = (+ db_session.query(UserActiveResearch)+ .filter_by(+ username=username,+ status="in_progress",+ )+ .count())- return++ if active_count < max_concurrent:+ # We have slots - start directly!+ logger.info(+ f"Direct mode: Starting research {research_id} immediately "+ f"(active: {active_count}/{max_concurrent})"+ )++ # Start the research directly+ self._start_research_directly(+ username,+ research_id,+ password,+ **kwargs,+ )+ return+ else:+ logger.info(+ f"Direct mode: Max concurrent reached ({active_count}/"+ f"{max_concurrent}), queueing {research_id}"+ )else:logger.info(- f"Direct mode: Max concurrent reached ({active_count}/"- f"{self.max_concurrent_per_user}), queueing {research_id}"+ f"User {username} has queue_mode={queue_mode}, "+ f"queueing research {research_id}")except Exception:logger.exception(@@ -291,33 +317,48 @@ class QueueProcessorV2:"""Main loop that processes the queue."""while self.running:try:- # Get list of users to check+ # Get list of users to check (don't clear immediately)with self._users_lock:users_to_check = list(self._users_to_check)- self._users_to_check.clear()# Process each user's queue+ users_to_remove = []for user_session in users_to_check:try:username, session_id = user_session.split(":", 1)- self._process_user_queue(username, session_id)+ # _process_user_queue returns True if queue is empty+ queue_empty = self._process_user_queue(+ username, session_id+ )+ if queue_empty:+ users_to_remove.append(user_session)except Exception:logger.exception(f"Error processing queue for {user_session}")+ # On error, remove the user to avoid infinite loops+ users_to_remove.append(user_session)++ # Only remove users whose queues are now empty+ with self._users_lock:+ for user_session in users_to_remove:+ self._users_to_check.discard(user_session)except Exception:logger.exception("Error in queue processor loop")time.sleep(self.check_interval)- def _process_user_queue(self, username: str, session_id: str):+ def _process_user_queue(self, username: str, session_id: str) -> bool:"""Process the queue for a specific user.Args:username: The usernamesession_id: The Flask session ID++ Returns:+ True if the queue is empty, False if there are still items"""# Get the user's password from session storepassword = session_password_store.get_session_password(@@ -327,7 +368,7 @@ class QueueProcessorV2:logger.debug(f"No password available for user {username}, skipping queue check")- return+ return True # Remove from checking# Open the user's encrypted databasetry:@@ -335,12 +376,22 @@ class QueueProcessorV2:engine = db_manager.open_user_database(username, password)if not engine:logger.error(f"Failed to open database for user {username}")- return+ return True # Remove from checking# Get a session and process the queuewith get_user_db_session(username) as db_session:queue_service = UserQueueService(db_session)+ # Get user's settings using SettingsManager+ from ...settings.manager import SettingsManager++ settings_manager = SettingsManager(db_session)++ # Get user's max concurrent setting (env > DB > default)+ max_concurrent = settings_manager.get_setting(+ "app.max_concurrent_researches", 3+ )+# Get queue statusqueue_status = queue_service.get_queue_status() or {"active_tasks": 0,@@ -348,12 +399,15 @@ class QueueProcessorV2:}# Calculate available slots- available_slots = (- self.max_concurrent_per_user - queue_status["active_tasks"]- )+ available_slots = max_concurrent - queue_status["active_tasks"]- if available_slots <= 0 or queue_status["queued_tasks"] == 0:- return+ if available_slots <= 0:+ # No slots available, but queue might not be empty+ return False # Keep checking++ if queue_status["queued_tasks"] == 0:+ # Queue is empty+ return True # Remove from checkinglogger.info(f"Processing queue for {username}: "@@ -371,8 +425,15 @@ class QueueProcessorV2:available_slots,)+ # Check if there are still items in queue+ updated_status = queue_service.get_queue_status() or {+ "queued_tasks": 0+ }+ return updated_status["queued_tasks"] == 0+except Exception:logger.exception(f"Error processing queue for user {username}")+ return True # Remove from checking on errordef _start_queued_researches(self,@@ -549,6 +610,157 @@ class QueueProcessorV2:logger.exception(f"Error in process_user_request for {username}")return 0+ def queue_progress_update(+ self, username: str, research_id: str, progress: float+ ):+ """+ Queue a progress update that needs database access.+ For compatibility with old processor during migration.++ Args:+ username: The username+ research_id: The research ID+ progress: The progress value (0-100)+ """+ # In processor_v2, we can update directly if we have database access+ # or queue it for later processing+ operation_id = str(uuid.uuid4())+ if not hasattr(self, "pending_operations"):+ self.pending_operations = {}++ self.pending_operations[operation_id] = {+ "username": username,+ "operation_type": "progress_update",+ "research_id": research_id,+ "progress": progress,+ "timestamp": time.time(),+ }+ logger.debug(+ f"Queued progress update for research {research_id}: {progress}%"+ )++ def queue_error_update(+ self,+ username: str,+ research_id: str,+ status: str,+ error_message: str,+ metadata: Dict[str, Any],+ completed_at: str,+ report_path: Optional[str] = None,+ ):+ """+ Queue an error status update that needs database access.+ For compatibility with old processor during migration.++ Args:+ username: The username+ research_id: The research ID+ status: The status to set (failed, suspended, etc.)+ error_message: The error message+ metadata: Research metadata+ completed_at: Completion timestamp+ report_path: Optional path to error report+ """+ operation_id = str(uuid.uuid4())+ if not hasattr(self, "pending_operations"):+ self.pending_operations = {}++ self.pending_operations[operation_id] = {+ "username": username,+ "operation_type": "error_update",+ "research_id": research_id,+ "status": status,+ "error_message": error_message,+ "metadata": metadata,+ "completed_at": completed_at,+ "report_path": report_path,+ "timestamp": time.time(),+ }+ logger.info(+ f"Queued error update for research {research_id} with status {status}"+ )++ def process_pending_operations_for_user(+ self, username: str, db_session+ ) -> int:+ """+ Process pending operations for a user when we have database access.+ Called from request context where encrypted database is accessible.+ For compatibility with old processor during migration.++ Args:+ username: Username to process operations for+ db_session: Active database session for the user++ Returns:+ Number of operations processed+ """+ if not hasattr(self, "pending_operations"):+ return 0++ # Find pending operations for this user+ operations_to_process = []+ for op_id, op_data in list(self.pending_operations.items()):+ if op_data["username"] == username:+ operations_to_process.append((op_id, op_data))++ if not operations_to_process:+ return 0++ processed_count = 0++ for op_id, op_data in operations_to_process:+ try:+ operation_type = op_data.get("operation_type")++ if operation_type == "progress_update":+ # Update progress in database+ from ...database.models import ResearchHistory++ research = (+ db_session.query(ResearchHistory)+ .filter_by(research_id=op_data["research_id"])+ .first()+ )+ if research:+ # Store progress in metadata or wherever appropriate+ if research.metadata:+ research.metadata["progress"] = op_data["progress"]+ else:+ research.metadata = {+ "progress": op_data["progress"]+ }+ db_session.commit()+ processed_count += 1++ elif operation_type == "error_update":+ # Update error status in database+ from ...database.models import ResearchHistory++ research = (+ db_session.query(ResearchHistory)+ .filter_by(research_id=op_data["research_id"])+ .first()+ )+ if research:+ research.status = op_data["status"]+ research.error_message = op_data["error_message"]+ research.metadata = op_data["metadata"]+ research.completed_at = op_data["completed_at"]+ if op_data.get("report_path"):+ research.report_path = op_data["report_path"]+ db_session.commit()+ processed_count += 1++ # Remove processed operation+ del self.pending_operations[op_id]++ except Exception:+ logger.exception(f"Error processing operation {op_id}")++ return processed_count+# Global queue processor instancequeue_processor = QueueProcessorV2()diff --git a/src/local_deep_research/web/services/research_service.py b/src/local_deep_research/web/services/research_service.pyindex 6c7245adb8..a3e5ebb40a 100644--- a/src/local_deep_research/web/services/research_service.py+++ b/src/local_deep_research/web/services/research_service.py@@ -537,7 +537,7 @@ def run_research_process(# Queue the progress update to be processed in main threadif adjusted_progress is not None:- from ..queue.processor import queue_processor+ from ..queue.processor_v2 import queue_processorif username:queue_processor.queue_progress_update(@@ -1603,9 +1603,8 @@ def run_research_process("""# Queue the error update to be processed in main thread- # Using the existing queue processor system (there are multiple implementations:- # processor.py, processor_v2.py, and several queue models as noted in issue #596)- from ..queue.processor import queue_processor+ # Using the queue processor v2 system+ from ..queue.processor_v2 import queue_processorif username:queue_processor.queue_error_update(@@ -1795,7 +1794,7 @@ def handle_termination(# Queue the status update to be processed in the main thread# This avoids Flask request context errors in background threadstry:- from ..queue.processor import queue_processor+ from ..queue.processor_v2 import queue_processornow = datetime.now(UTC)completed_at = now.isoformat()