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: allow editing a refreshed reply when the tab Date disagrees (#15087)

Sourav Rajvi21088a45d461
diff --git a/studio/backend/storage/studio_db.py b/studio/backend/storage/studio_db.py
index cbb2aa2ffc9..675e5e9b2a7 100644
--- a/studio/backend/storage/studio_db.py
+++ b/studio/backend/storage/studio_db.py
@@ -3908,32 +3908,42 @@ def _settle_handed_off_generation(conn: sqlite3.Connection, message: dict) -> di
def _detach_terminal_generation_for_edit(
conn: sqlite3.Connection, thread_id: str, message: dict
) -> bool:
- row = conn.execute(
- """SELECT r.id AS run_id, r.status AS run_status,
- m.parent_id, m.role, m.metadata_json, m.attachments_json, m.created_at
- FROM chat_generation_runs r
- JOIN chat_messages m ON m.id = r.assistant_message_id
- WHERE r.thread_id = ? AND r.assistant_message_id = ?""",
+ stored = conn.execute(
+ """SELECT parent_id, role, metadata_json, attachments_json
+ FROM chat_messages WHERE thread_id = ? AND id = ?""",
(thread_id, str(message["id"])),
).fetchone()
+ if stored is None:
+ return False
+ stored_metadata = json.loads(stored["metadata_json"]) if stored["metadata_json"] else {}
+ run_id = stored_metadata.get("generationRunId") if isinstance(stored_metadata, dict) else None
+ if not isinstance(run_id, str):
+ return False
+ # Bind by the stamped run id. Refresh/Reload inserts a new run for a new assistant
+ # id, but create_run stamps created_at itself, so a tab that kept its own Date would
+ # fail a createdAt match (#15004). The run row is the authority.
+ row = conn.execute(
+ """SELECT id AS run_id, status AS run_status
+ FROM chat_generation_runs
+ WHERE id = ? AND thread_id = ? AND assistant_message_id = ?""",
+ (run_id, thread_id, str(message["id"])),
+ ).fetchone()
if row is None or str(row["run_status"]) not in _GENERATION_TERMINAL_STATUSES:
return False
- stored_metadata = json.loads(row["metadata_json"]) if row["metadata_json"] else {}
incoming_metadata = message.get("metadata")
# Not generationSettled: a settle whose `incomplete` disagrees with the producer's is refused,
# so a finished reply would never become editable (#13938). The run status above decides.
if (
- not isinstance(stored_metadata, dict)
- or stored_metadata.get("serverManaged") is not True
- or stored_metadata.get("generationRunId") != row["run_id"]
+ stored_metadata.get("serverManaged") is not True
or isinstance(incoming_metadata, dict)
and incoming_metadata.get("serverManaged") is True
- or str(message.get("role")) != str(row["role"])
- or (message.get("parentId") or None) != (row["parent_id"] or None)
- or int(message.get("createdAt", row["created_at"])) != int(row["created_at"])
+ or str(message.get("role")) != str(stored["role"])
+ or (message.get("parentId") or None) != (stored["parent_id"] or None)
):
return False
- stored_attachments = json.loads(row["attachments_json"]) if row["attachments_json"] else None
+ stored_attachments = (
+ json.loads(stored["attachments_json"]) if stored["attachments_json"] else None
+ )
if json.dumps(message.get("attachments"), sort_keys = True) != json.dumps(
stored_attachments, sort_keys = True
):
diff --git a/studio/backend/tests/test_chat_generation_runs.py b/studio/backend/tests/test_chat_generation_runs.py
index 84e9fdb0cb6..dcf029bce44 100644
--- a/studio/backend/tests/test_chat_generation_runs.py
+++ b/studio/backend/tests/test_chat_generation_runs.py
@@ -464,6 +464,25 @@ def test_running_generation_still_cannot_be_explicitly_edited(chat_home):
assert runs_db.get_run("run-1", "alice")["status"] == "running"
+def test_refreshed_generation_can_be_edited_when_tab_created_at_differs(chat_home):
+ """#15004: Refresh/Reload lets create_run insert the assistant row with its own
+ created_at. The tab keeps the Date it minted for the bubble, so an edit must still
+ detach the finished run."""
+ _create()
+ token = runs_db.get_worker_token("run-1")
+ assert runs_db.mark_running("run-1", token)
+ assert runs_db.finish_run("run-1", worker_token = token, status = "completed", finish_reason = "stop")
+ stored = studio_db.get_chat_message("thread-1", "assistant-1")
+ assert stored["createdAt"] != 1
+ edited = {key: value for key, value in stored.items() if key != "metadata"}
+ edited["content"] = [{"type": "text", "text": "edited after refresh"}]
+ edited["createdAt"] = 1
+ edited["metadata"] = {"incomplete": {"reason": "stop"}}
+ saved = studio_db.upsert_chat_message(edited, allow_generation_edit = True)
+ assert saved["content"] == [{"type": "text", "text": "edited after refresh"}]
+ assert runs_db.get_run("run-1", "alice") is None
+
+
def test_batched_events_have_gapless_cursor_and_terminal_flush(chat_home):
run, _created = _create()
worker_token = runs_db.get_worker_token("run-1")