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: index FK child columns, partial index for the run lease sweep, keep the inventory clean on Deep Research start (#16795)

Daniel Han252af165b09f
diff --git a/studio/backend/storage/research_runs_db.py b/studio/backend/storage/research_runs_db.py
index 81bec927c44..20614e96974 100644
--- a/studio/backend/storage/research_runs_db.py
+++ b/studio/backend/storage/research_runs_db.py
@@ -158,6 +158,13 @@ def _bind_assistant_locked(
"serverManaged": True,
}
if message is None:
+ # An empty placeholder adds no attachment entries; keep a clean inventory clean (#14217).
+ inventory_was_clean = (
+ conn.execute(
+ "SELECT 1 FROM chat_attachment_inventory_state WHERE singleton=1 AND dirty=0"
+ ).fetchone()
+ is not None
+ )
conn.execute(
"""INSERT INTO chat_messages
(id, thread_id, parent_id, role, content_json, metadata_json, created_at)
@@ -170,6 +177,8 @@ def _bind_assistant_locked(
created,
),
)
+ if inventory_was_clean:
+ conn.execute("UPDATE chat_attachment_inventory_state SET dirty=0 WHERE singleton=1")
conn.execute(
"UPDATE chat_threads SET updated_at=MAX(COALESCE(updated_at, created_at), ?) WHERE id=?",
(created, thread_id),
diff --git a/studio/backend/storage/studio_db.py b/studio/backend/storage/studio_db.py
index 63e5f9e9038..b2e0125677f 100644
--- a/studio/backend/storage/studio_db.py
+++ b/studio/backend/storage/studio_db.py
@@ -922,6 +922,9 @@ def _ensure_schema(conn: sqlite3.Connection) -> None:
"CREATE INDEX IF NOT EXISTS idx_scheduled_task_runs_finished"
" ON scheduled_task_runs(finished_at)"
)
+ conn.execute(
+ "CREATE INDEX IF NOT EXISTS idx_scheduled_task_runs_thread ON scheduled_task_runs(thread_id)"
+ )
conn.execute(
"""
CREATE TABLE IF NOT EXISTS research_runs (
@@ -1093,6 +1096,15 @@ def _ensure_schema(conn: sqlite3.Connection) -> None:
"CREATE INDEX IF NOT EXISTS idx_research_runs_lease "
"ON research_runs(status, lease_expires_at)"
)
+ conn.execute("CREATE INDEX IF NOT EXISTS idx_research_runs_thread ON research_runs(thread_id)")
+ conn.execute(
+ "CREATE INDEX IF NOT EXISTS idx_research_runs_user_message "
+ "ON research_runs(user_message_id)"
+ )
+ conn.execute(
+ "CREATE INDEX IF NOT EXISTS idx_research_runs_assistant_message "
+ "ON research_runs(assistant_message_id)"
+ )
conn.execute(
"CREATE INDEX IF NOT EXISTS idx_research_sources_run ON research_sources(run_id, id)"
)
@@ -1155,6 +1167,20 @@ def _ensure_schema(conn: sqlite3.Connection) -> None:
"CREATE INDEX IF NOT EXISTS idx_chat_generation_runs_thread_status "
"ON chat_generation_runs(thread_id, status)"
)
+ conn.execute(
+ "CREATE INDEX IF NOT EXISTS idx_chat_generation_runs_user_message "
+ "ON chat_generation_runs(user_message_id)"
+ )
+ conn.execute(
+ "CREATE INDEX IF NOT EXISTS idx_chat_generation_runs_assistant_message "
+ "ON chat_generation_runs(assistant_message_id)"
+ )
+ # Partial on the reconcile sweep's exact status list, so the periodic sweep skips terminal rows.
+ conn.execute(
+ "CREATE INDEX IF NOT EXISTS idx_chat_generation_runs_active "
+ "ON chat_generation_runs(created_at, id) "
+ "WHERE status IN ('queued','running','cancelling')"
+ )
conn.execute(
"""
CREATE TRIGGER IF NOT EXISTS reject_second_active_chat_generation_insert
diff --git a/studio/backend/tests/test_studio_db_hot_path_indexes.py b/studio/backend/tests/test_studio_db_hot_path_indexes.py
new file mode 100644
index 00000000000..f617167db22
--- /dev/null
+++ b/studio/backend/tests/test_studio_db_hot_path_indexes.py
@@ -0,0 +1,136 @@
+# 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 pytest
+
+from storage import chat_generation_runs_db as runs
+from storage import research_runs_db as research
+from storage import studio_db as db
+
+PNG = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+aS1sAAAAASUVORK5CYII="
+
+
[email protected](autouse = True)
+def studio_home(tmp_path, monkeypatch):
+ monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(tmp_path))
+ db.close_wal_keeper()
+ db.reset_schema_state_for_tests()
+ runs.reset_schema_state_for_tests()
+ for name in ("history", "new"):
+ db.upsert_chat_thread({"id": name, "modelType": "gguf", "createdAt": 1})
+ db.upsert_chat_message(
+ {
+ "id": "image",
+ "threadId": "history",
+ "role": "user",
+ "content": [{"type": "image", "image": PNG}],
+ "createdAt": 1,
+ }
+ )
+ db.upsert_chat_message(
+ {
+ "id": "user",
+ "threadId": "new",
+ "role": "user",
+ "content": [{"type": "text", "text": "hello"}],
+ "createdAt": 1,
+ }
+ )
+ yield
+ db.close_wal_keeper()
+
+
+def _query(statement, parameters = ()):
+ conn = db.get_connection()
+ try:
+ return conn.execute(statement, parameters).fetchall()
+ finally:
+ conn.close()
+
+
+def test_every_foreign_key_child_column_leads_an_index():
+ conn = db.get_connection()
+ try:
+ tables = [
+ row[0]
+ for row in conn.execute("SELECT name FROM sqlite_master WHERE type='table'").fetchall()
+ ]
+ missing = []
+ for table in tables:
+ leading = {
+ conn.execute(f"PRAGMA index_info('{index[1]}')").fetchone()[2]
+ for index in conn.execute(f"PRAGMA index_list('{table}')").fetchall()
+ }
+ for fk in conn.execute(f"PRAGMA foreign_key_list('{table}')").fetchall():
+ if fk[3] not in leading:
+ missing.append(f"{table}.{fk[3]}")
+ finally:
+ conn.close()
+ assert missing == [], f"cascading deletes full-scan these tables: {missing}"
+
+
+def test_schema_is_idempotent():
+ before = _query("SELECT type, name, sql FROM sqlite_master ORDER BY type, name")
+ conn = db.get_connection()
+ try:
+ db._ensure_schema(conn)
+ conn.commit()
+ finally:
+ conn.close()
+ assert _query("SELECT type, name, sql FROM sqlite_master ORDER BY type, name") == before
+
+
+def test_lease_sweep_reads_only_active_runs():
+ runs.reconcile_runs(stale_after_ms = 60_000)
+ plan = _query(
+ "EXPLAIN QUERY PLAN SELECT id, status, cancel_requested FROM chat_generation_runs "
+ "WHERE status IN ('queued','running','cancelling') "
+ "AND COALESCE(progress_at, started_at, created_at) <= ? ORDER BY created_at, id",
+ (0,),
+ )
+ detail = " ".join(str(row[3]) for row in plan)
+ assert "idx_chat_generation_runs_active" in detail
+ assert "TEMP B-TREE" not in detail
+
+
+def test_research_placeholder_keeps_a_clean_inventory_clean(monkeypatch):
+ before = db.list_chat_attachments_page()[0]
+ calls = []
+ real = db._rebuild_chat_attachment_inventory
+ monkeypatch.setattr(
+ db, "_rebuild_chat_attachment_inventory", lambda conn: calls.append(True) or real(conn)
+ )
+ research.create_run(
+ run_id = "run-1",
+ owner_subject = "alice",
+ thread_id = "new",
+ user_message_id = "user",
+ assistant_message_id = "assistant",
+ config = {"model": "local-model", "inferenceRequest": {"model": "local-model"}},
+ )
+ state = _query("SELECT dirty FROM chat_attachment_inventory_state WHERE singleton=1")
+ assert state[0][0] == 0
+ db.sync_chat_messages("new", db.list_chat_messages("new"))
+ assert db.list_chat_attachments_page()[0] == before
+ assert calls == [], "starting Deep Research caused a full history rebuild"
+
+
+def test_research_placeholder_keeps_a_dirty_inventory_dirty():
+ db.list_chat_attachments_page()
+ conn = db.get_connection()
+ try:
+ conn.execute("UPDATE chat_attachment_inventory_state SET dirty=1 WHERE singleton=1")
+ conn.commit()
+ finally:
+ conn.close()
+ research.create_run(
+ run_id = "run-1",
+ owner_subject = "alice",
+ thread_id = "new",
+ user_message_id = "user",
+ assistant_message_id = "assistant",
+ config = {"model": "local-model", "inferenceRequest": {"model": "local-model"}},
+ )
+ state = _query("SELECT dirty FROM chat_attachment_inventory_state WHERE singleton=1")
+ assert state[0][0] == 1
+ assert [item["messageId"] for item in db.list_chat_attachments_page()[0]] == ["image"]