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: stop re-hashing kept spills and re-reading the record on every prune (#15884)

Daniel Han9e2da971e3c9
diff --git a/studio/backend/core/inference/tools.py b/studio/backend/core/inference/tools.py
index 6014c52e70b..03f8f70765a 100644
--- a/studio/backend/core/inference/tools.py
+++ b/studio/backend/core/inference/tools.py
@@ -20871,18 +20871,20 @@ def _spill_full_output(
return None, True
-def _spill_files(root: str, directory: str, owned: "set[str]") -> "list[str]":
- """The spills in one directory: the ones ``owned`` records this process having written.
- Everything else there is the user's, whatever it is called. Links are never spills."""
+def _spill_files(root: str, directory: str, owned: "dict[str, tuple[str, str]]") -> "list[str]":
+ """The spills in one directory, by stamp only: hashing every kept spill on each prune costs up to
+ the whole byte cap, and `_unlink_verified_spill` checks the digest before anything is deleted."""
try:
names = os.listdir(directory)
except OSError:
return []
- return [
- path
- for path in (os.path.join(directory, name) for name in names)
- if _is_recorded_spill(root, path, owned)
- ]
+ spills = []
+ for name in names:
+ path = os.path.join(directory, name)
+ recorded = owned.get(os.path.relpath(path, root).replace(os.sep, "/"))
+ if recorded is not None and recorded[0] == _spill_stamp(path):
+ spills.append(path)
+ return spills
def _unlink_verified_spill(root: str, path: str, owned: "dict[str, tuple[str, str]]") -> bool:
@@ -20990,6 +20992,9 @@ def _prune_spills_locked(target_dir: str, root: str) -> None:
if _unlink_verified_spill(root, path, owned):
removed.add(path)
# Manifest follows the files: a name left after its file is gone would still count as owned.
+ # Sandbox removal drops the record without this lock; a recreated directory must not inherit it.
+ if not os.path.exists(_spill_record_path(root)):
+ identity = None
_write_spill_manifest(
root,
{
@@ -20997,6 +21002,7 @@ def _prune_spills_locked(target_dir: str, root: str) -> None:
for name, stamp in owned.items()
if os.path.join(root, *name.split("/")) not in removed
},
+ identity = identity,
)
# Remove emptied scopes so cleanup does not see the sandbox as non-empty.
for name in sorted(os.listdir(root)):
diff --git a/studio/backend/tests/test_tool_result_fits_window.py b/studio/backend/tests/test_tool_result_fits_window.py
index 3993fb28d08..9d6783618aa 100644
--- a/studio/backend/tests/test_tool_result_fits_window.py
+++ b/studio/backend/tests/test_tool_result_fits_window.py
@@ -2130,3 +2130,78 @@ class TestTheResultIsFittedAsItIsReplayed:
# At most: the head stops on a line boundary.
assert len(head) <= int(re.match(r"(\d+) chars", notice).group(1)), len(head)
assert head == tools._defuse_sentinels(head)
+
+
+class TestAPruneReadsTheRecordOnce:
+ def test_the_rewrite_reuses_the_identity_it_already_read(self, tmp_path, monkeypatch):
+ for n in range(tools._SPILL_KEEP + 5):
+ tools._truncate(f"run {n}\n" + _dense(3_000), 200, workdir = str(tmp_path))
+ root = tmp_path / tools._SPILL_DIR
+ record = tools._spill_record_path(str(root))
+ before = open(record, encoding = "utf-8").read().splitlines()[0]
+ reads = []
+ real = tools._spill_record
+
+ def _counting(path):
+ reads.append(path)
+ return real(path)
+
+ monkeypatch.setattr(tools, "_spill_record", _counting)
+ monkeypatch.setattr(tools, "_SPILL_KEEP", 5)
+
+ tools._prune_spills(str(root))
+
+ assert len(reads) == 1, reads
+ assert open(record, encoding = "utf-8").read().splitlines()[0] == before
+ assert len(_spills(root)) == 5
+
+ def test_a_record_forgotten_during_the_prune_does_not_get_its_identity_back(
+ self, tmp_path, monkeypatch
+ ):
+ """Sandbox removal drops the record without the spill lock; a recreated directory must not inherit it."""
+ for n in range(tools._SPILL_KEEP + 5):
+ tools._truncate(f"run {n}\n" + _dense(3_000), 200, workdir = str(tmp_path))
+ root = tmp_path / tools._SPILL_DIR
+ record = tools._spill_record_path(str(root))
+ real = tools._unlink_verified_spill
+
+ def _forget_first(*args, **kwargs):
+ tools._forget_spill_record(record)
+ return real(*args, **kwargs)
+
+ monkeypatch.setattr(tools, "_unlink_verified_spill", _forget_first)
+ monkeypatch.setattr(tools, "_SPILL_KEEP", 5)
+
+ tools._prune_spills(str(root))
+
+ assert tools._spill_record(str(root))[0] is None
+
+ def test_kept_spills_are_not_hashed_again(self, tmp_path, monkeypatch):
+ for n in range(tools._SPILL_KEEP):
+ tools._truncate(f"run {n}\n" + _dense(3_000), 200, workdir = str(tmp_path))
+ root = tmp_path / tools._SPILL_DIR
+ hashed = []
+ real = tools._file_digest
+ monkeypatch.setattr(
+ tools, "_file_digest", lambda *a, **k: hashed.append(a) or real(*a, **k)
+ )
+
+ tools._prune_spills(str(root))
+
+ assert hashed == []
+ assert len(_spills(root)) == tools._SPILL_KEEP
+
+ def test_a_spill_whose_content_no_longer_matches_is_not_deleted(self, tmp_path, monkeypatch):
+ for n in range(tools._SPILL_KEEP + 1):
+ tools._truncate(f"run {n}\n" + _dense(3_000), 200, workdir = str(tmp_path))
+ root = tmp_path / tools._SPILL_DIR
+ identity, owned = tools._spill_record(str(root))
+ oldest = min(_spills(root), key = os.path.getmtime)
+ name = os.path.relpath(oldest, root).replace(os.sep, "/")
+ owned[name] = (owned[name][0], "0" * 64)
+ tools._write_spill_manifest(str(root), owned, identity = identity)
+ monkeypatch.setattr(tools, "_SPILL_KEEP", tools._SPILL_KEEP - 1)
+
+ tools._prune_spills(str(root))
+
+ assert os.path.exists(oldest)