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 messages from another window when deleting in a stale one (#16926)
Nilay634b4f47e1e1
diff --git a/studio/backend/storage/studio_db.py b/studio/backend/storage/studio_db.pyindex b2e0125677f..71a4fa1f711 100644--- a/studio/backend/storage/studio_db.py+++ b/studio/backend/storage/studio_db.py@@ -4424,18 +4424,27 @@ def sync_chat_messages()# Computed before the upsert to tell forced relinks from edits; research ids are excluded since their delete is exempt, or the reseat skips a surviving parent.pruned: set = set()+ unseen_ids: set = set()if prune_missing:- pruned = (- {- str(row["id"])- for row in conn.execute(- "SELECT id FROM chat_messages WHERE thread_id = ?",- (thread_id,),- ).fetchall()- }- - requested_ids- - prune_protected_ids- )+ stored_rows = conn.execute(+ "SELECT id, parent_id, role FROM chat_messages WHERE thread_id = ?",+ (thread_id,),+ ).fetchall()+ unseen_ids = {str(row["id"]) for row in stored_rows} - requested_ids+ pruned = unseen_ids - prune_protected_ids+ if explicitly_deleted_ids:+ # Deleting a user message also deletes its replies, including ones this window never saw.+ deleted_user_ids = {+ str(row["id"]) for row in stored_rows if row["role"] == "user"+ } & explicitly_deleted_ids+ pruned &= explicitly_deleted_ids | (+ {+ str(row["id"])+ for row in stored_rows+ if row["role"] == "assistant" and row["parent_id"] in deleted_user_ids+ }+ - managed_ids+ )protected = generation_ids if allow_research_update else managed_idsmessages = [m@@ -4448,7 +4457,7 @@ def sync_chat_messages(]# Repair research parents the prune deletes, walking the stored chain (dangling parent breaks import).# Batch-written ids are excluded so an authorized reparent is not overwritten.- reseat_candidates = managed_ids - {str(m["id"]) for m in messages}+ reseat_candidates = (managed_ids | unseen_ids) - pruned - {str(m["id"]) for m in messages}reseat_parents = {message_id: _surviving_parent_id(conn, thread_id, message_id, pruned)for message_id, stored_parent in _parents_of(conn, thread_id, reseat_candidates).items()@@ -4522,15 +4531,7 @@ def sync_chat_messages(content_json,)if prune_missing:- existing_ids = {- row["id"]- for row in conn.execute(- "SELECT id FROM chat_messages WHERE thread_id = ?",- (thread_id,),- ).fetchall()- }- # Update permission is not delete permission: these ids are prune-exempt even for allow_research_update callers.- missing_ids = sorted(existing_ids - requested_ids - prune_protected_ids)+ missing_ids = sorted(pruned)for start in range(0, len(missing_ids), _SQLITE_IN_CHUNK_SIZE):chunk = missing_ids[start : start + _SQLITE_IN_CHUNK_SIZE]placeholders = ",".join("?" for _ in chunk)diff --git a/studio/backend/tests/test_chat_history_storage.py b/studio/backend/tests/test_chat_history_storage.pyindex adc344f4999..414451a8471 100644--- a/studio/backend/tests/test_chat_history_storage.py+++ b/studio/backend/tests/test_chat_history_storage.py@@ -432,6 +432,79 @@ def test_sync_chat_messages_prunes_when_requested(tmp_path, monkeypatch):assert [message["id"] for message in messages] == ["msg-2"]+def _turn(message_id: str, parent_id: str | None, created_at: int) -> dict:+ return _stored_message(+ id = message_id,+ threadId = "thread-1",+ parentId = parent_id,+ role = "assistant" if message_id.startswith("a") else "user",+ createdAt = created_at,+ )+++def test_a_stale_window_delete_keeps_turns_it_never_saw(tmp_path, monkeypatch):+ _reset_studio_db(tmp_path, monkeypatch)+ studio_db.upsert_chat_thread(_thread())+ seen = [_turn("u1", None, 1), _turn("a1", "u1", 2), _turn("u2", "a1", 3), _turn("a2", "u2", 4)]+ studio_db.sync_chat_messages("thread-1", seen)+ studio_db.sync_chat_messages("thread-1", seen + [_turn("u3", "a2", 5), _turn("a3", "u3", 6)])++ messages = studio_db.sync_chat_messages(+ "thread-1",+ seen[:2],+ prune_missing = True,+ deleted_message_ids = ["u2", "a2"],+ )++ parents = {message["id"]: message["parentId"] for message in messages}+ assert parents == {"u1": None, "a1": "u1", "u3": "a1", "a3": "u3"}+++ "other_window, seen_count, deleted",+ [+ ([_turn("a2b", "u2", 5)], 4, ["u2", "a2"]),+ ([], 3, ["u2"]),+ ],+)+def test_a_stale_window_delete_drops_replies_it_never_saw(+ tmp_path, monkeypatch, other_window, seen_count, deleted+):+ _reset_studio_db(tmp_path, monkeypatch)+ studio_db.upsert_chat_thread(_thread())+ turns = [_turn("u1", None, 1), _turn("a1", "u1", 2), _turn("u2", "a1", 3), _turn("a2", "u2", 4)]+ studio_db.sync_chat_messages("thread-1", turns + other_window)++ messages = studio_db.sync_chat_messages(+ "thread-1",+ [m for m in turns[:seen_count] if m["id"] not in deleted],+ prune_missing = True,+ deleted_message_ids = deleted,+ )++ parents = {message["id"]: message["parentId"] for message in messages}+ assert parents == {"u1": None, "a1": "u1"}+++def test_a_prune_does_not_walk_the_chain_of_rows_it_deletes(tmp_path, monkeypatch):+ _reset_studio_db(tmp_path, monkeypatch)+ studio_db.upsert_chat_thread(_thread())+ ids = [f"u{i}" if i % 2 == 0 else f"a{i}" for i in range(40)]+ turns = [_turn(m, ids[i - 1] if i else None, i) for i, m in enumerate(ids)]+ studio_db.sync_chat_messages("thread-1", turns)+ walked = []+ real = studio_db._surviving_parent_id+ monkeypatch.setattr(+ studio_db,+ "_surviving_parent_id",+ lambda conn, thread_id, message_id, pruned: walked.append(message_id)+ or real(conn, thread_id, message_id, pruned),+ )++ assert studio_db.sync_chat_messages("thread-1", [], prune_missing = True) == []+ assert walked == []++def test_upsert_chat_message_rejects_cross_thread_id_conflict(tmp_path, monkeypatch):_reset_studio_db(tmp_path, monkeypatch)studio_db.upsert_chat_thread(_thread("thread-1"))@@ -1226,6 +1299,23 @@ def test_deleting_a_mid_chain_ancestor_relinks_to_the_surviving_grandparent(tmp_assert next(m for m in synced if m["id"] == "prompt")["parentId"] == "a0"+def test_a_stale_window_delete_relinks_a_research_prompt_it_never_saw(tmp_path, monkeypatch):+ _, messages = _research_thread(tmp_path, monkeypatch, extra_ancestors = 2)++ synced = studio_db.sync_chat_messages(+ "src",+ _without(messages, "a1", "prompt", "report"),+ prune_missing = True,+ deleted_message_ids = ["a1"],+ )++ assert {m["id"]: m["parentId"] for m in synced} == {+ "a0": None,+ "prompt": "a0",+ "report": "prompt",+ }++def test_a_relink_to_a_surviving_message_that_is_not_the_ancestor_is_ignored(tmp_path, monkeypatch):# The bulk sync drops the claim instead of rejecting the batch; reseat uses the stored chain._, messages = _research_thread(tmp_path, monkeypatch, extra_ancestors = 3)