Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Studio: stop web results crowding attached files out of Deep Research (#17434)
Nilayce489c168684
diff --git a/studio/backend/core/research_runs.py b/studio/backend/core/research_runs.pyindex b0dd90cc7f4..edffd42e7e9 100644--- a/studio/backend/core/research_runs.py+++ b/studio/backend/core/research_runs.py@@ -713,6 +713,22 @@ def _fit_source_catalog(catalog: str, max_chars: int) -> str:return ("".join(kept) if not kept or kept[0].endswith("\n") else "\n\n".join(kept)).rstrip()+def _fit_synthesis_catalogs(+ web_entries: list[str], document_entries: list[str], max_chars: int+) -> tuple[str, str]:+ limits = _water_fill([len("\n".join(web_entries)), len("\n".join(document_entries))], max_chars)+ fitted = []+ for entries, limit in zip((web_entries, document_entries), limits):+ kept, used = [], -1+ for entry in entries:+ used += len(entry) + 1+ if used > limit:+ break+ kept.append(entry)+ fitted.append("\n".join(kept))+ return fitted[0], fitted[1]++def _fit_decision_inputs(question: str, plan: dict, system_chars: int, total_budget: int | None) -> tuple[str, str]:@@ -2347,8 +2363,7 @@ class ResearchSupervisor:raise LeaseLost()if resuming:sources = list(run.get("sources") or [])[:max_sources]- remaining = max(0, max_sources - len(sources))- document_sources = list(run.get("documentSources") or [])[:remaining]+ document_sources = list(run.get("documentSources") or [])[:max_sources]for step in run.get("steps") or []:result = step.get("result") if isinstance(step.get("result"), dict) else {}@@ -2393,7 +2408,7 @@ class ResearchSupervisor:or f"{source.get('documentId') or source.get('filename')}:{source.get('page') or ''}")if source_key not in document_source_keys:- if len(sources) + len(document_sources) >= max_sources:+ if len(document_sources) >= max_sources:continuewritten = await asyncio.to_thread(db.upsert_document_source,@@ -2666,7 +2681,7 @@ class ResearchSupervisor:or f"{source.get('documentId') or source.get('filename')}:{source.get('page') or ''}")if source_key not in document_source_keys:- if len(sources) + len(document_sources) >= max_sources:+ if len(document_sources) >= max_sources:continuewritten = await asyncio.to_thread(db.upsert_document_source,@@ -2691,7 +2706,7 @@ class ResearchSupervisor:rag_result = ""rag_sources = accepted_rag_sourcesfor source in mcp_sources:- if len(sources) + len(document_sources) >= max_sources:+ if len(document_sources) >= max_sources:breakwritten = await asyncio.to_thread(db.upsert_document_source,@@ -2705,7 +2720,7 @@ class ResearchSupervisor:rag_sources.append(source)step_sources = []for match in _URL_BLOCK.finditer(result if action["action"] == "search" else ""):- if len(sources) + len(document_sources) >= max_sources:+ if len(sources) >= max_sources:breaksource = {k: match.group(k).strip() for k in ("title", "url", "snippet")}allowed, _reason, _hostname = check_url_access(@@ -2818,18 +2833,18 @@ class ResearchSupervisor:await self._check_active(run["id"])if not completed_steps and not sources and not document_sources:raise ValueError(f"No research step gathered any evidence. {step_error}".rstrip())- source_catalog = "\n".join(+ web_entries = [f"{index}. Title: {_citation_title(source, source['url'])}\n URL: {source['url']}"for index, source in enumerate(sources, 1)- )- document_source_catalog = "\n".join(+ ]+ document_entries = [f"{index}. Filename: {source.get('filename') or 'Document'}\n"f" Page: {source.get('page') if source.get('page') is not None else '(unknown)'}\n"f" Citation: {_document_source_citation(source)}\n"f" Document ID: {source.get('documentId') or '(unknown)'}\n"f" Chunk ID: {source.get('chunkId') or '(unknown)'}"for index, source in enumerate(document_sources, 1)- )+ ]# Model-derived JSON shares the evidence budget; history gets only what the fixed scaffold leaves.total_budget = _prompt_char_budget(_SYNTHESIS_CONTEXT_RESERVE_TOKENS, _run_inference_request(run)@@ -2839,6 +2854,22 @@ class ResearchSupervisor:_SYNTHESIS_AUDIT_SYSTEM_PROMPT,run["config"],)+ report_system = _system_prompt_with_instructions(_REPORT_SYSTEM_PROMPT, run["config"])+ catalog_chars = len("\n".join(web_entries)) + len("\n".join(document_entries))+ source_catalog, document_source_catalog = _fit_synthesis_catalogs(+ web_entries,+ document_entries,+ catalog_chars+ if total_budget is None or not document_entries+ else max(+ 0,+ total_budget+ - max(len(audit_system), len(report_system))+ - len(question)+ - len(plan_json),+ )+ // 2,+ )audit_scaffold_chars = (len(audit_system)+ len(question)@@ -2916,7 +2947,6 @@ class ResearchSupervisor:breakexcept (ValueError, json.JSONDecodeError):continue- report_system = _system_prompt_with_instructions(_REPORT_SYSTEM_PROMPT, run["config"])report_scaffold_chars = (len(report_system)+ len(question)diff --git a/studio/backend/tests/test_research_document_evidence.py b/studio/backend/tests/test_research_document_evidence.pyindex 3a857c0110c..1405789e18b 100644--- a/studio/backend/tests/test_research_document_evidence.py+++ b/studio/backend/tests/test_research_document_evidence.py@@ -74,6 +74,7 @@ def _synthesis_prompt(knowledge_base,context_length = None,query = "pump",+ max_sources = 100,) -> str:from core import research_runs as worker@@ -92,7 +93,7 @@ def _synthesis_prompt("question": "how does the pump work?","budgets": {"maxSteps": STEPS,- "maxSources": 100,+ "maxSources": max_sources,"modelTimeoutSeconds": 900,"toolTimeoutSeconds": 10,},@@ -138,6 +139,39 @@ def test_every_step_keeps_its_top_document_passage_in_the_report_prompt(researchassert all(f"web-{step}-0" in prompt for step in range(STEPS))+def test_web_results_do_not_use_up_the_room_for_document_passages(research_home, monkeypatch):+ prompt = _synthesis_prompt(monkeypatch, _knowledge_base_results, max_sources = 40)++ assert all(f"manual-fact-{step}" in prompt for step in range(STEPS))+ assert all(f"Chunk ID: chunk-{step}\n" in prompt for step in range(STEPS))+ assert len(research_db.get_run("run-1")["sources"]) == 40+++def test_small_context_keeps_passages_when_both_catalogs_fill(research_home, monkeypatch):+ def distinct_chunks(step):+ sources = [+ {+ "chunkId": f"{index:08d}-1111-4222-8333-444455556666",+ "documentId": "9b2f6c1e-7d4a-4f0b-9c3e-2a1b5d6e7f80",+ "filename": "manual.pdf",+ "page": index + 1,+ "text": _passage(index),+ }+ for index in range(step * 5, step * 5 + 5)+ ]+ return (+ "\n\n".join(source["text"] for source in sources)+ + RAG_SOURCES_SENTINEL+ + json.dumps(sources)+ )++ prompt = _synthesis_prompt(monkeypatch, distinct_chunks, context_length = 8_192, max_sources = 40)++ assert all(f"web-{step}-0" in prompt for step in range(STEPS))+ assert "https://example.test/0/0" in prompt+ assert "Chunk ID: 00000000-1111-4222-8333-444455556666" in prompt++def test_web_results_keep_their_room_when_the_documents_match_nothing(research_home, monkeypatch):prompt = _synthesis_prompt(monkeypatch, lambda _step: "No matching chunks were found in the knowledge base."@@ -157,3 +191,14 @@ def test_small_context_keeps_web_results_when_the_documents_match_nothing()assert all(f"web-{step}-0" in prompt for step in range(STEPS))+++def test_web_only_catalog_is_not_trimmed(research_home, monkeypatch):+ prompt = _synthesis_prompt(+ monkeypatch,+ lambda _step: "No matching chunks were found in the knowledge base.",+ context_length = 4_096,+ )++ catalog = prompt.split("<source_catalog>\n", 1)[1].split("</source_catalog>", 1)[0]+ assert catalog.count("URL: ") == len(research_db.get_run("run-1")["sources"])