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: memoize the RAG roster's cleaned document names (#17494)

Daniel Hane33cb8d25b9d
diff --git a/studio/backend/routes/inference.py b/studio/backend/routes/inference.py
index 20fc9bdb0b7..8bb447137d7 100644
--- a/studio/backend/routes/inference.py
+++ b/studio/backend/routes/inference.py
@@ -6005,6 +6005,8 @@ def _roster_scopes(rag_scope: dict) -> list[str]:
return scopes
+# Runs per document, 3x per row, every RAG turn. typed: 1 and 1.0 hash alike but str() differently.
[email protected]_cache(maxsize = 65536, typed = True)
def _roster_name(raw: object) -> str:
"""Linked-folder names are raw disk paths; newlines, quotes and direction overrides are neutralized."""
name = _re.sub(r"\s+", " ", str(raw or "").translate(_ROSTER_STRIP)).strip()
diff --git a/studio/backend/tests/test_rag_roster_name_memo.py b/studio/backend/tests/test_rag_roster_name_memo.py
new file mode 100644
index 00000000000..04c15a9925f
--- /dev/null
+++ b/studio/backend/tests/test_rag_roster_name_memo.py
@@ -0,0 +1,66 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""The roster's cleaned document names are cached, with the same text as a fresh cleanup."""
+
+import asyncio
+
+import pytest
+
+from core.rag import store
+
+TOOLS = [{"type": "function", "function": {"name": "search_knowledge_base"}}]
+
+NAMES = [
+ "syllabus.pdf",
+ " spaced \t\n name .pdf",
+ 'quote " and back\\slash.txt',
+ "‮evil‬.pdf",
+ "⁦isolate⁩.md",
+ "line\r\nbreak.pdf",
+ "x" * 300 + ".pdf",
+ "\U0001f600" * 130,
+ "研究資料.pdf",
+ "",
+ " ",
+]
+
+
+def roster_name():
+ from routes import inference
+
+ inference._roster_name.cache_clear()
+ yield inference._roster_name
+ inference._roster_name.cache_clear()
+
+
+def test_cached_names_match_a_fresh_cleanup(roster_name):
+ for raw in [*NAMES, None, 7, 7.0, True, b"bytes.pdf"]:
+ assert roster_name(raw) == roster_name(raw) == roster_name.__wrapped__(raw)
+ assert [roster_name(1), roster_name(1.0), roster_name(True)] == ["1", "1.0", "True"]
+
+
+def test_each_filename_is_cleaned_once_across_turns(roster_name, rag_conn):
+ from routes import inference
+
+ for i in range(60):
+ store.create_document(
+ rag_conn,
+ scope = "project_p1",
+ filename = f"doc {i:03d}.pdf",
+ sha256 = f"d{i}",
+ document_id = f"d{i}",
+ status = "completed",
+ )
+ rag_conn.execute("UPDATE documents SET num_chunks = 3")
+ rag_conn.commit()
+
+ def nudge():
+ return asyncio.run(inference._apply_rag_nudge("", TOOLS, rag_scope = {"project_id": "p1"}))
+
+ first = nudge()
+ assert "doc 059.pdf" in first and "more" in first
+ assert roster_name.cache_info().misses == 60
+ assert nudge() == first
+ assert roster_name.cache_info().misses == 60