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: keep project sources when a knowledge base is picked (#14255)
Daniel Hane3b00a1e7c44
diff --git a/studio/backend/core/rag/tool.py b/studio/backend/core/rag/tool.pyindex db57485db52..de2a0f1ddd4 100644--- a/studio/backend/core/rag/tool.py+++ b/studio/backend/core/rag/tool.py@@ -56,20 +56,19 @@ def _resolve_scope(scope_project_id: str | None = None,scope_conversation_id: str | None = None,) -> str | list[str] | None:- """KB (an explicit pick) is exclusive; project and thread scopes combine so a- project chat also retrieves from its own attached documents.+ """A KB pick replaces the thread scope but keeps the project's (#9485).The conversation archive is exclusive too, and takes precedence: it is a differentcorpus (this chat's evicted turns), so sharing a top-K would have old turns anddocument passages crowd each other out."""if scope_conversation_id:return conversation_archive_scope(scope_conversation_id)- if scope_kb_id:- return kb_scope(scope_kb_id)scopes = []+ if scope_kb_id:+ scopes.append(kb_scope(scope_kb_id))if scope_project_id:scopes.append(project_scope(scope_project_id))- if scope_thread_id:+ if scope_thread_id and not scope_kb_id:scopes.append(thread_scope(scope_thread_id))if not scopes:return Nonediff --git a/studio/backend/routes/inference.py b/studio/backend/routes/inference.pyindex 455a55ada9c..53d5bd8e051 100644--- a/studio/backend/routes/inference.py+++ b/studio/backend/routes/inference.py@@ -6484,14 +6484,14 @@ _ROSTER_COUNT_SQL = (def _roster_scopes(rag_scope: dict) -> list[str]:- """KB is exclusive, matching retrieval. Thread precedes project so a file just- attached to this chat survives truncation."""+ """Matches _resolve_scope. The pick precedes project so a just-attached file survives+ truncation."""from core.rag.store import kb_scope, project_scope, thread_scope- if rag_scope.get("kb_id"):- return [kb_scope(rag_scope["kb_id"])]scopes = []- if rag_scope.get("thread_id"):+ if rag_scope.get("kb_id"):+ scopes.append(kb_scope(rag_scope["kb_id"]))+ elif rag_scope.get("thread_id"):scopes.append(thread_scope(rag_scope["thread_id"]))if rag_scope.get("project_id"):scopes.append(project_scope(rag_scope["project_id"]))diff --git a/studio/backend/tests/test_rag_kb_keeps_project_scope.py b/studio/backend/tests/test_rag_kb_keeps_project_scope.pynew file mode 100644index 00000000000..763cc381530--- /dev/null+++ b/studio/backend/tests/test_rag_kb_keeps_project_scope.py@@ -0,0 +1,104 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""#9485: picking a knowledge base in a project chat must not drop the project's sources."""++import asyncio+import json+import math++import pytest++from core.rag import store, tool+from core.rag.chunking import Chunk++VOCAB = ["alpha", "bravo", "company", "customer", "thread"]+++def _embed(text):+ v = [float(text.lower().count(w)) for w in VOCAB]+ n = math.sqrt(sum(x * x for x in v)) or 1.0+ return [x / n for x in v]+++def bow_embeddings(monkeypatch):+ from core.rag import embeddings+ monkeypatch.setattr(+ embeddings,+ "encode",+ lambda texts, *, model_name = None, normalize = True: [_embed(t) for t in texts],+ )+ monkeypatch.setattr(embeddings, "dim", lambda model_name = None: len(VOCAB))+++def _add_doc(conn, scope, doc_id, filename, text):+ store.create_document(conn, scope = scope, filename = filename, sha256 = doc_id, document_id = doc_id)+ chunk = Chunk(+ text = text,+ token_count = len(text.split()),+ page_number = None,+ source_page_index = 0,+ chunk_index = 0,+ page_char_start = 0,+ page_char_end = len(text),+ )+ store.add_chunks(conn, scope, doc_id, [chunk], [_embed(text)])+ conn.execute("UPDATE documents SET status='completed', num_chunks=1 WHERE id=?", (doc_id,))+ conn.commit()+++def test_scope_resolution():+ assert tool._resolve_scope("K", "T", "P") == ["kb_K", "project_P"]+ assert tool._resolve_scope("K", "T", None) == "kb_K"+ assert tool._resolve_scope(None, "T", "P") == ["project_P", "thread_T"]+ # The conversation archive stays exclusive.+ assert tool._resolve_scope("K", "T", "P", "C") == store.conversation_archive_scope("C")+++def _seed(conn):+ _add_doc(conn, "kb_K", "dk", "company.pdf", "alpha bravo company policy")+ _add_doc(conn, "project_P", "dp", "customer.pdf", "alpha bravo customer notes")+ _add_doc(conn, "thread_T", "dt", "thread.pdf", "alpha bravo thread upload")+++def test_search_with_kb_also_reads_project(rag_conn, bow_embeddings):+ _seed(rag_conn)+ _, sources = tool.search_knowledge_base_with_sources(+ query = "alpha bravo", scope_kb_id = "K", scope_thread_id = "T", scope_project_id = "P"+ )+ assert {s["filename"] for s in sources} == {"company.pdf", "customer.pdf"}+++def test_autoinject_with_kb_also_reads_project(rag_conn, bow_embeddings, monkeypatch):+ from core.inference import tools+ from storage import rag_db++ monkeypatch.setattr(rag_db, "RAG_AVAILABLE", True, raising = False)+ monkeypatch.setattr(rag_db, "rag_available", lambda: True, raising = False)+ _seed(rag_conn)+ out = tools.build_rag_autoinject(+ [{"role": "user", "content": "alpha bravo"}],+ {+ "kb_id": "K",+ "thread_id": "T",+ "project_id": "P",+ "autoinject": True,+ "autoinject_min_score": 0.0,+ },+ )+ blob = json.dumps(out)+ assert "company.pdf" in blob and "customer.pdf" in blob and "thread.pdf" not in blob+++def test_roster_with_kb_also_names_project(rag_conn):+ from routes import inference++ _seed(rag_conn)+ tools = [{"type": "function", "function": {"name": "search_knowledge_base"}}]+ out = asyncio.run(+ inference._apply_rag_nudge(+ "", tools, rag_scope = {"kb_id": "K", "thread_id": "T", "project_id": "P"}+ )+ )+ assert '"company.pdf"' in out and '"customer.pdf"' in out and '"thread.pdf"' not in outdiff --git a/studio/backend/tests/test_rag_nudge_roster.py b/studio/backend/tests/test_rag_nudge_roster.pyindex a61b5b7e8e3..04481d1587f 100644--- a/studio/backend/tests/test_rag_nudge_roster.py+++ b/studio/backend/tests/test_rag_nudge_roster.py@@ -146,11 +146,12 @@ def test_roster_skips_a_filename_that_collapses_to_nothing(rag_conn):assert '""' not in out-def test_kb_scope_excludes_other_scopes(rag_conn):+def test_kb_scope_replaces_thread_but_keeps_project(rag_conn):_doc(rag_conn, "kb_k1", "d1", "kb.pdf")_doc(rag_conn, "project_p1", "d2", "project.pdf")- out = _nudge({"kb_id": "k1", "project_id": "p1"})- assert "kb.pdf" in out and "project.pdf" not in out+ _doc(rag_conn, "thread_t1", "d3", "thread.pdf")+ out = _nudge({"kb_id": "k1", "project_id": "p1", "thread_id": "t1"})+ assert "kb.pdf" in out and "project.pdf" in out and "thread.pdf" not in outdef test_roster_truncates_with_exact_remainder(rag_conn):diff --git a/studio/frontend/src/components/assistant-ui/thread.tsx b/studio/frontend/src/components/assistant-ui/thread.tsxindex 4ebde4f3a8c..16a5ce68f0c 100644--- a/studio/frontend/src/components/assistant-ui/thread.tsx+++ b/studio/frontend/src/components/assistant-ui/thread.tsx@@ -509,8 +509,6 @@ type PromptQueueTarget = {getDocumentThreadId: () => string | null;/** project captured when a new chat has no persisted row to read. */getQueueProjectId: () => string | null;- /** a knowledge base replaces every other scope, project sources included. */- usesKnowledgeBase: boolean;getRunningThreadIds: () => string[];isRunning: () => boolean;append: (prompt: QueuedPrompt) => void | Promise<void>;@@ -725,11 +723,6 @@ async function targetHasIndexingDocuments(item: PromptQueueItem) {return true;}}- // Unless a knowledge base is active: the adapter sends kb_id alone, so the- // project's sources cannot reach this run and waiting on them only delays it.- if (item.target.usesKnowledgeBase) {- return false;- }// Project sources are retrieved whatever the Docs pill says (chat-adapter's// rag_scope), and isIndexing() above only answers while the bar that watches// them is mounted, which a background queue has not. So ask directly, and@@ -4309,9 +4302,6 @@ const Composer: FC<{const usesThreadDocumentsAtQueueStart =chatStateAtQueueStart.ragEnabled &&chatStateAtQueueStart.ragSource.type === "thread";- const usesKnowledgeBaseAtQueueStart =- chatStateAtQueueStart.ragEnabled &&- chatStateAtQueueStart.ragSource.type === "kb";let deferModelResolution =chatStateAtQueueStart.modelLoading &&parseExternalModelId(@@ -4581,7 +4571,6 @@ const Composer: FC<{indexingActiveRef.current,getQueueProjectId: () => projectIdAtQueueStart,usesThreadDocuments: usesThreadDocumentsAtQueueStart,- usesKnowledgeBase: usesKnowledgeBaseAtQueueStart,usesLocalModel:parseExternalModelId(runSettingsAtQueueStart.params.checkpoint) === null,usesDeepResearch: runSettingsAtQueueStart.deepResearchEnabled,diff --git a/studio/frontend/src/features/chat/api/chat-adapter.ts b/studio/frontend/src/features/chat/api/chat-adapter.tsindex f189ffd408b..abcec00c0e6 100644--- a/studio/frontend/src/features/chat/api/chat-adapter.ts+++ b/studio/frontend/src/features/chat/api/chat-adapter.ts@@ -2383,12 +2383,12 @@ export async function buildLocalTokenCountExtras(rag_scope: {...(ragEnabled && ragSource.type === "kb"? { kb_id: ragSource.kbId }- : {- ...(ragEnabled && threadId ? { thread_id: threadId } : {}),- ...(projectRagEnabled && ragProjectId- ? { project_id: ragProjectId }- : {}),- }),+ : ragEnabled && threadId+ ? { thread_id: threadId }+ : {}),+ ...(projectRagEnabled && ragProjectId+ ? { project_id: ragProjectId }+ : {}),// An unpersisted New Chat has neither id, and {} is falsy in Python, so the count alone would// drop the tool and its nudge.default_top_k: ragTopK,@@ -4843,26 +4843,20 @@ export function createOpenAIStreamAdapter(if (transitionSignal.aborted) return;const ragScope =runtime.ragEnabled || projectRagEnabled- ? runtime.ragEnabled && runtime.ragSource.type === "kb"- ? {- kb_id: runtime.ragSource.kbId,- default_top_k: runtime.ragTopK,- mode: runtime.ragMode,- autoinject: runtime.ragAutoInject,- autoinject_min_score: runtime.ragAutoInjectMinScore,- }- : {- ...(runtime.ragEnabled+ ? {+ ...(runtime.ragEnabled && runtime.ragSource.type === "kb"+ ? { kb_id: runtime.ragSource.kbId }+ : runtime.ragEnabled? { thread_id: resolvedThreadId }: {}),- ...(projectRagEnabled && researchProjectId- ? { project_id: researchProjectId }- : {}),- default_top_k: runtime.ragTopK,- mode: runtime.ragMode,- autoinject: runtime.ragAutoInject,- autoinject_min_score: runtime.ragAutoInjectMinScore,- }+ ...(projectRagEnabled && researchProjectId+ ? { project_id: researchProjectId }+ : {}),+ default_top_k: runtime.ragTopK,+ mode: runtime.ragMode,+ autoinject: runtime.ragAutoInject,+ autoinject_min_score: runtime.ragAutoInjectMinScore,+ }: undefined;const threadKey = resolvedThreadId;@@ -6943,14 +6937,12 @@ export function createOpenAIStreamAdapter(rag_scope: {...(ragEnabled && ragSource.type === "kb"? { kb_id: ragSource.kbId }- : {- ...(ragEnabled && resolvedThreadId- ? { thread_id: resolvedThreadId }- : {}),- ...(projectRagEnabled && ragProjectId- ? { project_id: ragProjectId }- : {}),- }),+ : ragEnabled && resolvedThreadId+ ? { thread_id: resolvedThreadId }+ : {}),+ ...(projectRagEnabled && ragProjectId+ ? { project_id: ragProjectId }+ : {}),default_top_k: ragTopK,mode: ragMode,autoinject: resolveAutoInject(@@ -7146,14 +7138,12 @@ export function createOpenAIStreamAdapter(rag_scope: {...(ragEnabled && ragSource.type === "kb"? { kb_id: ragSource.kbId }- : {- ...(ragEnabled && resolvedThreadId- ? { thread_id: resolvedThreadId }- : {}),- ...(projectRagEnabled && ragProjectId- ? { project_id: ragProjectId }- : {}),- }),+ : ragEnabled && resolvedThreadId+ ? { thread_id: resolvedThreadId }+ : {}),+ ...(projectRagEnabled && ragProjectId+ ? { project_id: ragProjectId }+ : {}),default_top_k: ragTopK,mode: ragMode,autoinject: resolveAutoInject(diff --git a/studio/frontend/src/features/chat/types/api.ts b/studio/frontend/src/features/chat/types/api.tsindex bafd6aef365..973ba4ddfad 100644--- a/studio/frontend/src/features/chat/types/api.ts+++ b/studio/frontend/src/features/chat/types/api.ts@@ -738,7 +738,7 @@ export interface OpenAIChatCompletionsRequest {sandbox_level?: "high" | "low";/** Local models + enable_tools only. Full-access escape hatch. */bypass_permissions?: boolean;- /** `kb_id` is exclusive; otherwise project and thread scopes may combine. */+ /** `kb_id` replaces `thread_id`; `project_id` combines with either. */rag_scope?: {kb_id?: string;project_id?: string;diff --git a/studio/frontend/src/features/rag/components/thread-documents-bar.tsx b/studio/frontend/src/features/rag/components/thread-documents-bar.tsxindex 2a44482c6f7..038736755f5 100644--- a/studio/frontend/src/features/rag/components/thread-documents-bar.tsx+++ b/studio/frontend/src/features/rag/components/thread-documents-bar.tsx@@ -1028,18 +1028,16 @@ export function ThreadDocumentsBar({[],);- // Mirrors chat-adapter's rag_scope: an active KB replaces the project scope,- // but a KB preference left over while the pill is off does not.const threadProjectId = useThreadProjectId(effectiveThreadId);// Attaching before the row has been read would file the file by guess.const projectUnresolved = threadProjectId === undefined;// A host where the vector extension cannot load answers 503 to every project// source request, so do not open a scope it can only fail.const ragUnavailable = useRagAvailabilityStore((s) => s.isUnavailable());+ // Retrieval keeps the project beside a KB (#9485), so it is always watched for indexing.+ const watchedProjectId = ragUnavailable ? null : (threadProjectId ?? null);const projectId =- (ragEnabled && ragSource.type === "kb") || ragUnavailable- ? null- : (threadProjectId ?? null);+ ragEnabled && ragSource.type === "kb" ? null : watchedProjectId;// This chat's own choice if it made one, otherwise the saved default. Keeps a// pick in one chat from redirecting every other chat in the project.const projectAttachmentTarget =@@ -1076,8 +1074,11 @@ export function ThreadDocumentsBar({// from another chat is visible rather than silently in effect. Retrieval already// combines both scopes (core/rag/tool.py).const projectLister = useCallback(- () => (projectId ? listProjectDocuments(projectId) : Promise.resolve([])),- [projectId],+ () =>+ watchedProjectId+ ? listProjectDocuments(watchedProjectId)+ : Promise.resolve([]),+ [watchedProjectId],);const {documents: projectDocuments,@@ -1089,13 +1090,13 @@ export function ThreadDocumentsBar({retry: retryProject,canRetry: canRetryProject,} = useRagDocuments(- projectId ? { type: "project", projectId } : null,+ watchedProjectId ? { type: "project", projectId: watchedProjectId } : null,projectLister,);// Tell the composer whether any doc is still indexing, so it can hold a queued send until- // retrieval covers them (Composer.enqueueSend). For KB / RAG-off scope is null, so both lists are- // empty and this reads false. From the hooks, not the rows: work started in the Sources panel is+ // retrieval covers them (Composer.enqueueSend). A KB or RAG off leaves only the project watched,+ // which reads false without one. From the hooks, not the rows: work started in the Sources panel is// in flight before either instance has a row for it, and a job already running on a reopened// project arrives with the first list, so hold until that lands. Both scopes hold on their first// list, for the same reason: reopening a chat whose own attachment was still indexing lists@@ -1329,7 +1330,7 @@ export function ThreadDocumentsBar({<LinkedFolderCard name={folderNames.get(folderId)} docs={docs} /></div>));- const fileCount = documents.length + projectDocuments.length;+ const fileCount = documents.length + (projectId ? projectDocuments.length : 0);useEffect(() => {useChatRuntimeStore.getState().setRagPanelFileCount(fileCount);}, [fileCount]);diff --git a/studio/frontend/tests/project-attachment-simulations.test.ts b/studio/frontend/tests/project-attachment-simulations.test.tsindex a7beff77435..ddef33fe6c9 100644--- a/studio/frontend/tests/project-attachment-simulations.test.ts+++ b/studio/frontend/tests/project-attachment-simulations.test.ts@@ -115,9 +115,12 @@ function ragScope(opts: {projectId?: string | null;}): Record<string, string> {const { ragEnabled, kbId, threadId, projectRagEnabled, projectId } = opts;- if (ragEnabled && kbId) return { kb_id: kbId };return {- ...(ragEnabled && threadId ? { thread_id: threadId } : {}),+ ...(ragEnabled && kbId+ ? { kb_id: kbId }+ : ragEnabled && threadId+ ? { thread_id: threadId }+ : {}),...(projectRagEnabled && projectId ? { project_id: projectId } : {}),};}@@ -141,11 +144,13 @@ test("a project chat with the pill on retrieves both", () => {);});-test("a knowledge base replaces everything, project included", () => {+// #9485: picking a knowledge base in a project chat must not drop the project's sources.+test("a knowledge base replaces the thread but keeps the project", () => {assert.deepEqual(ragScope({ ragEnabled: true, kbId: "K", threadId: "T", projectRagEnabled: true, projectId: "P" }),- { kb_id: "K" },+ { kb_id: "K", project_id: "P" },);+ assert.deepEqual(ragScope({ ragEnabled: true, kbId: "K", threadId: "T" }), { kb_id: "K" });});// ------------------------------------------------------- folder syncs the composer gates ondiff --git a/studio/frontend/tests/rag-refresh-sequencing.test.ts b/studio/frontend/tests/rag-refresh-sequencing.test.tsindex a7da39cf551..d159cda08da 100644--- a/studio/frontend/tests/rag-refresh-sequencing.test.ts+++ b/studio/frontend/tests/rag-refresh-sequencing.test.ts@@ -137,9 +137,14 @@ test("a failure is only reported for the request still being awaited", () => {test("no project scope is opened where RAG cannot run", () => {assert.match(THREAD_DOCUMENTS_BAR,- /const projectId =\s*\(ragEnabled && ragSource\.type === "kb"\) \|\| ragUnavailable\s*\? null\s*: \(threadProjectId \?\? null\);/,+ /const watchedProjectId = ragUnavailable \? null : \(threadProjectId \?\? null\);\s*const projectId =\s*ragEnabled && ragSource\.type === "kb" \? null : watchedProjectId;/,+ );+ // Watched beside an active KB too: retrieval adds the project there (#9485), so a+ // send must still wait for a project source that is indexing.+ assert.match(+ THREAD_DOCUMENTS_BAR,+ /watchedProjectId \? \{ type: "project", projectId: watchedProjectId \} : null/,);- assert.match(THREAD_DOCUMENTS_BAR, /projectId \? \{ type: "project", projectId \} : null/);});// A project's sources also change from the Sources panel: an upload invalidated@@ -753,24 +758,13 @@ test("a failed row read holds a queued prompt instead of releasing it", () => {);});-// A knowledge base replaces every other scope in rag_scope, so a queue scoped to-// one cannot be affected by a project upload or a folder sync.-test("a knowledge-base queue does not wait on project sources", () => {- assert.match(- THREAD,- /const usesKnowledgeBaseAtQueueStart =\s*chatStateAtQueueStart\.ragEnabled &&\s*chatStateAtQueueStart\.ragSource\.type === "kb";/,- );- assert.match(THREAD, /usesKnowledgeBase: usesKnowledgeBaseAtQueueStart,/);- // Ahead of the project lookup, and after the thread one, which a KB queue- // never takes anyway.- assert.match(- THREAD,- /if \(item\.target\.usesKnowledgeBase\) \{\s*return false;\s*\}[\s\S]{0,900}?const projectId = threadId/,- );- // The exclusivity this relies on, in the adapter that builds the scope.+// A knowledge base replaces only the thread's documents in rag_scope; the project's+// sources still reach the run (#9485), so a KB queue waits on them like any other.+test("a knowledge-base queue still waits on project sources", () => {+ assert.doesNotMatch(THREAD, /usesKnowledgeBase/);assert.match(CHAT_ADAPTER,- /ragEnabled && ragSource\.type === "kb"\s*\? \{ kb_id: ragSource\.kbId \}/,+ /ragEnabled && ragSource\.type === "kb"\s*\? \{ kb_id: ragSource\.kbId \}\s*: ragEnabled && resolvedThreadId\s*\? \{ thread_id: resolvedThreadId \}\s*: \{\}\),\s*\.\.\.\(projectRagEnabled && ragProjectId\s*\? \{ project_id: ragProjectId \}/,);});