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: keep a Library file's star, folder and name after an edit (#12793)

Nilayc120fc8f280e
diff --git a/studio/backend/core/inference/tools.py b/studio/backend/core/inference/tools.py
index 9b2022c6629..3a14f6edf2e 100644
--- a/studio/backend/core/inference/tools.py
+++ b/studio/backend/core/inference/tools.py
@@ -12140,7 +12140,9 @@ def _edit_file_write(
"was being prepared; nothing was written. Read it again and "
"redo the edit against the current contents."
)
- os.replace(tmp, path)
+ from core import library
+
+ library.replace_file(tmp, path)
tmp = ""
except OSError as exc:
return f"Error: cannot write '{os.path.basename(path)}': {exc}"
diff --git a/studio/backend/core/library.py b/studio/backend/core/library.py
index 12fb64c1558..36b92654ebe 100644
--- a/studio/backend/core/library.py
+++ b/studio/backend/core/library.py
@@ -1162,6 +1162,7 @@ _SOURCES = (
def list_items() -> list[dict]:
"""Every item with its overlay applied, newest activity first. A failing source is skipped so
one broken store cannot empty the whole Library."""
+ generation = _LISTING.generation
overlay = library_db.list_entries()
items: list[dict] = []
for source in _SOURCES:
@@ -1191,7 +1192,12 @@ def list_items() -> list[dict]:
if entry and entry["name"]:
item["name"] = entry["name"]
try:
- library_db.reconcile_entries(adopt, stale)
+ if adopt or stale:
+ with _replace_lock:
+ # A listing begun before a replace_file swap may have seen its new inode before the carry: not stale.
+ if _LISTING.generation != generation:
+ stale = []
+ library_db.reconcile_entries(adopt, stale)
except Exception:
logger.warning("library.overlay_reconcile_failed", exc_info = True)
items.sort(key = lambda item: item["updatedAt"], reverse = True)
@@ -1216,6 +1222,49 @@ def fingerprint(item_id: str) -> Optional[str]:
return None
+_replace_lock = threading.Lock()
+
+
+def overlay_write():
+ """Held across a fingerprint check and the overlay write it guards, so an edit_file swap cannot
+ land between them and leave the write keyed to the file it replaced."""
+ with _replace_lock:
+ yield
+
+
+def replace_file(tmp: str, path: str) -> None:
+ """``os.replace(tmp, path)``, carrying the overlay row of the file it replaces over to the new
+ inode, which the listing would otherwise drop as a different file."""
+ with _replace_lock:
+ before = os.stat(path)
+ staged = os.stat(tmp)
+ os.replace(tmp, path)
+ _carry_overlay(path, before, staged)
+
+
+def _carry_overlay(path: str, before: os.stat_result, staged: os.stat_result) -> None:
+ old = _fingerprint(before)
+ try:
+ after = os.stat(path)
+ # A path made again since the swap is a different file, which starts fresh.
+ if not os.path.samestat(staged, after):
+ return
+ for item_id, entry in library_db.list_entries().items():
+ # NULL: a legacy row not yet adopted, which a listing racing this swap would adopt as the old inode.
+ if entry["fingerprint"] not in (old, None) or not item_id.startswith("sandbox:"):
+ continue
+ try:
+ listed = os.stat(_sandbox_path(item_id.partition(":")[2]))
+ except (LookupError, OSError):
+ continue
+ if os.path.samestat(listed, after):
+ library_db.carry_fingerprint(item_id, entry["fingerprint"], _fingerprint(after))
+ invalidate_listing()
+ except Exception:
+ logger.warning("library.overlay_carry_failed", exc_info = True)
+
+
def safe_file_name(
name: str,
fallback: str = "file",
@@ -2163,7 +2212,7 @@ _overlay_lock = threading.Lock()
def mark_opened(item_id: str) -> bool:
"""Record that the item was just opened, kept for the file it is now. False when it is not
there, or is a path whose file is gone."""
- with _overlay_lock:
+ with _overlay_lock, overlay_write():
found = fingerprint(item_id)
if found is None and (path_derived(item_id) or not item_exists(item_id)):
return False
diff --git a/studio/backend/routes/library.py b/studio/backend/routes/library.py
index 870bca176cb..98509174f05 100644
--- a/studio/backend/routes/library.py
+++ b/studio/backend/routes/library.py
@@ -195,20 +195,21 @@ def _still_there(item_id: str, fingerprint: Optional[str]) -> bool:
@router.patch("/items")
def patch_item(body: ItemPatch, current_subject: str = Depends(get_current_subject)) -> dict:
- fingerprint = library.fingerprint(body.id)
- if fingerprint is None and library.path_derived(body.id):
- raise HTTPException(status_code = 404, detail = "Item not found")
- try:
- library_db.update_entry(
- body.id,
- name = body.name.strip() if body.name else None,
- favorite = body.favorite,
- folder_id = body.folderId,
- move = "folderId" in body.model_fields_set,
- fingerprint = fingerprint,
- )
- except KeyError:
- raise HTTPException(status_code = 404, detail = "Folder not found")
+ with library.overlay_write():
+ fingerprint = library.fingerprint(body.id)
+ if fingerprint is None and library.path_derived(body.id):
+ raise HTTPException(status_code = 404, detail = "Item not found")
+ try:
+ library_db.update_entry(
+ body.id,
+ name = body.name.strip() if body.name else None,
+ favorite = body.favorite,
+ folder_id = body.folderId,
+ move = "folderId" in body.model_fields_set,
+ fingerprint = fingerprint,
+ )
+ except KeyError:
+ raise HTTPException(status_code = 404, detail = "Folder not found")
return {"ok": True}
diff --git a/studio/backend/storage/library_db.py b/studio/backend/storage/library_db.py
index bfa4381ce2b..6c88d7b30a7 100644
--- a/studio/backend/storage/library_db.py
+++ b/studio/backend/storage/library_db.py
@@ -358,6 +358,18 @@ def reconcile_entries(adopt: list[tuple[str, str]], stale: list[tuple[str, str]]
conn.close()
+def carry_fingerprint(item_id: str, old: Optional[str], new: str) -> None:
+ conn = get_connection()
+ try:
+ conn.execute(
+ "UPDATE library_entries SET fingerprint = ? WHERE item_id = ? AND fingerprint IS ?",
+ (new, item_id, old),
+ )
+ conn.commit()
+ finally:
+ conn.close()
+
+
def delete_entry(item_id: str) -> None:
conn = get_connection()
try:
diff --git a/studio/backend/tests/test_library_routes.py b/studio/backend/tests/test_library_routes.py
index 93f2f0b5f71..c20b7268236 100644
--- a/studio/backend/tests/test_library_routes.py
+++ b/studio/backend/tests/test_library_routes.py
@@ -408,6 +408,153 @@ def test_a_sandbox_file_that_is_gone_takes_no_rename_a_later_file_would_inherit(
assert _items(client)[0][_SANDBOX_A]["name"] == "a.txt"
+def test_a_sandbox_file_the_model_edits_keeps_its_name_star_and_folder(
+ client, signed_in, monkeypatch
+):
+ from core.inference.tools import execute_tool
+
+ monkeypatch.setattr(library, "_SOURCES", (library._sandbox_items,))
+ _directory, path = _sandbox_chat("report.txt", b"teh report\n")
+ folder = _folder(client, "Reports", None)
+ _patch(client, id = _SANDBOX_ID, name = "Q3 report", favorite = True, folderId = folder)
+ result = execute_tool(
+ "edit_file",
+ {"path": "report.txt", "edits": [{"old_string": "teh", "new_string": "the"}]},
+ session_id = "t-lib",
+ )
+ assert Path(path).read_bytes() == b"the report\n", result
+ library.invalidate_listing()
+ item = _items(client)[0][_SANDBOX_ID]
+ assert (item["name"], item["favorite"], item["folderId"]) == ("Q3 report", True, folder)
+ assert _favorites(client) == [_SANDBOX_ID]
+
+
+def test_a_listing_during_an_edit_does_not_drop_the_files_name_star_and_folder(
+ client, signed_in, monkeypatch
+):
+ import contextvars
+ import threading
+
+ from core.inference.tools import execute_tool
+
+ monkeypatch.setattr(library, "_SOURCES", (library._sandbox_items,))
+ _directory, path = _sandbox_chat("report.txt", b"teh report\n")
+ _patch(client, id = _SANDBOX_ID, name = "Q3 report", favorite = True)
+ carry = library_db.carry_fingerprint
+ listings = []
+
+ def list_then_carry(*args):
+ library.invalidate_listing()
+ listing = threading.Thread(
+ target = contextvars.copy_context().run, args = (library.list_items,)
+ )
+ listing.start()
+ listing.join(timeout = 1)
+ listings.append(listing)
+ carry(*args)
+
+ monkeypatch.setattr(library_db, "carry_fingerprint", list_then_carry)
+ execute_tool(
+ "edit_file",
+ {"path": "report.txt", "edits": [{"old_string": "teh", "new_string": "the"}]},
+ session_id = "t-lib",
+ )
+ for listing in listings:
+ listing.join(timeout = 10)
+ assert Path(path).read_bytes() == b"the report\n"
+ library.invalidate_listing()
+ item = _items(client)[0][_SANDBOX_ID]
+ assert (item["name"], item["favorite"]) == ("Q3 report", True)
+
+
+def test_a_folder_move_during_an_edit_keeps_the_files_name_and_star(client, signed_in, monkeypatch):
+ import threading
+
+ from core.inference.tools import execute_tool
+
+ monkeypatch.setattr(library, "_SOURCES", (library._sandbox_items,))
+ _sandbox_chat("report.txt", b"teh report\n")
+ _patch(client, id = _SANDBOX_ID, name = "Q3 report", favorite = True)
+ folder = _folder(client, "Reports", None)
+ carry = library_db.carry_fingerprint
+ moves = []
+
+ def move_then_carry(*args):
+ move = threading.Thread(
+ target = _patch, args = (client,), kwargs = {"id": _SANDBOX_ID, "folderId": folder}
+ )
+ move.start()
+ move.join(timeout = 1)
+ moves.append(move)
+ carry(*args)
+
+ monkeypatch.setattr(library_db, "carry_fingerprint", move_then_carry)
+ execute_tool(
+ "edit_file",
+ {"path": "report.txt", "edits": [{"old_string": "teh", "new_string": "the"}]},
+ session_id = "t-lib",
+ )
+ for move in moves:
+ move.join(timeout = 10)
+ library.invalidate_listing()
+ item = _items(client)[0][_SANDBOX_ID]
+ assert (item["name"], item["favorite"], item["folderId"]) == ("Q3 report", True, folder)
+
+
+def test_a_legacy_row_keeps_its_name_and_star_through_an_edit_a_cached_listing_saw(
+ client, signed_in, monkeypatch
+):
+ from core.inference.tools import execute_tool
+
+ monkeypatch.setattr(library, "_SOURCES", (library._remembered("_sandbox_items", 60.0),))
+ _sandbox_chat("report.txt", b"teh report\n")
+ _patch(client, id = _SANDBOX_ID, name = "Q3 report", favorite = True)
+ _items(client)
+ conn = library_db.get_connection()
+ conn.execute("UPDATE library_entries SET fingerprint = NULL WHERE item_id = ?", (_SANDBOX_ID,))
+ conn.commit()
+ conn.close()
+ execute_tool(
+ "edit_file",
+ {"path": "report.txt", "edits": [{"old_string": "teh", "new_string": "the"}]},
+ session_id = "t-lib",
+ )
+ _items(client)
+ library.invalidate_listing()
+ item = _items(client)[0][_SANDBOX_ID]
+ assert (item["name"], item["favorite"]) == ("Q3 report", True)
+
+
+def test_a_path_made_again_right_after_an_edit_starts_fresh(client, signed_in, monkeypatch):
+ from core.inference.tools import execute_tool
+
+ monkeypatch.setattr(library, "_SOURCES", (library._sandbox_items,))
+ directory, path = _sandbox_chat("report.txt", b"teh report\n")
+ _patch(client, id = _SANDBOX_ID, name = "Q3 report", favorite = True)
+ # Linux keeps no birth time, so a recreated file reusing a freed inode passes for it: keep both allocated.
+ os.link(path, os.path.join(directory, ".held"))
+ replace = os.replace
+
+ def replace_then_recreate(src, dst):
+ replace(src, dst)
+ if os.path.basename(dst) == "report.txt":
+ other = dst + ".other"
+ Path(other).write_bytes(b"someone else\n")
+ replace(other, dst)
+
+ monkeypatch.setattr(os, "replace", replace_then_recreate)
+ execute_tool(
+ "edit_file",
+ {"path": "report.txt", "edits": [{"old_string": "teh", "new_string": "the"}]},
+ session_id = "t-lib",
+ )
+ monkeypatch.setattr(os, "replace", replace)
+ assert Path(path).read_bytes() == b"someone else\n"
+ library.invalidate_listing()
+ item = _items(client)[0][_SANDBOX_ID]
+ assert (item["name"], item["favorite"]) == ("report.txt", False)
+
+
def test_a_sandbox_delete_takes_only_the_file_it_was_listed_as(client, signed_in, monkeypatch):
monkeypatch.setattr(library, "_SOURCES", (library._sandbox_items,))
_directory, path = _sandbox_chat("a.txt", b"listed")