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: open linked-folder sources in binary mode on windows (#8621)
Maheswar Kumarbb67053381e1
diff --git a/studio/backend/core/rag/folder_sync.py b/studio/backend/core/rag/folder_sync.pyindex 5bf695e5305..4941ba6fdf9 100644--- a/studio/backend/core/rag/folder_sync.py+++ b/studio/backend/core/rag/folder_sync.py@@ -979,6 +979,19 @@ def _scan(if os.path.splitext(entry.name)[1].lower() not in config.UPLOAD_EXTS:continuest = entry.stat(follow_symlinks = False)+ from_path = False+ if st.st_ino in (None, 0):+ # Windows builds DirEntry from FindFirstFileW, which carries no file index, so+ # DirEntry.stat leaves st_dev/st_ino at 0. os.lstat opens a handle and fills+ # both; without it the stored identity is (0, 0) forever and a replacement of+ # identical size and mtime never reaches the reconciliation work list.+ try:+ st = os.lstat(full)+ from_path = True+ except OSError:+ # A file that vanished mid-scan must still reach _snapshot as a failure+ # rather than abort the scan, which is never authoritative for deletion.+ passrel = os.path.relpath(full, root).replace(os.sep, "/")found[rel] = {"path": full,@@ -986,6 +999,9 @@ def _scan("mtime_ns": st.st_mtime_ns,"device": st.st_dev,"inode": st.st_ino,+ # A recovered identity is comparable to the next scan's, but not to os.fstat's:+ # shared-folder and WebDAV drivers report different ids for the two call paths.+ "identity_from_path": from_path,}if config.FOLDER_MAX_FILES and len(found) > config.FOLDER_MAX_FILES:raise RuntimeError(@@ -1001,7 +1017,8 @@ def _snapshot(root: str, metadata: dict) -> str:resolved = os.path.realpath(source)if not _is_within(root, resolved):raise RuntimeError("File escaped the linked folder")- flags = os.O_RDONLY | getattr(os, "O_NOFOLLOW", 0)+ # os.fdopen already forces this descriptor binary on Windows; O_BINARY only guards a raw os.read.+ flags = os.O_RDONLY | getattr(os, "O_NOFOLLOW", 0) | getattr(os, "O_BINARY", 0)fd = os.open(source, flags)target = Nonetry:@@ -1017,14 +1034,21 @@ def _snapshot(root: str, metadata: dict) -> str:metadata["inode"],)actual = (before.st_size, before.st_mtime_ns, before.st_dev, before.st_ino)- if actual != expected:+ # Only an identity os.fstat can reproduce is comparable here: os.scandir reports none at all+ # on Windows, and the os.lstat _scan falls back to disagrees with os.fstat on file systems+ # without stable file ids. Size and mtime still gate those, and the post-copy check below is+ # fstat to fstat either way.+ usable = metadata["inode"] not in (None, 0) and not metadata.get("identity_from_path")+ compared = 4 if usable else 2+ if actual[:compared] != expected[:compared]:raise RuntimeError("Linked source changed during reconciliation")if config.MAX_UPLOAD_BYTES and before.st_size > config.MAX_UPLOAD_BYTES:raise RuntimeError("Linked source exceeds the RAG file size limit")with os.fdopen(fd, "rb", closefd = False) as src, open(target, "xb") as dst:_copy_exact(src, dst, before.st_size)after = os.fstat(fd)- if (after.st_size, after.st_mtime_ns, after.st_dev, after.st_ino) != expected:+ # Both sides are fstat here, so the identity is always comparable.+ if (after.st_size, after.st_mtime_ns, after.st_dev, after.st_ino) != actual:raise RuntimeError("Linked source changed while it was copied")return str(target)except Exception:diff --git a/studio/backend/tests/test_rag_linked_folders.py b/studio/backend/tests/test_rag_linked_folders.pyindex 65aed89a51a..4692c9fb3e6 100644--- a/studio/backend/tests/test_rag_linked_folders.py+++ b/studio/backend/tests/test_rag_linked_folders.py@@ -11,7 +11,7 @@ import osimport sqlite3import threadingimport time-from contextlib import closing+from contextlib import closing, contextmanagerfrom pathlib import Pathfrom types import SimpleNamespace@@ -855,6 +855,112 @@ def test_snapshot_copy_is_bounded_to_the_validated_size():assert target.getvalue() == b"validated"+def _snapshot_metadata(document: Path, **overrides) -> dict:+ stats = os.stat(document)+ return {+ "path": str(document),+ "size_bytes": stats.st_size,+ "mtime_ns": stats.st_mtime_ns,+ "device": stats.st_dev,+ "inode": stats.st_ino,+ } | overrides+++def test_snapshot_copies_the_source_byte_for_byte(rag_home):+ """CRLF runs and 0x1A bytes must survive: a text-mode read would eat both."""+ source = rag_home / "binary-source"+ source.mkdir()+ document = source / "notes.md"+ payload = b"first line\r\nsecond line\x1athird line\r\n"+ document.write_bytes(payload)++ snapshot = folder_sync._snapshot(str(source), _snapshot_metadata(document))++ try:+ assert Path(snapshot).read_bytes() == payload+ finally:+ folder_sync._remove_snapshot(snapshot)+++def test_snapshot_accepts_a_source_the_scan_could_not_identify(rag_home):+ """os.scandir reports st_dev/st_ino as 0 on Windows, so every file failed there."""+ source = rag_home / "identity-less"+ source.mkdir()+ document = source / "notes.md"+ payload = b"windows scandir reports no identity\n"+ document.write_bytes(payload)+ metadata = _snapshot_metadata(document, device = 0, inode = 0)++ snapshot = folder_sync._snapshot(str(source), metadata)++ try:+ assert Path(snapshot).read_bytes() == payload+ finally:+ folder_sync._remove_snapshot(snapshot)+++def test_snapshot_still_rejects_a_changed_source_without_an_identity(rag_home):+ source = rag_home / "identity-less-changed"+ source.mkdir()+ document = source / "notes.md"+ document.write_bytes(b"original")+ metadata = _snapshot_metadata(document, device = 0, inode = 0)+ document.write_bytes(b"replaced with a longer body")++ with pytest.raises(RuntimeError, match = "changed during reconciliation"):+ folder_sync._snapshot(str(source), metadata)+++def test_snapshot_rejects_a_source_swapped_mid_copy_without_an_identity(rag_home, monkeypatch):+ """The post-copy check compares fstat to fstat, so it still works with no scan identity."""+ source = rag_home / "identity-less-swapped"+ source.mkdir()+ document = source / "notes.md"+ document.write_bytes(b"stable body")+ metadata = _snapshot_metadata(document, device = 0, inode = 0)+ real_copy = folder_sync._copy_exact++ def copy_then_touch(src, dst, size):+ real_copy(src, dst, size)+ with open(document, "ab") as handle:+ handle.write(b" appended")++ monkeypatch.setattr(folder_sync, "_copy_exact", copy_then_touch)++ with pytest.raises(RuntimeError, match = "changed while it was copied"):+ folder_sync._snapshot(str(source), metadata)+++def test_snapshot_compares_the_identity_when_the_scan_recorded_one(rag_home):+ source = rag_home / "identity-kept"+ source.mkdir()+ document = source / "notes.md"+ document.write_bytes(b"same size body")+ metadata = _snapshot_metadata(document, inode = os.stat(document).st_ino + 1)++ with pytest.raises(RuntimeError, match = "changed during reconciliation"):+ folder_sync._snapshot(str(source), metadata)+++def test_snapshot_ignores_a_path_recovered_identity_os_fstat_disagrees_with(rag_home):+ """Shared-folder and WebDAV drivers give os.lstat and os.fstat different ids for one file."""+ source = rag_home / "identity-unstable"+ source.mkdir()+ document = source / "notes.md"+ payload = b"the scan recovered an id os.fstat will not repeat\n"+ document.write_bytes(payload)+ metadata = _snapshot_metadata(+ document, inode = os.stat(document).st_ino + 1, identity_from_path = True+ )++ snapshot = folder_sync._snapshot(str(source), metadata)++ try:+ assert Path(snapshot).read_bytes() == payload+ finally:+ folder_sync._remove_snapshot(snapshot)++@requires_sqlite_vecdef test_linked_folders_cannot_overlap_within_a_scope(rag_home):parent = rag_home / "parent"@@ -926,6 +1032,70 @@ def test_same_size_same_mtime_inode_replacement_is_reconciled(rag_home, stub_embassert store.search_lexical(conn, folder["scope"], "other", 5)+class _IdentitylessStat:+ """DirEntry.stat as Windows returns it: FindFirstFileW carries no file index."""++ st_dev = 0+ st_ino = 0++ def __init__(self, stats):+ self._stats = stats++ def __getattr__(self, name):+ return getattr(self._stats, name)+++class _IdentitylessEntry:+ def __init__(self, entry):+ self._entry = entry++ def stat(self, *, follow_symlinks = True):+ return _IdentitylessStat(self._entry.stat(follow_symlinks = follow_symlinks))++ def __getattr__(self, name):+ return getattr(self._entry, name)+++def windows_scandir(monkeypatch):+ """Strip the identity os.scandir cannot supply on Windows, leaving os.lstat intact."""+ real_scandir = os.scandir++ @contextmanager+ def scandir(directory):+ with real_scandir(directory) as entries:+ yield [_IdentitylessEntry(entry) for entry in entries]++ monkeypatch.setattr(folder_sync.os, "scandir", scandir)+++@requires_sqlite_vec+def test_same_size_same_mtime_replacement_is_reconciled_without_a_scandir_identity(+ rag_home, stub_embeddings, windows_scandir+):+ """The scan must recover the identity itself, or Windows indexes go stale forever."""+ source, folder = _folder(rag_home)+ path = source / "notes.txt"+ path.write_text("first text", encoding = "utf-8")+ assert _run(folder["id"])["status"] == "completed"+ before = _mapping(folder)+ assert before["inode"] not in (None, 0, "0")++ replacement = source / "replacement.txt"+ replacement.write_text("other text", encoding = "utf-8")+ os.utime(replacement, ns = (path.stat().st_atime_ns, path.stat().st_mtime_ns))+ replacement.replace(path)+ assert path.stat().st_size == before["size_bytes"]+ assert path.stat().st_mtime_ns == before["mtime_ns"]++ assert _run(folder["id"])["changed"] == 1+ with _connection() as conn:+ after = _mapping(folder)+ assert after["inode"] != before["inode"]+ assert after["document_id"] != before["document_id"]+ assert store.search_lexical(conn, folder["scope"], "other", 5)++@requires_sqlite_vecdef test_content_identical_touch_updates_metadata_without_reembedding(rag_home, stub_embeddings, monkeypatch