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 a chat's attached files when you fork it (#11295)
Nilayd35ed556b76c
diff --git a/studio/backend/core/rag/conversation_archive.py b/studio/backend/core/rag/conversation_archive.pyindex df12d7f23f3..db51f18126a 100644--- a/studio/backend/core/rag/conversation_archive.py+++ b/studio/backend/core/rag/conversation_archive.py@@ -2564,6 +2564,67 @@ def delete_thread_documents(thread_id: str, *, created_before: Optional[str] = Nreturn len(removed)+def copy_thread_documents(source_thread_id: str, thread_id: str) -> tuple[dict[str, str], bool]:+ """Copy a thread's finished uploads into another thread. Returns the source-to-copy document id+ map and whether an upload still being ingested was left behind. Failed uploads are not copied+ and not reported: the documents bar never shows them.++ Files are copied before the transaction so rag.db's write lock is never held across file I/O.+ """+ from .ingestion import _copy_upload, _remove_upload++ scope = store.thread_scope(thread_id)+ copied: list = []+ conn = rag_db.get_connection()+ try:+ rows = conn.execute(+ "SELECT * FROM documents WHERE scope=? ORDER BY created_at",+ (store.thread_scope(source_thread_id),),+ ).fetchall()+ documents = [dict(row) for row in rows if row["status"] == "completed"]+ for document in documents:+ copied.append(_copy_upload(document["stored_path"]))+ conn.execute("BEGIN IMMEDIATE")+ sources = []+ for document, stored_path in zip(documents, copied):+ source = store.get_document(conn, document["id"])+ if source is None or source["status"] != "completed":+ raise RuntimeError("Source document changed while its file was being copied")+ sources.append((source, stored_path))+ document_ids = store.copy_documents(conn, sources, scope, thread_id = thread_id)+ conn.commit()+ except Exception:+ conn.rollback()+ for stored_path in copied:+ _remove_upload(stored_path)+ raise+ finally:+ conn.close()+ return document_ids, any(row["status"] in ("pending", "running") for row in rows)+++def thread_has_documents(thread_id: str) -> bool:+ """Whether the thread has uploads that did not fail. Read over a metadata connection so a fork+ can still report them when vec0 cannot load and nothing can be copied."""+ if not rag_db.rag_db_path().is_file():+ return False+ conn = rag_db.get_metadata_connection()+ try:+ if not conn.execute(+ "SELECT 1 FROM sqlite_master WHERE type='table' AND name='documents'"+ ).fetchone():+ return False+ return (+ conn.execute(+ "SELECT 1 FROM documents WHERE scope=? AND status != 'failed' LIMIT 1",+ (store.thread_scope(thread_id),),+ ).fetchone()+ is not None+ )+ finally:+ conn.close()++def _delete_scope(scope: str,thread_id: str,diff --git a/studio/backend/core/rag/ingestion.py b/studio/backend/core/rag/ingestion.pyindex 04fb4470e96..fcf0c03927d 100644--- a/studio/backend/core/rag/ingestion.py+++ b/studio/backend/core/rag/ingestion.py@@ -15,7 +15,9 @@ import loggingimport osimport queueimport re+import shutilimport threading+import uuidfrom collections.abc import Callablefrom core.rag import account_db as rag_db@@ -70,6 +72,21 @@ def _remove_upload(stored_path: str | None, *, keep_path: str | None = None) ->logger.warning("failed to remove RAG upload %s", stored_path, exc_info = True)+def _copy_upload(stored_path: str | None) -> str | None:+ if not stored_path or not os.path.isfile(stored_path):+ return None+ from utils.paths import ensure_dir, rag_uploads_root++ ext = os.path.splitext(stored_path)[1].lower()+ target = str(ensure_dir(rag_uploads_root()) / f"{uuid.uuid4().hex}{ext}")+ try:+ shutil.copyfile(stored_path, target)+ except OSError:+ _remove_upload(target)+ raise+ return target++def _emit(job_id: str, event: dict) -> None:with _jobs_lock:q = _jobs.get(account_key(job_id))diff --git a/studio/backend/core/rag/store.py b/studio/backend/core/rag/store.pyindex 6d75938e9e2..596cb2057dd 100644--- a/studio/backend/core/rag/store.py+++ b/studio/backend/core/rag/store.py@@ -514,6 +514,69 @@ def delete_document(conn.commit()+def copy_documents(+ conn: sqlite3.Connection,+ documents: list[tuple[dict, str | None]],+ scope: str,+ *,+ thread_id: str,+) -> dict[str, str]:+ """Copy completed documents, paired with their file copies, into ``scope`` without+ committing. Returns the source-to-copy document id map."""+ document_ids: dict[str, str] = {}+ chunk_ids: dict[str, str] = {}+ for source, stored_path in documents:+ document_id = create_document(+ conn,+ scope = scope,+ filename = source["filename"],+ sha256 = source["sha256"],+ thread_id = thread_id,+ status = source["status"],+ stored_path = stored_path,+ embedding_model = source["embedding_model"],+ created_at = source["created_at"],+ commit = False,+ )+ document_ids[source["id"]] = document_id+ conn.execute(+ "UPDATE documents SET num_chunks=? WHERE id=?", (source["num_chunks"], document_id)+ )+ for r in conn.execute(+ "SELECT id, chunk_index FROM chunks WHERE document_id=?", (source["id"],)+ ).fetchall():+ chunk_ids[r["id"]] = f"{document_id}:{r['chunk_index']}"+ conn.execute(+ "INSERT INTO chunks("+ "id, document_id, scope, chunk_index, text, page_number, "+ "source_page_index, token_count, kind, pdf_regions_json) "+ "SELECT ? || ':' || chunk_index, ?, ?, chunk_index, text, page_number, "+ "source_page_index, token_count, kind, pdf_regions_json "+ "FROM chunks WHERE document_id=?",+ (document_id, document_id, scope, source["id"]),+ )+ conn.execute(+ "INSERT INTO chunks_fts(text, chunk_id, scope) "+ "SELECT text, id, scope FROM chunks WHERE document_id=?",+ (document_id,),+ )+ if chunk_ids and rag_db.vec_table_exists(conn):+ # vec0 scans the whole partition for any chunk filter, so read each source scope once.+ for source_scope in {source["scope"] for source, _ in documents}:+ conn.executemany(+ "INSERT INTO chunks_vec(scope, chunk_id, embedding) VALUES(?,?,?)",+ [+ (scope, chunk_ids[r["chunk_id"]], r["embedding"])+ for r in conn.execute(+ "SELECT chunk_id, embedding FROM chunks_vec WHERE scope=?",+ (source_scope,),+ ).fetchall()+ if r["chunk_id"] in chunk_ids+ ],+ )+ return document_ids++def linked_folder_rows_exist(conn: sqlite3.Connection) -> bool:"""Whether anything here can be hidden by the linked-folder filters.diff --git a/studio/backend/routes/chat_history.py b/studio/backend/routes/chat_history.pyindex e249a3f7ac4..dc776b59c88 100644--- a/studio/backend/routes/chat_history.py+++ b/studio/backend/routes/chat_history.py@@ -70,6 +70,7 @@ from storage.studio_db import (list_chat_messages,list_chat_messages_for_threads,list_chat_threads,+ remap_chat_thread_document_ids,sync_chat_messages,update_chat_project,update_chat_thread,@@ -864,6 +865,35 @@ def _remove_thread_rag_data(thread_ids, *, cutoff: "str | None" = None) -> None:logger.warning("Could not remove the uploaded documents for %s", thread_id)+def _copy_thread_rag_documents(source_thread_id: str, thread_id: str) -> bool:+ """Copy the source's uploads into the fork. False when the fork should warn that some were+ left behind."""+ try:+ from core.rag import conversation_archive+ from storage import rag_db++ if not rag_db.rag_available():+ return not conversation_archive.thread_has_documents(source_thread_id)+ document_ids, missed = conversation_archive.copy_thread_documents(+ source_thread_id, thread_id+ )+ if get_chat_thread(thread_id) is None:+ conversation_archive.delete_thread_documents(thread_id)+ return False+ if not document_ids and get_chat_thread(source_thread_id) is None:+ return False+ remap_chat_thread_document_ids(thread_id, document_ids)+ return not missed+ except Exception:+ logger.warning(+ "Could not copy the uploaded documents of %s to %s",+ source_thread_id,+ thread_id,+ exc_info = True,+ )+ return False++async def _remove_sandboxes(thread_ids, delete_files: bool) -> "tuple[int, list[str]]":"""Drop each thread's sandbox off the event loop. Never raises. Returns how many went and which idsstill have files. The chat is the only way to those files, so a caller that never offered the@@ -1673,11 +1703,15 @@ def fork_thread(# The source can be deleted between the reads above and the fork transaction, which the# threadpool lets run concurrently. Report it gone rather than as a server fault.raise HTTPException(status_code = 404, detail = f"Thread {thread_id} or fork message not found")- messages = list_chat_messages(payload.newThreadId)# Stub: v1 always starts a fresh container and surfaces the same warning for every provider.warning: Optional[str] = Noneif source.get("openaiCodeExecContainerId") or source.get("anthropicCodeExecContainerId"):warning = "Sandbox starts fresh in fork; files from parent are not carried over."+ if not _copy_thread_rag_documents(thread_id, payload.newThreadId):+ warning = " ".join(+ filter(None, [warning, "Documents attached to the parent chat were not copied."])+ )+ messages = list_chat_messages(payload.newThreadId)return ChatForkResponse(thread = thread_from_row(forked),messages = [ChatMessage(**m) for m in messages],diff --git a/studio/backend/storage/studio_db.py b/studio/backend/storage/studio_db.pyindex c512d4c4021..e8c81e95fbe 100644--- a/studio/backend/storage/studio_db.py+++ b/studio/backend/storage/studio_db.py@@ -4204,6 +4204,41 @@ def fork_chat_thread(conn.close()+def remap_chat_thread_document_ids(thread_id: str, document_ids: dict[str, str]) -> None:+ if not document_ids:+ return+ conn = get_connection()+ try:+ conn.execute("BEGIN IMMEDIATE")+ _ensure_chat_attachment_inventory_current(conn)+ rows = conn.execute(+ "SELECT id, content_json, attachments_json, metadata_json FROM chat_messages "+ "WHERE thread_id = ?",+ (thread_id,),+ ).fetchall()+ for row in rows:+ content_json, metadata_json = row["content_json"], row["metadata_json"]+ for old, new in document_ids.items():+ content_json = content_json.replace(old, new)+ metadata_json = metadata_json and metadata_json.replace(old, new)+ if (content_json, metadata_json) != (row["content_json"], row["metadata_json"]):+ conn.execute(+ "UPDATE chat_messages SET content_json = ?, metadata_json = ? "+ "WHERE thread_id = ? AND id = ?",+ (content_json, metadata_json, thread_id, row["id"]),+ )+ _replace_chat_attachment_inventory(+ conn, row["id"], row["attachments_json"], content_json+ )+ _mark_chat_attachment_inventory_clean(conn)+ conn.commit()+ except Exception:+ conn.rollback()+ raise+ finally:+ conn.close()++def count_forks_for_message(thread_id: str, message_id: str) -> int:conn = get_connection()try:diff --git a/studio/backend/tests/test_rag_thread_document_cleanup.py b/studio/backend/tests/test_rag_thread_document_cleanup.pyindex e9431723cd8..f65da1e0ee2 100644--- a/studio/backend/tests/test_rag_thread_document_cleanup.py+++ b/studio/backend/tests/test_rag_thread_document_cleanup.py@@ -1,7 +1,9 @@# SPDX-License-Identifier: AGPL-3.0-only# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0+import jsonimport os+import sqlite3import timefrom datetime import datetime, timezone@@ -12,7 +14,7 @@ from fastapi.testclient import TestClientfrom auth.authentication import get_current_subjectfrom core.rag import ingestion, storefrom routes import chat_history, rag as rag_routes-from storage import rag_db+from storage import rag_db, studio_db@pytest.fixture@@ -172,3 +174,325 @@ def test_thread_documents_go_without_sqlite_vec(client, monkeypatch):assert _document_ids(client) == set()assert _chunk_count("vecless") == 0assert not os.path.exists(path)+++def _add_message(client, thread_id, message_id):+ response = client.put(+ f"/api/chat/threads/{thread_id}/messages/{message_id}",+ json = {"id": message_id, "threadId": thread_id, "role": "user", "createdAt": 1},+ )+ assert response.status_code == 200, response.text+++def _fork(client, thread_id, message_id, new_thread_id):+ response = client.post(+ f"/api/chat/threads/{thread_id}/fork",+ json = {"messageId": message_id, "newThreadId": new_thread_id, "createdAt": 2},+ )+ assert response.status_code == 200, response.text+ return response.json()+++def _thread_documents(client, thread_id):+ response = client.get(f"/api/rag/threads/{thread_id}/documents")+ assert response.status_code == 200, response.text+ return response.json()["documents"]+++def _search(client, thread_id, query, mode):+ response = client.post(+ "/api/rag/search", json = {"query": query, "thread_id": thread_id, "mode": mode}+ )+ assert response.status_code == 200, response.text+ return response.json()["results"]+++def test_forking_a_thread_copies_its_uploaded_documents(client):+ _create_thread(client, "source")+ _add_message(client, "source", "m1")+ text = "victor whiskey xray " * 50+ source = _upload(client, "source", "source.txt", text)++ assert _fork(client, "source", "m1", "fork")["containerSnapshotWarning"] is None++ documents = _thread_documents(client, "fork")+ assert [(d["filename"], d["status"]) for d in documents] == [("source.txt", "completed")]+ copy = documents[0]["id"]+ assert copy != source+ assert documents[0]["numChunks"] == _thread_documents(client, "source")[0]["numChunks"]+ for mode in ("lexical", "dense"):+ hits = _search(client, "fork", "victor whiskey xray", mode)+ assert hits and {hit["documentId"] for hit in hits} == {copy}+ assert _stored_path(copy) != _stored_path(source)+ with open(_stored_path(copy), encoding = "utf-8") as copied:+ assert copied.read() == text+++def _vectors(document_id):+ conn = rag_db.get_connection()+ try:+ rows = conn.execute(+ "SELECT chunk_id, embedding FROM chunks_vec WHERE chunk_id LIKE ?",+ (f"{document_id}:%",),+ ).fetchall()+ return {row["chunk_id"].rsplit(":", 1)[1]: bytes(row["embedding"]) for row in rows}+ finally:+ conn.close()+++def test_a_fork_copies_each_documents_own_vectors(client):+ _create_thread(client, "source")+ _add_message(client, "source", "m1")+ sources = [+ _upload(client, "source", "short.txt", "kilo lima mike " * 50),+ _upload(client, "source", "long.txt", " ".join(f"november{i}" for i in range(3000))),+ ]+ _fork(client, "source", "m1", "fork")++ copies = {d["filename"]: d["id"] for d in _thread_documents(client, "fork")}+ assert len(_vectors(sources[1])) > 1+ for source, filename in zip(sources, ("short.txt", "long.txt")):+ assert _vectors(copies[filename]) == _vectors(source)+++def test_a_forks_documents_are_independent_of_the_source_thread(client):+ _create_thread(client, "source")+ _add_message(client, "source", "m1")+ _upload(client, "source", "source.txt", "yankee zulu alpha " * 50)+ _fork(client, "source", "m1", "fork")+ copy = _thread_documents(client, "fork")[0]["id"]+ copy_path = _stored_path(copy)++ _upload(client, "source", "later.txt", "bravo charlie delta " * 50)+ assert [d["id"] for d in _thread_documents(client, "fork")] == [copy]++ response = client.request("DELETE", "/api/chat/threads", json = {"ids": ["source"]})++ assert response.status_code == 200, response.text+ assert _document_ids(client) == {copy}+ assert os.path.isfile(copy_path)+ assert _search(client, "fork", "yankee zulu alpha", "dense")+++def test_a_fork_survives_documents_that_cannot_be_copied(client, monkeypatch):+ from core.rag import conversation_archive++ _create_thread(client, "source")+ _add_message(client, "source", "m1")+ source = _upload(client, "source", "source.txt", "echo foxtrot golf " * 50)++ def broken(conn, *args, **kwargs):+ conn.execute("INSERT INTO chunks_fts(text, chunk_id, scope) VALUES('x', 'x', 'x')")+ raise RuntimeError("disk full")++ monkeypatch.setattr(conversation_archive.store, "copy_documents", broken)+ before = set(os.listdir(os.path.dirname(_stored_path(source))))++ warning = _fork(client, "source", "m1", "fork")["containerSnapshotWarning"]++ assert "not copied" in (warning or "")++ assert _thread_documents(client, "fork") == []+ assert _document_ids(client) == {source}+ assert set(os.listdir(os.path.dirname(_stored_path(source)))) == before+ conn = rag_db.get_metadata_connection()+ try:+ assert conn.execute("SELECT COUNT(*) FROM chunks_fts WHERE scope='x'").fetchone()[0] == 0+ finally:+ conn.close()+++def test_a_fork_warns_when_existing_documents_cannot_load_without_vec(client, monkeypatch):+ _create_thread(client, "source")+ _add_message(client, "source", "m1")+ source = _upload(client, "source", "source.txt", "echo foxtrot golf " * 50)+ with monkeypatch.context() as unavailable:+ unavailable.setattr(rag_db, "rag_available", lambda: False)+ warning = _fork(client, "source", "m1", "fork")["containerSnapshotWarning"]+ assert "not copied" in (warning or "")+ assert _thread_documents(client, "fork") == []+ assert _document_ids(client) == {source}+++def test_a_fork_without_documents_needs_no_warning_without_vec(client, monkeypatch):+ _create_thread(client, "source")+ _add_message(client, "source", "m1")+ monkeypatch.setattr(rag_db, "rag_available", lambda: False)+ assert _fork(client, "source", "m1", "fork")["containerSnapshotWarning"] is None+++def _add_document(thread_id, status):+ conn = rag_db.get_connection()+ try:+ store.create_document(+ conn,+ scope = store.thread_scope(thread_id),+ thread_id = thread_id,+ filename = f"{status}.txt",+ sha256 = f"{status}-upload",+ status = status,+ )+ finally:+ conn.close()++[email protected]("status", ["pending", "running"])+def test_a_fork_warns_when_an_upload_is_still_indexing(client, status):+ _create_thread(client, "source")+ _add_message(client, "source", "m1")+ _add_document("source", status)+ warning = _fork(client, "source", "m1", "fork")["containerSnapshotWarning"]+ assert "not copied" in (warning or "")++[email protected]("vec", [True, False])+def test_a_fork_does_not_warn_about_failed_uploads(client, monkeypatch, vec):+ _create_thread(client, "source")+ _add_message(client, "source", "m1")+ _add_document("source", "failed")+ if not vec:+ monkeypatch.setattr(rag_db, "rag_available", lambda: False)+ assert _fork(client, "source", "m1", "fork")["containerSnapshotWarning"] is None+++def test_a_fork_copies_files_before_taking_the_rag_write_lock(client, monkeypatch):+ _create_thread(client, "source")+ _add_message(client, "source", "m1")+ for index in range(2):+ _upload(client, "source", f"source{index}.txt", f"echo foxtrot golf {index} " * 50)+ copy_upload = ingestion._copy_upload+ writable = []++ def copy_with_concurrent_writer(path):+ conn = rag_db.get_metadata_connection()+ try:+ conn.execute("PRAGMA busy_timeout = 20")+ conn.execute("BEGIN IMMEDIATE")+ writable.append(True)+ except sqlite3.OperationalError:+ writable.append(False)+ finally:+ conn.rollback()+ conn.close()+ return copy_upload(path)++ monkeypatch.setattr(ingestion, "_copy_upload", copy_with_concurrent_writer)+ assert _fork(client, "source", "m1", "fork")["containerSnapshotWarning"] is None+ assert writable == [True, True]+++def test_a_fork_warns_if_a_source_document_is_deleted_during_file_copy(client, monkeypatch):+ _create_thread(client, "source")+ _add_message(client, "source", "m1")+ source = _upload(client, "source", "source.txt", "echo foxtrot golf " * 50)+ copy_upload = ingestion._copy_upload++ def copy_then_delete(path):+ copied = copy_upload(path)+ response = client.delete(f"/api/rag/documents/{source}")+ assert response.status_code == 200, response.text+ return copied++ monkeypatch.setattr(ingestion, "_copy_upload", copy_then_delete)+ warning = _fork(client, "source", "m1", "fork")["containerSnapshotWarning"]+ assert "not copied" in (warning or "")+ assert _thread_documents(client, "fork") == []+++def test_deleting_a_fork_during_file_copy_does_not_leave_orphan_documents(client, monkeypatch):+ _create_thread(client, "source")+ _add_message(client, "source", "m1")+ source = _upload(client, "source", "source.txt", "echo foxtrot golf " * 50)+ upload_dir = os.path.dirname(_stored_path(source))+ before = set(os.listdir(upload_dir))+ copy_upload = ingestion._copy_upload++ def copy_then_delete_fork(path):+ copied = copy_upload(path)+ response = client.request("DELETE", "/api/chat/threads", json = {"ids": ["fork"]})+ assert response.status_code == 200, response.text+ return copied++ monkeypatch.setattr(ingestion, "_copy_upload", copy_then_delete_fork)+ _fork(client, "source", "m1", "fork")+ assert _document_ids(client) == {source}+ assert set(os.listdir(upload_dir)) == before+++def test_a_fork_warns_if_the_source_is_deleted_before_document_lookup(client, monkeypatch):+ from core.rag import conversation_archive++ _create_thread(client, "source")+ _add_message(client, "source", "m1")+ _upload(client, "source", "source.txt", "echo foxtrot golf " * 50)+ copy_documents = conversation_archive.copy_thread_documents++ def delete_then_copy(source_thread_id, thread_id):+ response = client.request("DELETE", "/api/chat/threads", json = {"ids": ["source"]})+ assert response.status_code == 200, response.text+ return copy_documents(source_thread_id, thread_id)++ monkeypatch.setattr(conversation_archive, "copy_thread_documents", delete_then_copy)+ warning = _fork(client, "source", "m1", "fork")["containerSnapshotWarning"]+ assert "not copied" in (warning or "")+ assert _thread_documents(client, "fork") == []+++def _cite(client, thread_id, message_id, document_id):+ sources = [+ {+ "citationId": 1,+ "chunkId": f"{document_id}:0",+ "documentId": document_id,+ "filename": "source.txt",+ }+ ]+ part = {+ "type": "tool-call",+ "toolCallId": "call-1",+ "toolName": "search_documents",+ "args": {"query": "hotel"},+ "result": "hotel india\n__RAG_SOURCES__:" + json.dumps(sources),+ }+ response = client.put(+ f"/api/chat/threads/{thread_id}/messages/{message_id}",+ json = {+ "id": message_id,+ "threadId": thread_id,+ "parentId": "m1",+ "role": "assistant",+ "content": [part],+ "metadata": {"custom": {"sources": sources}},+ "createdAt": 2,+ },+ )+ assert response.status_code == 200, response.text+++def _cited(message):+ result = message["content"][0]["result"]+ return json.loads(result.split("__RAG_SOURCES__:", 1)[1])[0]+++def test_a_forks_copied_messages_cite_the_forks_documents(client):+ _create_thread(client, "source")+ _add_message(client, "source", "m1")+ source = _upload(client, "source", "source.txt", "hotel india juliet " * 50)+ _cite(client, "source", "m2", source)++ forked = _fork(client, "source", "m2", "fork")++ copy = _thread_documents(client, "fork")[0]["id"]+ stored = client.get("/api/chat/threads/fork/messages").json()["messages"]+ for messages in (forked["messages"], stored):+ answer = [m for m in messages if m["role"] == "assistant"][0]+ cited = _cited(answer)+ assert (cited["documentId"], cited["chunkId"]) == (copy, f"{copy}:0")+ assert answer["metadata"]["custom"]["sources"][0]["documentId"] == copy+ parent = client.get("/api/chat/threads/source/messages/m2").json()+ assert (_cited(parent)["documentId"], _cited(parent)["chunkId"]) == (source, f"{source}:0")+ conn = studio_db.get_connection()+ try:+ assert conn.execute("SELECT dirty FROM chat_attachment_inventory_state").fetchone()[0] == 0+ finally:+ conn.close()