Skip to content

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.

Studio: let conversation search return every earlier turn that fits (#16036)

Nilayaf3712bf95b9
diff --git a/studio/backend/core/inference/tools.py b/studio/backend/core/inference/tools.py
index e5f3a40d0a7..29a4163dd5f 100644
--- a/studio/backend/core/inference/tools.py
+++ b/studio/backend/core/inference/tools.py
@@ -13692,18 +13692,17 @@ def _search_conversation(arguments: dict, rag_scope: dict | None) -> str:
if not found:
return "No earlier turns of this conversation matched that query."
- # Halve until the rendered result fits: chunk size is a target, not a weight (overlap, other
- # tokenizer, markup). A single chunk that still does not fit is refused.
+ # One fewer until the text the model reads fits: chunk size is a target, not a weight (overlap,
+ # other tokenizer, markup). A single chunk that still does not fit is refused.
if budget is not None:
counter = scope.get("token_counter")
attempt = max(1, int(top_k or 1))
while True:
- rendered = _rendered_conversation_search(found)
- if _conversation_search_cost(rendered, counter) <= int(budget):
- return rendered
+ if _conversation_search_cost(found[0], counter) <= int(budget):
+ return _rendered_conversation_search(found)
if attempt <= 1:
return "There is no room left in this context to search earlier conversation."
- attempt = max(1, attempt // 2)
+ attempt -= 1
found = _recall(attempt)
if not found:
return "No earlier turns of this conversation matched that query."
@@ -13731,7 +13730,7 @@ def _conversation_search_tokens(text: str) -> int:
def _rendered_conversation_search(found) -> str:
- """The tool result exactly as the model would receive it."""
+ """The full result; the model never sees the source map."""
text, sources = found
if sources:
import json as _json
diff --git a/studio/backend/tests/test_conversation_recall_injection.py b/studio/backend/tests/test_conversation_recall_injection.py
index 9d0d78cac97..314e00e5bc1 100644
--- a/studio/backend/tests/test_conversation_recall_injection.py
+++ b/studio/backend/tests/test_conversation_recall_injection.py
@@ -944,7 +944,7 @@ def test_conversation_search_refuses_a_result_the_budget_cannot_hold(archived, m
{"thread_id": THREAD, "budget_tokens": rag_config.CHUNK_TOKENS * 4},
)
- assert asked == [4, 2, 1]
+ assert asked == [4, 3, 2, 1]
assert "no room" in answer.lower()
@@ -971,6 +971,41 @@ def test_conversation_search_returns_what_the_budget_does_hold(archived, monkeyp
assert "an earlier turn" in answer
+def test_conversation_search_prices_only_what_the_model_reads(archived, monkeypatch):
+ from core.inference.tool_loop_controller import strip_result_for_model
+ from core.rag import config as rag_config
+ from core.rag import tool as rag_tool
+
+ def fake_recall(
+ thread_id,
+ query,
+ *,
+ top_k = None,
+ branch_messages = None,
+ ):
+ sources = [
+ {"filename": "conversation", "text": "y" * 4 * rag_config.CHUNK_TOKENS, "turn": n}
+ for n in range(1, top_k + 1)
+ ]
+ return rag_tool.render_conversation_sources(sources), sources
+
+ monkeypatch.setattr(conversation_archive, "recall", fake_recall)
+
+ def model_chunks(budget, top_k):
+ answer = tools_mod._search_conversation(
+ {"query": "pelicans", "top_k": top_k},
+ {"thread_id": THREAD, "budget_tokens": budget, "token_counter": lambda t: len(t) // 4},
+ )
+ # The source card still receives the citation data the model does not read.
+ assert tools_mod.RAG_SOURCES_SENTINEL in answer
+ return strip_result_for_model(answer, "search_conversation").count("<chunk ")
+
+ assert model_chunks(rag_config.CHUNK_TOKENS * 5, 4) == 4
+ assert model_chunks(rag_config.CHUNK_TOKENS * 3 // 2, 1) == 1
+ # Markup pushes four full chunks past four chunks of room; three still fit.
+ assert model_chunks(rag_config.CHUNK_TOKENS * 4, 8) == 3
+
+
def test_the_conversation_tool_survives_studios_explicit_allowlist(monkeypatch):
"""The allowlist must keep search_conversation, since Unsloth's enabled_tools never names it."""
import asyncio