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.

262 KB
Raw Blame
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0

"""Chat files live under UNSLOTH_STUDIO_HOME and can be listed, fetched and cleaned up with the chat."""

import asyncio
import errno
import functools
import hashlib
import io
import json
import os
import re
import shutil
import subprocess
import sys
import tempfile
import threading
import time
import platform
from pathlib import Path

import pytest

from .thread_drain import join_when_started


def _shared_setup_1(monkeypatch, tmp_path):
    monkeypatch.setenv("UNSLOTH_STUDIO_SANDBOX_HOME", str(tmp_path / "sb"))

    from core.inference import tools

    tools._workdirs.clear()
    return tools


def _shared_setup_2(monkeypatch, tmp_path):
    monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(tmp_path / "home"))

    from core.inference import tools

    _forget_sandbox_state(tools)
    return tools


def _shared_setup_3(monkeypatch, root):
    monkeypatch.setenv("UNSLOTH_STUDIO_SANDBOX_HOME", str(root))

    from core.inference import tools

    tools._workdirs.clear()
    return tools


def _shared_setup_4(monkeypatch, tmp_path):
    monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(tmp_path / "home"))

    from core.inference import tools
    from storage import studio_db

    _forget_sandbox_state(tools)
    return studio_db, tools


def _shared_setup_5(monkeypatch, tmp_path):
    root = _shared_root(tmp_path, monkeypatch)

    from core.inference import tools

    _forget_sandbox_state(tools)
    return root, tools


def _shared_setup_6():
    from core.inference import tools

    tools._workdirs.clear()
    tools._legacy_sandbox_migrated = False
    return tools


def _shared_setup_7(monkeypatch):
    monkeypatch.delenv("UNSLOTH_STUDIO_SANDBOX_HOME", raising = False)

    from core.inference import tools

    tools._workdirs.clear()
    return tools


def _shared_setup_8(monkeypatch, tmp_path):
    import asyncio

    monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(tmp_path / "home"))

    from core.inference import tools
    from routes import chat_history

    return asyncio, chat_history, tools


def _shared_setup_9():
    import inspect

    from routes import chat_history

    route = inspect.getsource(chat_history.delete_project)
    return route


def _shared_setup_10(monkeypatch, studio_db, workspace):
    monkeypatch.setattr(
        studio_db,
        "ensure_chat_project_workspace",
        lambda pid: {
            "id": pid,
            "rootPath": str(workspace),
            "sandboxPath": str(workspace / "sandbox"),
        },
    )


def _shared_setup_11(fake_home, monkeypatch, tmp_path):
    monkeypatch.setenv("HOME", str(fake_home))
    monkeypatch.setenv("USERPROFILE", str(fake_home))
    monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(tmp_path / "home"))
    monkeypatch.delenv("UNSLOTH_STUDIO_SANDBOX_HOME", raising = False)


def _shared_setup_12(inference, monkeypatch, sandbox):
    monkeypatch.setattr(inference, "_authenticate_header_or_query", _noop_async)
    monkeypatch.setattr(
        inference,
        "_sandbox_dir_for",
        lambda session_id, create = False: str(sandbox),
    )


def _shared_setup_13(monkeypatch):
    monkeypatch.delenv("UNSLOTH_COMPILE_LOCATION", raising = False)

    from utils import cache_cleanup
    from utils.paths import storage_roots

    storage_roots.setup_cache_env()
    pinned = Path(os.environ["UNSLOTH_COMPILE_LOCATION"])
    return cache_cleanup, pinned


def _shared_setup_14():
    import asyncio

    from fastapi import HTTPException

    from routes import inference

    return HTTPException, asyncio, inference


def _shared_setup_15(monkeypatch, tmp_path):
    import asyncio

    monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(tmp_path / "home"))

    from routes import inference

    return asyncio, inference


def _shared_setup_16(monkeypatch, tmp_path):
    monkeypatch.setenv("UNSLOTH_STUDIO_SANDBOX_HOME", str(tmp_path / "sb"))

    from core.inference import tools
    from routes import inference

    tools._workdirs.clear()
    return inference, tools


def _shared_setup_17():
    import inspect

    from routes import chat_history
    from storage import studio_db

    return chat_history, inspect, studio_db


def _shared_setup_18(HTTPException, asyncio, inference):
    with pytest.raises(HTTPException) as caught:
        asyncio.new_event_loop().run_until_complete(
            inference.reveal_sandbox_dir("thread-1", request = None, token = None, session = None)
        )
    return caught


def _shared_setup_19(monkeypatch, tmp_path):
    monkeypatch.setenv("UNSLOTH_STUDIO_SANDBOX_HOME", str(tmp_path / "sb"))

    from fastapi import HTTPException

    from core.inference import tools

    return HTTPException, tools


def _shared_setup_20(monkeypatch, opened, path_utils):
    monkeypatch.setattr(
        path_utils,
        "reveal_in_file_manager",
        lambda path, expect_dir = False: opened.append(path),
    )


def _shared_setup_21(launch_dir, monkeypatch):
    monkeypatch.chdir(launch_dir)
    monkeypatch.delenv("UNSLOTH_COMPILE_LOCATION", raising = False)

    from utils import cache_cleanup

    return cache_cleanup


def _shared_setup_22(monkeypatch, tmp_path):
    monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(tmp_path / "home"))
    monkeypatch.setenv("UNSLOTH_STUDIO_PROJECTS_HOME", str(tmp_path / "projects"))

    from core.inference import tools

    return tools


def _shared_setup_23(tmp_path):
    from utils.paths import path_utils

    sandbox = tmp_path / "sandbox" / "thread-1"
    sandbox.mkdir(parents = True)
    return path_utils, sandbox


_FRONTEND_SRC = Path(__file__).resolve().parents[2] / "frontend" / "src"


@functools.lru_cache(maxsize = None)
def _frontend_text(*rel: str) -> str:
    """Must-make promises are searched only in user-visible copy; must-not-make ones across all sources."""
    paths: list[Path] = []
    for r in rel:
        target = _FRONTEND_SRC / r
        if target.is_dir():
            paths += [p for p in sorted(target.rglob("*")) if p.suffix in (".ts", ".tsx")]
        else:
            paths.append(target)
    return "\n".join(p.read_text(encoding = "utf-8") for p in paths if p.is_file())


def _frontend_copy_text() -> str:
    """Where the sidebar's user-visible strings live: the locales, and the component itself."""
    return _frontend_text("i18n/locales", "components/app-sidebar.tsx")


def _frontend_src_text() -> str:
    """Every frontend source file, for asserting a string is absent from all of them."""
    return _frontend_text(".")


def _sidebar_function_body(name: str) -> str:
    """The body of a top-level `function <name>(...) {...}` in app-sidebar.tsx, brace-matched so
    reformatting does not change what is read, and so unrelated edits elsewhere cannot fail it."""
    sidebar = (_FRONTEND_SRC / "components" / "app-sidebar.tsx").read_text(encoding = "utf-8")
    start = sidebar.index(f"function {name}(")
    open_brace = sidebar.index("{", start)
    depth = 0
    for i in range(open_brace, len(sidebar)):
        if sidebar[i] == "{":
            depth += 1
        elif sidebar[i] == "}":
            depth -= 1
            if depth == 0:
                return sidebar[open_brace : i + 1]
    raise AssertionError(f"unbalanced braces reading {name} out of app-sidebar.tsx")


def test_sandbox_lives_under_the_studio_home(tmp_path, monkeypatch):
    fake_home = tmp_path / "userprofile"
    studio_home = tmp_path / "custom_studio_home"
    fake_home.mkdir()
    studio_home.mkdir()

    monkeypatch.setenv("HOME", str(fake_home))
    monkeypatch.setenv("USERPROFILE", str(fake_home))
    monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(studio_home))

    from core.inference import tools

    tools._workdirs.clear()
    wd = Path(tools.get_sandbox_workdir("__LOCALID_aB3xY7q"))
    print(f"\n[{platform.system()}] sandbox workdir = {wd}")

    assert str(wd).startswith(str(studio_home)), wd
    assert not str(wd).startswith(str(fake_home / "studio_sandbox")), wd
    assert wd.parent.name == "sandbox"
    assert wd.name.startswith("__LOCALID_")


def test_sandbox_home_override(tmp_path, monkeypatch):
    """A user who wants the sandbox on another volume can say so."""
    elsewhere = tmp_path / "other volume" / "sandboxes"
    monkeypatch.setenv("UNSLOTH_STUDIO_SANDBOX_HOME", str(elsewhere))

    from core.inference import tools

    tools._workdirs.clear()
    wd = Path(tools.get_sandbox_workdir("__LOCALID_aB3xY7q"))
    assert wd == elsewhere / "__LOCALID_aB3xY7q"


def test_legacy_sandbox_is_migrated(tmp_path, monkeypatch):
    """Files created before the move are carried over, not abandoned."""
    fake_home = tmp_path / "userprofile"
    studio_home = tmp_path / "studio_home"
    legacy = fake_home / "studio_sandbox" / "__LOCALID_old1234"
    legacy.mkdir(parents = True)
    (legacy / "results.csv").write_text("a,b\n1,2\n")

    monkeypatch.setenv("HOME", str(fake_home))
    monkeypatch.setenv("USERPROFILE", str(fake_home))
    monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(studio_home))
    monkeypatch.delenv("UNSLOTH_STUDIO_SANDBOX_HOME", raising = False)

    tools = _shared_setup_6()
    wd = Path(tools.get_sandbox_workdir("__LOCALID_new5678"))
    for thread in threading.enumerate():
        if thread.name == "sandbox-migrate":
            join_when_started(thread, timeout = 30)
    moved = wd.parent / "__LOCALID_old1234" / "results.csv"
    print(f"\nmigrated to {moved}")
    assert moved.is_file()
    assert moved.read_text() == "a,b\n1,2\n"


def test_images_stay_inline_and_everything_else_downloads():
    """Images stay inline with a real type; other files download, as inline HTML runs script."""
    from core.inference import tools as _t
    from routes.inference import _SANDBOX_MEDIA_TYPES

    assert (
        set(_SANDBOX_MEDIA_TYPES) == _t._IMAGE_EXTS
    ), "the route's inline map and the tool-result classifier drifted apart"

    for ext in (".png", ".jpg", ".jpeg", ".gif", ".webp", ".bmp", ".avif"):
        assert ext in _SANDBOX_MEDIA_TYPES
    for ext in (".csv", ".txt", ".py", ".json", ".pdf", ".zip", ".html", ".svg"):
        assert ext not in _SANDBOX_MEDIA_TYPES, ext

    import inspect
    from routes import inference

    src = inspect.getsource(inference.serve_sandbox_file)
    assert "application/octet-stream" in src
    assert "Content-Disposition" in src
    assert "attachment" in src
    assert "File type not allowed" not in src, "non-images are no longer rejected"


def test_sandbox_listing_route_exists():
    """The UI can enumerate a chat's files, and learn where they live."""
    from routes.inference import router

    sandbox_routes = sorted(r.path for r in router.routes if "sandbox" in r.path)
    print(f"\nsandbox routes = {sandbox_routes}")
    assert sandbox_routes == [
        "/sandbox/{session_id}",
        "/sandbox/{session_id}/open",
        "/sandbox/{session_id}/reveal",
        "/sandbox/{session_id}/upload",
        "/sandbox/{session_id}/{filename:path}",
    ]

    import inspect
    from routes import inference

    src = inspect.getsource(inference.list_sandbox_files)
    assert '"path"' in src, "the listing must answer 'where did my file go'"


def test_both_executors_report_created_files(tmp_path, monkeypatch):
    """A file is reported whether it came from python or from bash."""
    tools = _shared_setup_1(monkeypatch, tmp_path)
    session = "__LOCALID_exec123"
    workdir = tools.get_sandbox_workdir(session)

    for name, run in (
        (
            "py.csv",
            lambda: tools._python_exec("open('py.csv','w').write('a,b\\n')", session_id = session),
        ),
        ("sh.csv", lambda: tools._bash_exec("printf 'a,b\\n' > sh.csv", session_id = session)),
    ):
        result = run()
        print(f"\n{name} -> {result!r}")
        assert "__FILES__:" in result, result
        assert name in result
        assert os.path.isfile(os.path.join(workdir, name))

    from core.inference.tool_loop_controller import strip_result_for_model

    stripped = strip_result_for_model('done\n__FILES__:[{"name": "x.csv"}]')
    assert "__FILES__" not in stripped
    assert stripped.strip() == "done"


def test_internal_temp_files_are_not_reported(tmp_path, monkeypatch):
    """The executor's own scratch script is not a user-facing artifact."""
    tools = _shared_setup_1(monkeypatch, tmp_path)
    result = tools._python_exec("print('hi')", session_id = "__LOCALID_tmp999")
    assert "studio_exec_" not in result
    assert "__FILES__" not in result


def test_tool_description_says_files_are_kept():
    from core.inference import tools

    note = tools._SANDBOX_PATHS_NOTE
    print(f"\nsandbox note = {note!r}")
    assert "download link" in note
    assert "name the files you created" in note
    assert "absolute path" in note


def test_compiled_cache_is_pinned_under_the_studio_home(tmp_path, monkeypatch):
    """Not left CWD-relative, which put it in %USERPROFILE% on Windows."""
    monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(tmp_path / "studio_home"))
    monkeypatch.delenv("UNSLOTH_COMPILE_LOCATION", raising = False)

    import importlib
    from utils.paths import storage_roots

    importlib.reload(storage_roots)
    storage_roots.ensure_studio_directories()

    location = Path(os.environ["UNSLOTH_COMPILE_LOCATION"])
    print(f"\n[{platform.system()}] compiled cache pinned to {location}")
    assert location.is_absolute()
    assert str(tmp_path / "studio_home") in str(location)


def test_cache_cleanup_finds_the_configured_cache_and_not_the_cwd_one(tmp_path, monkeypatch):
    """unsloth_zoo writes only to UNSLOTH_COMPILE_LOCATION once it is set, so a same-named folder in the launcher's CWD is not ours."""
    from utils import cache_cleanup

    monkeypatch.chdir(tmp_path)
    configured = tmp_path / "studio_home" / "compiled_cache"
    configured.mkdir(parents = True)
    (tmp_path / "unsloth_compiled_cache").mkdir()
    monkeypatch.setenv("UNSLOTH_COMPILE_LOCATION", str(configured))

    found = {str(Path(d).resolve()) for d in cache_cleanup.get_existing_cache_dirs()}
    assert str(configured.resolve()) in found
    assert str((tmp_path / "unsloth_compiled_cache").resolve()) not in found

    monkeypatch.delenv("UNSLOTH_COMPILE_LOCATION")
    found = {str(Path(d).resolve()) for d in cache_cleanup.get_existing_cache_dirs()}
    assert str((tmp_path / "unsloth_compiled_cache").resolve()) in found


def test_deleting_a_chat_cleans_up_its_sandbox(tmp_path, monkeypatch):
    """An empty sandbox always goes; files need an explicit opt-in."""
    tools = _shared_setup_1(monkeypatch, tmp_path)
    empty = Path(tools.get_sandbox_workdir("__LOCALID_empty11"))
    assert tools.remove_session_sandbox("__LOCALID_empty11") is True
    assert not empty.exists()

    tools._workdirs.clear()
    withfile = Path(tools.get_sandbox_workdir("__LOCALID_files22"))
    (withfile / "keep.csv").write_text("a\n")
    assert tools.remove_session_sandbox("__LOCALID_files22") is False
    assert (withfile / "keep.csv").is_file()
    assert tools.remove_session_sandbox("__LOCALID_files22", delete_files = True) is True
    assert not withfile.exists()


def test_sandbox_removal_cannot_escape_the_root(tmp_path, monkeypatch):
    monkeypatch.setenv("UNSLOTH_STUDIO_SANDBOX_HOME", str(tmp_path / "sb"))

    from core.inference import tools

    outside = tmp_path / "precious"
    outside.mkdir()
    for bad in ("..", "../precious", "/etc", "project-abc", ""):
        assert tools.remove_session_sandbox(bad, delete_files = True) is False
    assert outside.is_dir()


def test_a_windows_device_name_never_becomes_a_directory(tmp_path, monkeypatch):
    """Windows device names like CON and NUL are reserved even as folder names."""
    tools = _shared_setup_1(monkeypatch, tmp_path)
    seen = set()
    for reserved in ("con", "NUL", "aux", "COM1", "lpt9", "nul.txt"):
        workdir = Path(tools.get_sandbox_workdir(reserved))
        assert workdir.name.startswith("_id-"), reserved
        seen.add(workdir.name)
        assert tools.remove_session_sandbox(reserved, delete_files = True) is True
    assert len(seen) == 6, "reserved names shared a directory"


def test_reading_a_sandbox_never_creates_it(tmp_path, monkeypatch):
    """A GET must not leave a folder behind for every id it is asked about."""
    root = tmp_path / "sb"
    tools = _shared_setup_3(monkeypatch, root)
    resolved = Path(tools.resolve_sandbox_workdir("__LOCALID_ghost"))
    assert resolved == root / "__LOCALID_ghost"
    assert not resolved.exists()
    assert not root.exists()
    assert Path(tools.get_sandbox_workdir("__LOCALID_ghost")) == resolved
    assert resolved.is_dir()


def test_clearing_the_compiled_cache_covers_the_configured_location(tmp_path, monkeypatch):
    """The cleanup must follow UNSLOTH_COMPILE_LOCATION, not just the defaults."""
    pinned = tmp_path / "home" / "compiled_cache"
    pinned.mkdir(parents = True)
    (pinned / "unsloth_compiled_module_gemma3.py").write_text("x = 1\n")
    (pinned / "UnslothSFTTrainer.py").write_text("x = 1\n")
    monkeypatch.setenv("UNSLOTH_COMPILE_LOCATION", str(pinned))

    from utils import cache_cleanup

    assert pinned in cache_cleanup.get_existing_cache_dirs()
    cache_cleanup.clear_unsloth_compiled_cache(preserve_patterns = ["Unsloth*Trainer.py"])
    assert not (pinned / "unsloth_compiled_module_gemma3.py").exists()
    assert (pinned / "UnslothSFTTrainer.py").is_file()


def test_a_file_in_a_subdirectory_is_reported_and_servable(tmp_path, monkeypatch):
    """`df.to_csv("outputs/report.csv")` is ordinary; a top-level listing saw
    only the directory and dropped it, so no chip and no download."""
    tools = _shared_setup_1(monkeypatch, tmp_path)
    workdir = Path(tools.get_sandbox_workdir("__LOCALID_nested1"))
    before = tools._snapshot_workdir_files(str(workdir))
    (workdir / "outputs").mkdir()
    (workdir / "outputs" / "report.csv").write_text("a,b\n")
    (workdir / "top.txt").write_text("x")

    sentinels = tools._created_file_sentinels(str(workdir), before)
    assert "outputs/report.csv" in sentinels
    assert "top.txt" in sentinels


def test_the_walk_is_bounded(tmp_path, monkeypatch):
    """A chat that unpacked an archive must not turn a tool call into a crawl."""
    tools = _shared_setup_1(monkeypatch, tmp_path)
    workdir = Path(tools.get_sandbox_workdir("__LOCALID_deep111"))
    deep = workdir
    for level in range(8):
        deep = deep / f"level{level}"
        deep.mkdir()
        (deep / "f.txt").write_text("x")
    found = tools._snapshot_workdir_files(str(workdir))
    assert found, "nothing was found at all"
    assert max(name.count("/") + 1 for name in found) <= tools._MAX_SANDBOX_PATH_SEGMENTS


def test_files_written_before_a_timeout_are_still_reported(tmp_path, monkeypatch):
    """`printf data > report.csv; sleep 999` produced that file."""
    tools = _shared_setup_1(monkeypatch, tmp_path)
    result = tools._bash_exec(
        "printf data > report.csv; sleep 30",
        timeout = 3,
        session_id = "__LOCALID_slowrun",
    )
    assert "timed out" in result
    assert "__FILES__:" in result, result
    assert "report.csv" in result


def test_the_image_list_is_capped_like_the_file_list(tmp_path, monkeypatch):
    tools = _shared_setup_1(monkeypatch, tmp_path)
    workdir = Path(tools.get_sandbox_workdir("__LOCALID_manyimg"))
    before = tools._snapshot_workdir_files(str(workdir))
    for i in range(80):
        (workdir / f"frame{i:03d}.png").write_bytes(b"\x89PNG")
    sentinels = tools._created_file_sentinels(str(workdir), before)
    images = json.loads(sentinels.split("__IMAGES__:")[1])
    files = json.loads(sentinels.split("__FILES__:")[1].split("\n")[0])
    assert len(images) == tools._MAX_REPORTED_FILES
    assert len(files) == tools._MAX_REPORTED_FILES


def test_a_tool_printing_the_files_marker_keeps_its_output():
    """Only a structurally valid trailing envelope is stripped."""
    from core.inference.tool_loop_controller import strip_result_for_model

    printed = "here is the doc:\n__FILES__:see the manual\nand the answer is 42"
    assert strip_result_for_model(printed) == printed

    real = 'output\n__FILES__:[{"name": "a.csv", "size": 3}]'
    assert strip_result_for_model(real) == "output"


def test_clearing_all_chats_cleans_up_their_sandboxes(tmp_path, monkeypatch):
    """The UI's "Clear all chats" is the common bulk delete."""
    tools = _shared_setup_1(monkeypatch, tmp_path)
    empty = Path(tools.get_sandbox_workdir("__LOCALID_bulk111"))
    assert empty.is_dir()

    import routes.chat_history as chat_history

    monkeypatch.setattr(chat_history, "list_chat_threads", lambda: [{"id": "__LOCALID_bulk111"}])
    monkeypatch.setattr(chat_history, "clear_chat_history", lambda **_kwargs: ([], [], []))
    monkeypatch.setattr(chat_history, "_cancel_active_research", lambda request, ids: None)

    import asyncio

    body = asyncio.run(chat_history.clear_history(request = None, current_subject = "tester"))
    assert body["sandboxes_removed"] == 1
    assert not empty.exists()


def test_the_legacy_migration_is_startup_work(tmp_path, monkeypatch):
    """Cross-filesystem moves copy every session: startup work, never on the event loop."""
    home = tmp_path / "home"
    fake_home = tmp_path / "userprofile"
    fake_home.mkdir()
    monkeypatch.setenv("HOME", str(fake_home))
    monkeypatch.setenv("USERPROFILE", str(fake_home))
    monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(home))
    monkeypatch.delenv("UNSLOTH_STUDIO_SANDBOX_HOME", raising = False)

    legacy = fake_home / "studio_sandbox" / "__LOCALID_upgrade"
    legacy.mkdir(parents = True)
    (legacy / "sales.csv").write_text("a,b\n")

    tools = _shared_setup_6()
    Path(tools.resolve_sandbox_workdir("__LOCALID_upgrade"))
    assert (legacy / "sales.csv").is_file()

    # Joined, not polled: the legacy folder vanishes during staging, before the move lands.
    mover = tools.migrate_legacy_sandbox_in_background()
    mover.join(60)
    assert not mover.is_alive(), "the background migration never finished"
    resolved = Path(tools.resolve_sandbox_workdir("__LOCALID_upgrade"))
    assert (resolved / "sales.csv").is_file(), "the file did not follow the migration"
    assert not legacy.exists(), "the session was left at the legacy root"
    assert resolved.resolve().is_relative_to(Path(tools.sandbox_root()).resolve())


@pytest.mark.parametrize("paused_after", ["move", "mark"])
@pytest.mark.parametrize(
    "session_id",
    [
        "__LOCALID_reading",
        "_id-reading",
    ],
)
def test_a_read_does_not_answer_from_inside_a_legacy_move_s_staging_window(
    tmp_path, monkeypatch, paused_after, session_id
):
    """Reads during a legacy move's staging window must wait for the move, or the sandbox lists empty."""
    fake_home = tmp_path / "userprofile"
    fake_home.mkdir()
    _shared_setup_11(fake_home, monkeypatch, tmp_path)

    session = fake_home / "studio_sandbox" / session_id
    session.mkdir(parents = True)
    (session / "data.csv").write_text("a\n")

    tools = _shared_setup_6()

    staged = threading.Event()
    release = threading.Event()
    real_move = shutil.move

    real_mark = tools._mark_sandbox

    def pause_in_staging():
        staged.set()
        release.wait(10)

    def gated_move(source, destination, *args, **kwargs):
        moved = real_move(source, destination, *args, **kwargs)
        if paused_after == "move" and tools._STAGING_SUFFIX in os.path.basename(destination):
            pause_in_staging()
        return moved

    def gated_mark(path, name):
        marked = real_mark(path, name)
        if paused_after == "mark" and tools._STAGING_SUFFIX in os.path.basename(path):
            pause_in_staging()
        return marked

    monkeypatch.setattr(tools.shutil, "move", gated_move)
    monkeypatch.setattr(tools, "_mark_sandbox", gated_mark)

    mover = tools.migrate_legacy_sandbox_in_background()
    assert staged.wait(10), "the migration never reached the staging window"

    result = {}
    reader = threading.Thread(
        target = lambda: result.update(
            workdir = Path(tools.resolve_sandbox_workdir(session_id)),
        ),
        daemon = True,
    )
    reader.start()
    reader.join(1.0)
    returned_early = not reader.is_alive()
    release.set()
    reader.join(10)
    mover.join(10)

    assert not returned_early, "the read answered from inside the staging window"
    assert "workdir" in result, "the read never returned"
    assert (result["workdir"] / "data.csv").is_file(), f"{result['workdir']} lost its files"


def test_a_read_that_waited_out_a_stranded_move_finds_the_staging_tree(tmp_path, monkeypatch):
    """After waiting out a stranded move, a read must re-check staging, not return a missing destination."""
    fake_home = tmp_path / "userprofile"
    fake_home.mkdir()
    _shared_setup_11(fake_home, monkeypatch, tmp_path)

    session = fake_home / "studio_sandbox" / "__LOCALID_stranded"
    session.mkdir(parents = True)
    (session / "data.csv").write_text("a\n")

    tools = _shared_setup_6()

    staged = threading.Event()
    release = threading.Event()
    real_move = shutil.move
    real_rename = os.rename

    def gated_move(source, destination, *args, **kwargs):
        moved = real_move(source, destination, *args, **kwargs)
        if tools._STAGING_SUFFIX in os.path.basename(destination):
            staged.set()
            release.wait(10)
        return moved

    def failing_rename(source, destination, *args, **kwargs):
        if tools._STAGING_SUFFIX in os.path.basename(os.fspath(source)):
            raise OSError("rename refused")
        return real_rename(source, destination, *args, **kwargs)

    monkeypatch.setattr(tools.shutil, "move", gated_move)
    monkeypatch.setattr(tools.os, "rename", failing_rename)

    mover = tools.migrate_legacy_sandbox_in_background()
    assert staged.wait(10), "the migration never reached the staging window"

    result = {}
    reader = threading.Thread(
        target = lambda: result.update(
            workdir = Path(tools.resolve_sandbox_workdir("__LOCALID_stranded")),
        ),
        daemon = True,
    )
    reader.start()
    reader.join(1.0)
    returned_early = not reader.is_alive()
    release.set()
    reader.join(10)
    mover.join(10)

    assert not returned_early, "the read answered from inside the staging window"
    assert "workdir" in result, "the read never returned"
    assert tools._STAGING_SUFFIX in result["workdir"].name, result["workdir"]
    assert (result["workdir"] / "data.csv").is_file(), f"{result['workdir']} lost its files"


def test_the_migration_is_serialised(tmp_path, monkeypatch):
    """Two chats running their first tool call at once must not strand files."""
    import threading

    fake_home = tmp_path / "userprofile"
    fake_home.mkdir()
    _shared_setup_11(fake_home, monkeypatch, tmp_path)

    for index in range(6):
        session = fake_home / "studio_sandbox" / f"__LOCALID_race{index}"
        session.mkdir(parents = True)
        (session / "data.csv").write_text("a\n")

    tools = _shared_setup_6()
    results = []

    def first_tool_call(index):
        results.append(Path(tools.get_sandbox_workdir(f"__LOCALID_race{index}")))

    threads = [threading.Thread(target = first_tool_call, args = (i,)) for i in range(6)]
    for thread in threads:
        thread.start()
    for thread in threads:
        thread.join()

    assert len(results) == 6
    for workdir in results:
        assert (workdir / "data.csv").is_file(), f"{workdir} lost its files"


def test_a_first_tool_call_waits_out_a_move_already_in_staging(tmp_path, monkeypatch):
    """A tool call in the staging window must wait for the move, not create an empty sandbox there."""
    import shutil
    import threading

    fake_home = tmp_path / "userprofile"
    fake_home.mkdir()
    _shared_setup_11(fake_home, monkeypatch, tmp_path)

    session = fake_home / "studio_sandbox" / "__LOCALID_staged"
    session.mkdir(parents = True)
    (session / "data.csv").write_text("a\n")

    tools = _shared_setup_6()

    staged = threading.Event()
    release = threading.Event()
    real_move = shutil.move

    def gated_move(source, destination, *args, **kwargs):
        moved = real_move(source, destination, *args, **kwargs)
        if "__LOCALID_staged" in str(destination):
            staged.set()
            release.wait(10)
        return moved

    monkeypatch.setattr(tools.shutil, "move", gated_move)

    mover = threading.Thread(
        target = lambda: tools._migrate_legacy_sandbox(tools.sandbox_root()),
        daemon = True,
    )
    mover.start()
    assert staged.wait(10), "the migration never reached the staging window"

    result = {}
    caller = threading.Thread(
        target = lambda: result.update(
            workdir = Path(tools.get_sandbox_workdir("__LOCALID_staged")),
        ),
        daemon = True,
    )
    caller.start()
    caller.join(1.0)
    returned_early = not caller.is_alive()
    release.set()
    caller.join(10)
    mover.join(10)

    assert not returned_early, "the first tool call answered from inside the staging window"
    assert "workdir" in result, "the first tool call never returned"
    assert (result["workdir"] / "data.csv").is_file(), f"{result['workdir']} lost its files"


def test_the_migrated_flag_cannot_be_set_over_a_move_still_in_staging(tmp_path, monkeypatch):
    """The migrated flag must not be set while a move is still in staging, or the rollback retry is lost."""
    import shutil
    import threading

    fake_home = tmp_path / "userprofile"
    fake_home.mkdir()
    _shared_setup_11(fake_home, monkeypatch, tmp_path)

    session = fake_home / "studio_sandbox" / "__LOCALID_flag"
    session.mkdir(parents = True)
    (session / "data.csv").write_text("a\n")

    tools = _shared_setup_6()

    staged = threading.Event()
    release = threading.Event()
    real_move = shutil.move

    def gated_move(source, destination, *args, **kwargs):
        moved = real_move(source, destination, *args, **kwargs)
        if "__LOCALID_flag" in str(destination):
            staged.set()
            release.wait(10)
        return moved

    monkeypatch.setattr(tools.shutil, "move", gated_move)

    root = tools.sandbox_root()
    mover = threading.Thread(
        target = lambda: tools._migrate_one_legacy_session(root, "__LOCALID_flag"),
        daemon = True,
    )
    mover.start()
    assert staged.wait(10), "the per-session move never reached the staging window"

    tools._migrate_legacy_sandbox(root)
    assert (
        not tools._legacy_sandbox_migrated
    ), "a pass that ran through another session's staging window called the migration finished"

    result = {}
    caller = threading.Thread(
        target = lambda: result.update(
            workdir = Path(tools.get_sandbox_workdir("__LOCALID_flag")),
        ),
        daemon = True,
    )
    caller.start()
    caller.join(1.0)
    returned_early = not caller.is_alive()
    release.set()
    caller.join(10)
    mover.join(10)

    assert not returned_early, "the first tool call answered from inside the staging window"
    assert "workdir" in result, "the first tool call never returned"
    assert (result["workdir"] / "data.csv").is_file(), f"{result['workdir']} lost its files"


def test_a_rolled_back_move_puts_the_migration_back_on_the_table(tmp_path, monkeypatch):
    """A rolled-back move must keep the migration retryable; the done flag must not short-circuit it."""
    import os
    import shutil
    import threading

    fake_home = tmp_path / "userprofile"
    fake_home.mkdir()
    _shared_setup_11(fake_home, monkeypatch, tmp_path)

    legacy = fake_home / "studio_sandbox" / "__LOCALID_rollback"
    legacy.mkdir(parents = True)
    (legacy / "data.csv").write_text("a\n")

    tools = _shared_setup_6()

    staged = threading.Event()
    release = threading.Event()
    real_move = shutil.move
    real_rename = os.rename
    rename_failed = []

    def gated_move(source, destination, *args, **kwargs):
        moved = real_move(source, destination, *args, **kwargs)
        if "__LOCALID_rollback" in str(destination):
            staged.set()
            release.wait(10)
        return moved

    def failing_rename(source, destination, *args, **kwargs):
        if str(destination).endswith("__LOCALID_rollback") and not rename_failed:
            rename_failed.append(True)
            raise OSError(39, "Directory not empty")
        return real_rename(source, destination, *args, **kwargs)

    monkeypatch.setattr(tools.shutil, "move", gated_move)
    monkeypatch.setattr(tools.os, "rename", failing_rename)

    root = tools.sandbox_root()
    mover = threading.Thread(
        target = lambda: tools._migrate_one_legacy_session(root, "__LOCALID_rollback"),
        daemon = True,
    )
    mover.start()
    assert staged.wait(10), "the per-session move never reached the staging window"

    # Set by hand: models the gap between a pass reading an empty in-flight set and setting the flag.
    tools._migrate_legacy_sandbox(root)
    tools._legacy_sandbox_migrated = True

    release.set()
    mover.join(10)

    assert rename_failed, "the rename was never made to fail"
    assert (legacy / "data.csv").is_file(), "the rollback did not restore the legacy copy"
    assert (
        not tools._legacy_sandbox_migrated
    ), "a restored legacy copy left the migration marked finished, so nothing retries it"

    workdir = Path(tools.get_sandbox_workdir("__LOCALID_rollback"))
    assert (workdir / "data.csv").is_file(), f"{workdir} never got the restored files"


def test_a_move_that_begins_and_ends_inside_the_pass_still_counts(tmp_path, monkeypatch):
    """A move that begins and ends inside a pass must still count, since listing may have missed it."""
    import os
    import shutil

    fake_home = tmp_path / "userprofile"
    fake_home.mkdir()
    _shared_setup_11(fake_home, monkeypatch, tmp_path)

    legacy = fake_home / "studio_sandbox" / "__LOCALID_inside"
    legacy.mkdir(parents = True)
    (legacy / "data.csv").write_text("a\n")

    tools = _shared_setup_6()
    root = tools.sandbox_root()

    real_listdir = os.listdir
    real_rename = os.rename
    rename_failed = []
    ran = []

    def failing_rename(source, destination, *args, **kwargs):
        if str(destination).endswith("__LOCALID_inside") and not rename_failed:
            rename_failed.append(True)
            raise OSError(39, "Directory not empty")
        return real_rename(source, destination, *args, **kwargs)

    def listdir_then_move(path, *args, **kwargs):
        entries = real_listdir(path, *args, **kwargs)
        if not ran and os.path.realpath(str(path)) == os.path.realpath(
            str(fake_home / "studio_sandbox")
        ):
            ran.append(True)
            monkeypatch.setattr(tools.os, "rename", failing_rename)
            try:
                tools._migrate_one_legacy_session(root, "__LOCALID_inside")
            except OSError:
                pass
            monkeypatch.setattr(tools.os, "rename", real_rename)
            return [e for e in entries if e != "__LOCALID_inside"]
        return entries

    monkeypatch.setattr(tools.os, "listdir", listdir_then_move)
    monkeypatch.setattr(tools.shutil, "move", shutil.move)

    tools._migrate_legacy_sandbox(root)

    assert ran, "the nested move never ran"
    assert rename_failed, "the rename was never made to fail"
    assert (legacy / "data.csv").is_file(), "the rollback did not restore the legacy copy"
    assert (
        not tools._legacy_sandbox_migrated
    ), "the pass finished over a move it never listed, so the restored copy is stranded"

    workdir = Path(tools.get_sandbox_workdir("__LOCALID_inside"))
    assert (workdir / "data.csv").is_file(), f"{workdir} never got the restored files"


def test_a_rollback_between_the_two_reads_is_not_read_as_nothing_to_do(tmp_path, monkeypatch):
    """A rollback between the two reads must not be read as nothing to do and cache an empty sandbox."""
    import os
    import shutil
    import threading

    fake_home = tmp_path / "userprofile"
    fake_home.mkdir()
    _shared_setup_11(fake_home, monkeypatch, tmp_path)

    legacy = fake_home / "studio_sandbox" / "__LOCALID_split"
    legacy.mkdir(parents = True)
    (legacy / "data.csv").write_text("a\n")

    tools = _shared_setup_6()
    root = tools.sandbox_root()

    real_isdir = os.path.isdir
    real_rename = os.rename
    real_move = shutil.move
    staged = threading.Event()
    release = threading.Event()
    rolled_back = threading.Event()
    finish = threading.Event()
    rename_failed = []
    split = []

    def failing_rename(source, destination, *args, **kwargs):
        if str(destination).endswith("__LOCALID_split") and not rename_failed:
            rename_failed.append(True)
            raise OSError(39, "Directory not empty")
        return real_rename(source, destination, *args, **kwargs)

    def gated_move(source, destination, *args, **kwargs):
        moved = real_move(source, destination, *args, **kwargs)
        if "__LOCALID_split" in str(destination):
            staged.set()
            release.wait(10)
        return moved

    monkeypatch.setattr(tools.os, "rename", failing_rename)
    monkeypatch.setattr(tools.shutil, "move", gated_move)

    def run_mover():
        try:
            tools._migrate_one_legacy_session(root, "__LOCALID_split")
        finally:
            rolled_back.set()
            finish.wait(10)

    mover = threading.Thread(target = run_mover, daemon = True)
    mover.start()
    monkeypatch.setattr(tools, "_legacy_background", mover, raising = False)
    assert staged.wait(10), "the move never reached the staging window"

    def isdir_then_roll_back(path, *args, **kwargs):
        answer = real_isdir(path, *args, **kwargs)
        if not split and str(path).endswith("__LOCALID_split") and not answer:
            split.append(True)
            release.set()
            rolled_back.wait(10)
        return answer

    monkeypatch.setattr(tools.os.path, "isdir", isdir_then_roll_back)

    workdir = Path(tools.get_sandbox_workdir("__LOCALID_split"))

    monkeypatch.setattr(tools.os.path, "isdir", real_isdir)
    finish.set()
    mover.join(10)

    assert rename_failed, "the rename was never made to fail"
    assert split, "the caller never split its reads across the rollback"
    assert (workdir / "data.csv").is_file(), f"{workdir} lost its files across the rollback"


def test_a_stalled_migration_does_not_grow_a_lock_per_chat(tmp_path, monkeypatch):
    """A stalled migration must not add a lock per chat; only chats with a legacy folder get an entry."""
    fake_home = tmp_path / "userprofile"
    fake_home.mkdir()
    _shared_setup_11(fake_home, monkeypatch, tmp_path)

    legacy = fake_home / "studio_sandbox"
    legacy.mkdir(parents = True)
    (legacy / "__LOCALID_had_one").mkdir()
    (legacy / "__LOCALID_had_one" / "data.csv").write_text("a\n")
    (legacy / "_invalid").mkdir()

    tools = _shared_setup_6()
    tools._legacy_session_locks.clear()
    root = tools.sandbox_root()

    for index in range(25):
        tools._migrate_one_legacy_session(root, f"__LOCALID_never_there{index}")

    assert legacy.is_dir(), "this test needs the legacy root to still be there"
    assert (
        set(tools._legacy_session_locks) == set()
    ), f"chats with nothing at the legacy root left locks behind: {sorted(tools._legacy_session_locks)}"

    tools._migrate_one_legacy_session(root, "__LOCALID_had_one")
    assert (Path(root) / "__LOCALID_had_one" / "data.csv").is_file(), "the real move did not happen"
    assert set(tools._legacy_session_locks) == {"__LOCALID_had_one"}


def test_every_reported_file_is_downloadable(tmp_path, monkeypatch):
    """The walk and the download route must agree, or a listed file always 404s."""
    HTTPException, tools = _shared_setup_19(monkeypatch, tmp_path)
    from routes.inference import _contained_sandbox_path

    tools._workdirs.clear()
    workdir = Path(tools.get_sandbox_workdir("__LOCALID_agree11"))
    before = tools._snapshot_workdir_files(str(workdir))
    deep = workdir
    for level in range(6):
        deep = deep / f"d{level}"
        deep.mkdir()
        (deep / f"f{level}.csv").write_text("x")

    reported = json.loads(
        tools._created_file_sentinels(str(workdir), before).split("__FILES__:")[1].split("\n")[0]
    )
    assert reported, "nothing was reported at all"
    for entry in reported:
        try:
            _sandbox_dir, resolved = _contained_sandbox_path("__LOCALID_agree11", entry["name"])
        except HTTPException as refused:
            raise AssertionError(f"{entry['name']} reported but refused ({refused.status_code})")
        assert os.path.isfile(resolved), entry["name"]


def test_a_same_timestamp_overwrite_is_still_reported(tmp_path, monkeypatch):
    """Coarse-resolution volumes (FAT/exFAT) can repeat mtime_ns."""
    tools = _shared_setup_1(monkeypatch, tmp_path)
    workdir = Path(tools.get_sandbox_workdir("__LOCALID_coarse1"))
    target = workdir / "report.csv"
    target.write_text("a")
    before = tools._snapshot_workdir_files(str(workdir))
    target.write_text("a,b,c,d")
    os.utime(target, ns = (before["report.csv"][0], before["report.csv"][0]))
    assert "report.csv" in tools._created_file_sentinels(str(workdir), before)


@pytest.mark.parametrize(
    "payload",
    [
        "[null]",
        "[1, 2]",
        '["a.csv"]',
        '[{"size": 3}]',
        '[{"name": ""}]',
        '[{"name": 5}]',
        '[{"name": "a.csv", "size": "big"}]',
    ],
)
def test_a_malformed_files_envelope_is_left_as_text(payload):
    """Only the executor's own envelope is protocol; anything else is output."""
    from core.inference.tool_loop_controller import strip_result_for_model

    printed = f"tool output\n__FILES__:{payload}"
    assert strip_result_for_model(printed) == printed


def test_a_dotfile_a_tool_creates_is_reported(tmp_path, monkeypatch):
    """.gitignore is a real artifact the route serves, so only dot-directories are excluded."""
    tools = _shared_setup_1(monkeypatch, tmp_path)
    workdir = Path(tools.get_sandbox_workdir("__LOCALID_dotfile"))
    before = tools._snapshot_workdir_files(str(workdir))
    (workdir / ".gitignore").write_text("*.pyc\n")
    (workdir / ".env.example").write_text("KEY=\n")
    noise = workdir / ".cache"
    noise.mkdir()
    (noise / "junk.bin").write_bytes(b"x")

    reported = tools._snapshot_workdir_files(str(workdir))
    assert ".gitignore" in reported and ".env.example" in reported
    assert not any(name.startswith(".cache/") for name in reported), reported
    assert ".gitignore" in tools._created_file_sentinels(str(workdir), before)


def test_a_name_the_route_would_refuse_is_never_reported(tmp_path, monkeypatch):
    """A backslash or control character is legal in a POSIX filename but the
    download route rejects it, so the card must not offer it."""
    tools = _shared_setup_1(monkeypatch, tmp_path)
    workdir = Path(tools.get_sandbox_workdir("__LOCALID_badname"))
    before = tools._snapshot_workdir_files(str(workdir))
    for name in ("back\\slash.csv", "bell\x07.csv", "ok.csv"):
        try:
            (workdir / name).write_text("x")
        except OSError:
            continue

    reported = tools._snapshot_workdir_files(str(workdir))
    assert "ok.csv" in reported
    assert not any("\\" in name or any(ord(c) < 32 for c in name) for name in reported), reported
    del before


def test_deleting_a_chat_right_after_an_upgrade_finds_its_legacy_sandbox(tmp_path, monkeypatch):
    """The first thing a user does may be a delete, before any tool has run."""
    fake_home = tmp_path / "userprofile"
    fake_home.mkdir()
    _shared_setup_11(fake_home, monkeypatch, tmp_path)

    legacy = fake_home / "studio_sandbox" / "__LOCALID_oldchat"
    legacy.mkdir(parents = True)
    (legacy / "sales.csv").write_text("a,b\n")

    tools = _shared_setup_6()
    assert tools.remove_session_sandbox("__LOCALID_oldchat", delete_files = True) is True
    assert not legacy.exists()
    assert not (Path(tools.sandbox_root()) / "__LOCALID_oldchat").exists()


def test_a_configured_cache_that_holds_other_files_is_never_deleted(tmp_path, monkeypatch):
    """UNSLOTH_COMPILE_LOCATION is user-set. Pointed at a shared directory it
    would otherwise be rmtree'd at startup, taking whatever else lives there."""
    shared = tmp_path / "shared"
    (shared / "important").mkdir(parents = True)
    (shared / "important" / "notes.txt").write_text("user data")
    monkeypatch.setenv("UNSLOTH_COMPILE_LOCATION", str(shared))

    from utils import cache_cleanup

    cache_cleanup.clear_unsloth_compiled_cache()
    assert (shared / "important" / "notes.txt").read_text() == "user data"

    cache_cleanup.clear_unsloth_compiled_cache(preserve_patterns = ["Unsloth*Trainer.py"])
    assert (shared / "important" / "notes.txt").read_text() == "user data"


def test_an_unmarked_configured_cache_keeps_what_studio_did_not_write(tmp_path, monkeypatch):
    cache = tmp_path / "compiled_cache"
    cache.mkdir()
    (cache / "unsloth_compiled_module_gemma3.py").write_text("x = 1\n")
    (cache / "__pycache__").mkdir()
    monkeypatch.setenv("UNSLOTH_COMPILE_LOCATION", str(cache))

    from utils import cache_cleanup

    cache_cleanup.clear_unsloth_compiled_cache()
    assert not (cache / "unsloth_compiled_module_gemma3.py").exists()
    assert (cache / "__pycache__").is_dir()


def test_a_symlinked_session_cannot_serve_files_outside_the_sandbox(tmp_path, monkeypatch):
    """A symlinked session must not become a read root, or files under its target download."""
    fake_home = tmp_path / "userprofile"
    fake_home.mkdir()
    outside = tmp_path / "outside"
    outside.mkdir()
    (outside / "secret.txt").write_text("TOPSECRET")

    _shared_setup_11(fake_home, monkeypatch, tmp_path)

    legacy = fake_home / "studio_sandbox"
    legacy.mkdir(parents = True)
    (legacy / "__LOCALID_evil").symlink_to(outside)

    tools = _shared_setup_6()
    tools._migrate_legacy_sandbox(tools.sandbox_root())
    resolved = tools.resolve_sandbox_workdir("__LOCALID_evil")
    assert not (Path(tools.sandbox_root()) / "__LOCALID_evil").is_symlink()
    assert (legacy / "__LOCALID_evil").is_symlink(), "moved a link into the root"
    assert not Path(resolved).is_symlink()
    assert not str(Path(resolved).resolve()).startswith(str(outside.resolve()))
    assert not (Path(resolved) / "secret.txt").exists()


def test_the_executor_leaves_nothing_in_the_sandbox(tmp_path, monkeypatch):
    """The scratch script lives outside the sandbox, so a chat whose tools only printed holds just
    bookkeeping and an empty TMPDIR dir, removable without the opt-in."""
    tools = _shared_setup_1(monkeypatch, tmp_path)
    workdir = Path(tools.get_sandbox_workdir("__LOCALID_scratch"))
    tools._python_exec("print('hi')", session_id = "__LOCALID_scratch")
    assert sorted(p.name for p in workdir.iterdir() if p.name != ".cache") == [
        tools._SANDBOX_MARKER,
        tools._SANDBOX_TEMP_DIRNAME,
    ]
    assert list((workdir / tools._SANDBOX_TEMP_DIRNAME).iterdir()) == []

    assert tools.remove_session_sandbox("__LOCALID_scratch") is True
    assert not workdir.exists()


def test_a_file_under_the_scratch_dir_is_listed_and_blocks_removal(tmp_path, monkeypatch):
    """On Windows /tmp is the scratch dir, so files there must be listed and block removal."""
    inference, tools = _shared_setup_16(monkeypatch, tmp_path)
    session = "__LOCALID_scratch3"
    workdir = Path(tools.get_sandbox_workdir(session))
    scratch = Path(tools._sandbox_temp_dir(str(workdir)))
    (scratch / "report.csv").write_text("x")

    assert inference._sandbox_listing_names(str(workdir)) == [
        f"{tools._SANDBOX_TEMP_DIRNAME}/report.csv"
    ]
    assert tools.session_sandbox_has_files(session) is True
    assert tools.remove_session_sandbox(session) is False
    assert (scratch / "report.csv").is_file()


def test_a_real_file_still_blocks_removal(tmp_path, monkeypatch):
    """Only scratch is ignored; the user's own files still need the opt-in."""
    tools = _shared_setup_1(monkeypatch, tmp_path)
    workdir = Path(tools.get_sandbox_workdir("__LOCALID_keepit"))
    (workdir / "studio_exec_abc123.py").write_text("print(1)")
    (workdir / "sales.csv").write_text("a,b\n")

    assert tools.remove_session_sandbox("__LOCALID_keepit") is False
    assert (workdir / "sales.csv").is_file()


def test_the_download_route_serves_the_full_depth_under_the_scratch_dir(tmp_path, monkeypatch):
    """Card and route share one segment cap, so the route must discount the scratch dir."""
    HTTPException, tools = _shared_setup_19(monkeypatch, tmp_path)
    from routes.inference import _contained_sandbox_path

    tools._workdirs.clear()
    session = "__LOCALID_depth1"
    workdir = Path(tools.get_sandbox_workdir(session))
    scratch = Path(tools._sandbox_temp_dir(str(workdir)))
    (scratch / "a/b/c").mkdir(parents = True)
    (scratch / "a/b/c/result.csv").write_bytes(b"x")

    _, path = _contained_sandbox_path(session, f"{tools._SANDBOX_TEMP_DIRNAME}/a/b/c/result.csv")
    assert Path(path) == scratch / "a/b/c/result.csv"
    with pytest.raises(HTTPException):
        _contained_sandbox_path(session, f"{tools._SANDBOX_TEMP_DIRNAME}/a/b/c/d/toodeep.csv")


def test_only_the_real_scratch_dir_skips_a_path_segment(tmp_path, monkeypatch):
    """The walks never follow links, so the scratch-dir skip must key on the resolved directory."""
    HTTPException, tools = _shared_setup_19(monkeypatch, tmp_path)
    from routes import inference

    tools._workdirs.clear()
    session = "__LOCALID_depth2"
    workdir = Path(tools.get_sandbox_workdir(session))
    (workdir / "out/a/b/c").mkdir(parents = True)
    (workdir / "out/a/b/c/deep.csv").write_bytes(b"x")
    (workdir / "out/a/b/shallow.csv").write_bytes(b"x")
    try:
        (workdir / tools._SANDBOX_TEMP_DIRNAME).symlink_to(
            workdir / "out", target_is_directory = True
        )
    except (OSError, NotImplementedError):
        pytest.skip("symlinks unavailable (Windows without developer mode)")

    name = tools._SANDBOX_TEMP_DIRNAME
    assert f"{name}/a/b/c/deep.csv" not in inference._sandbox_listing_names(str(workdir))
    with pytest.raises(HTTPException):
        inference._contained_sandbox_path(session, f"{name}/a/b/c/deep.csv")
    inference._contained_sandbox_path(session, f"{name}/a/b/shallow.csv")


def test_the_listing_drops_a_directory_the_route_would_refuse(tmp_path, monkeypatch):
    monkeypatch.setenv("UNSLOTH_STUDIO_SANDBOX_HOME", str(tmp_path / "sb"))

    from core.inference import tools
    from routes.inference import _sandbox_listing_names

    tools._workdirs.clear()
    workdir = Path(tools.get_sandbox_workdir("__LOCALID_baddir"))
    try:
        bad = workdir / "back\\slash"
        bad.mkdir()
        (bad / "report.csv").write_text("x")
    except OSError:
        pytest.skip("filesystem rejects the name")
    (workdir / "ok.csv").write_text("x")

    names = _sandbox_listing_names(str(workdir))
    assert "ok.csv" in names
    assert not any("\\" in name for name in names), names


def test_a_user_file_named_like_scratch_is_kept(tmp_path, monkeypatch):
    """No filename is reserved: everything in the sandbox is the user's."""
    tools = _shared_setup_1(monkeypatch, tmp_path)
    workdir = Path(tools.get_sandbox_workdir("__LOCALID_prefix1"))
    (workdir / "studio_exec_results.csv").write_text("a,b\n")
    (workdir / "studio_exec_ab12cd.py").write_text("print(1)")

    snapshot = tools._snapshot_workdir_files(str(workdir))
    assert "studio_exec_results.csv" in snapshot
    assert "studio_exec_ab12cd.py" in snapshot
    assert tools.remove_session_sandbox("__LOCALID_prefix1") is False
    assert (workdir / "studio_exec_results.csv").is_file()
    assert (workdir / "studio_exec_ab12cd.py").is_file()


def test_an_existing_sandbox_override_keeps_its_permissions(tmp_path, monkeypatch):
    """UNSLOTH_STUDIO_SANDBOX_HOME can name a shared directory; locking it down
    to 0o700 would cut off everything else using it."""
    shared = tmp_path / "shared"
    shared.mkdir(mode = 0o755)
    before = shared.stat().st_mode & 0o777
    monkeypatch.setenv("UNSLOTH_STUDIO_SANDBOX_HOME", str(shared))

    from core.inference import tools

    tools._workdirs.clear()
    workdir = Path(tools.get_sandbox_workdir("__LOCALID_shared1"))
    assert (shared.stat().st_mode & 0o777) == before, "the shared root was re-permissioned"
    assert (workdir.stat().st_mode & 0o777) == 0o700


def test_a_sandbox_root_studio_creates_is_still_locked_down(tmp_path, monkeypatch):
    monkeypatch.setenv("UNSLOTH_STUDIO_SANDBOX_HOME", str(tmp_path / "fresh"))

    from core.inference import tools

    tools._workdirs.clear()
    tools.get_sandbox_workdir("__LOCALID_fresh11")
    assert ((tmp_path / "fresh").stat().st_mode & 0o777) == 0o700


def test_a_directory_of_plain_python_files_is_not_a_cache(tmp_path, monkeypatch):
    """A scripts directory of only .py files looks like a cache, so shape alone cannot decide."""
    scripts = tmp_path / "scripts"
    scripts.mkdir()
    (scripts / "helper.py").write_text("print(1)\n")
    (scripts / "__pycache__").mkdir()
    monkeypatch.setenv("UNSLOTH_COMPILE_LOCATION", str(scripts))

    from utils import cache_cleanup

    cache_cleanup.clear_unsloth_compiled_cache()
    assert (scripts / "helper.py").read_text() == "print(1)\n"


def test_a_marked_directory_is_cleared(tmp_path, monkeypatch):
    """Unsloth writes the marker when it creates the location."""
    cache = tmp_path / "compiled_cache"
    cache.mkdir()
    (cache / "helper.py").write_text("print(1)\n")

    from utils import cache_cleanup

    (cache / cache_cleanup.CACHE_MARKER).touch()
    monkeypatch.setenv("UNSLOTH_COMPILE_LOCATION", str(cache))
    cache_cleanup.clear_unsloth_compiled_cache()
    assert not (cache / "helper.py").exists()
    assert list(cache.iterdir()) == [cache / cache_cleanup.CACHE_MARKER]


def test_generated_modules_identify_a_cache_without_a_marker(tmp_path, monkeypatch):
    """An install that predates the marker is still cleaned, file by file."""
    cache = tmp_path / "old_cache"
    cache.mkdir()
    (cache / "unsloth_compiled_module_gemma3.py").write_text("x = 1\n", encoding = "utf-8")
    (cache / "UnslothCustomTrainer.py").write_text("class X: pass\n", encoding = "utf-8")
    monkeypatch.setenv("UNSLOTH_COMPILE_LOCATION", str(cache))

    from utils import cache_cleanup

    cache_cleanup.clear_unsloth_compiled_cache()
    assert not (cache / "unsloth_compiled_module_gemma3.py").exists()
    assert (cache / "UnslothCustomTrainer.py").is_file()


def test_studio_writes_the_marker_when_it_creates_the_location(tmp_path, monkeypatch):
    monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(tmp_path / "home"))
    cache_cleanup, pinned = _shared_setup_13(monkeypatch)
    assert (pinned / cache_cleanup.CACHE_MARKER).is_file()


def test_a_cached_sandbox_path_is_re_checked(tmp_path, monkeypatch):
    """A cached session path later swapped for a symlink must be re-checked before use."""
    root = tmp_path / "sb"
    outside = tmp_path / "outside"
    outside.mkdir()
    (outside / "secret.txt").write_text("TOPSECRET")
    tools = _shared_setup_3(monkeypatch, root)
    workdir = Path(tools.get_sandbox_workdir("__LOCALID_swapped"))
    assert workdir.is_dir()

    shutil.rmtree(workdir)
    workdir.symlink_to(outside)

    resolved = Path(tools.resolve_sandbox_workdir("__LOCALID_swapped"))
    assert resolved.name != "__LOCALID_swapped", resolved
    assert not (resolved / "secret.txt").exists()

    executing = Path(tools.get_sandbox_workdir("__LOCALID_swapped"))
    assert executing.name != "__LOCALID_swapped", executing
    assert not executing.is_symlink() and executing.is_dir()
    assert not (executing / "secret.txt").exists()


def test_a_shared_compile_location_loses_only_generated_files(tmp_path, monkeypatch):
    """UNSLOTH_COMPILE_LOCATION=$HOME/.cache after one compile made the whole
    directory look like ours; only the compiler's own output is."""
    shared = tmp_path / "dot_cache"
    (shared / "pip").mkdir(parents = True)
    (shared / "pip" / "wheel.whl").write_text("wheel")
    (shared / "notes.txt").write_text("keep me")
    (shared / "unsloth_compiled_module_gemma3.py").write_text("x = 1\n")
    monkeypatch.setenv("UNSLOTH_COMPILE_LOCATION", str(shared))

    from utils import cache_cleanup

    cache_cleanup.clear_unsloth_compiled_cache()
    assert shared.is_dir()
    assert (shared / "pip" / "wheel.whl").read_text() == "wheel"
    assert (shared / "notes.txt").read_text() == "keep me"
    assert not (shared / "unsloth_compiled_module_gemma3.py").exists()


def test_a_shared_compile_location_keeps_preserved_patterns(tmp_path, monkeypatch):
    shared = tmp_path / "dot_cache2"
    shared.mkdir()
    (shared / "UnslothSFTTrainer.py").write_text("trainer")
    (shared / "unsloth_compiled_module_llama.py").write_text("x = 1\n")
    (shared / "__pycache__").mkdir()
    monkeypatch.setenv("UNSLOTH_COMPILE_LOCATION", str(shared))

    from utils import cache_cleanup

    cache_cleanup.clear_unsloth_compiled_cache(preserve_patterns = ["Unsloth*Trainer.py"])
    assert (shared / "UnslothSFTTrainer.py").is_file()
    assert not (shared / "unsloth_compiled_module_llama.py").exists()
    assert (shared / "__pycache__").is_dir()


def test_a_marked_shared_directory_is_still_cleared_whole(tmp_path, monkeypatch):
    """The marker means Unsloth made the directory, so the old behaviour stands."""
    cache = tmp_path / "marked"
    cache.mkdir()

    from utils import cache_cleanup

    (cache / cache_cleanup.CACHE_MARKER).touch()
    (cache / "leftover.txt").write_text("x")
    monkeypatch.setenv("UNSLOTH_COMPILE_LOCATION", str(cache))
    cache_cleanup.clear_unsloth_compiled_cache()
    assert not (cache / "leftover.txt").exists()
    assert list(cache.iterdir()) == [cache / cache_cleanup.CACHE_MARKER]


def test_many_empty_directories_do_not_stall_the_snapshot(tmp_path, monkeypatch):
    """Directories never hit the file cap, so the walk needs its own budget."""
    tools = _shared_setup_1(monkeypatch, tmp_path)
    workdir = Path(tools.get_sandbox_workdir("__LOCALID_manydirs"))
    monkeypatch.setattr(tools, "_MAX_SNAPSHOT_DIRS", 5)
    for i in range(40):
        (workdir / f"out{i}").mkdir()

    visited = []
    real_walk = os.walk

    def counting_walk(top, *a, **kw):
        for entry in real_walk(top, *a, **kw):
            visited.append(entry[0])
            yield entry

    monkeypatch.setattr(tools.os, "walk", counting_walk)
    tools._snapshot_workdir_files(str(workdir))
    assert len(visited) <= 6, len(visited)


def test_a_files_marker_with_absurd_nesting_does_not_abort_the_turn(tmp_path):
    """json.loads raises RecursionError, which is not a ValueError."""
    from core.inference import tool_loop_controller

    payload = "[" * 200000 + "]" * 200000
    text = "tool output\n__FILES__:" + payload
    assert tool_loop_controller.strip_result_for_model(text) == text


def test_the_listing_walk_is_bounded_like_the_snapshot(tmp_path, monkeypatch):
    """A listing walk has the snapshot's directory budget, since it runs on a server worker."""
    from core.inference import tools
    from routes import inference

    sandbox = tmp_path / "sb"
    sandbox.mkdir()
    for i in range(40):
        (sandbox / f"out{i}").mkdir()

    visited = []
    real_walk = os.walk

    def counting_walk(top, *a, **kw):
        for entry in real_walk(top, *a, **kw):
            visited.append(entry[0])
            yield entry

    monkeypatch.setattr(inference, "_MAX_SNAPSHOT_DIRS", 5)
    monkeypatch.setattr(inference.os, "walk", counting_walk)
    inference._sandbox_listing_names(str(sandbox))
    assert len(visited) <= 6, len(visited)
    assert inference._MAX_SNAPSHOT_FILES == tools._MAX_SNAPSHOT_FILES


def test_the_listing_shows_a_user_file_named_like_scratch(tmp_path):
    """Nothing in the sandbox belongs to the executor, so nothing is hidden."""
    from routes import inference

    sandbox = tmp_path / "sb"
    sandbox.mkdir()
    (sandbox / "studio_exec_results.csv").write_text("a,b\n")
    (sandbox / "studio_exec_ab12cd.py").write_text("print(1)")

    names = inference._sandbox_listing_names(str(sandbox))
    assert names == ["studio_exec_ab12cd.py", "studio_exec_results.csv"], names


def test_clear_all_chats_can_take_the_files_too(tmp_path, monkeypatch):
    """DELETE /threads takes the files opt-in; a bulk clear without it strands sandboxes."""
    import inspect

    from routes import chat_history

    signature = inspect.signature(chat_history.clear_history)
    assert "delete_files" in signature.parameters
    assert signature.parameters["delete_files"].default is False

    source = inspect.getsource(chat_history.clear_history)
    assert "_remove_sandboxes(" in source
    assert "delete_files" in source.split("_remove_sandboxes(", 1)[1]


def test_a_failed_legacy_move_is_retried(tmp_path, monkeypatch):
    """A file locked by another process on Windows is retryable; giving up after
    one attempt strands it once the destination directory exists."""
    fake_home = tmp_path / "userprofile"
    legacy = fake_home / "studio_sandbox" / "__LOCALID_locked1"
    legacy.mkdir(parents = True)
    (legacy / "results.csv").write_text("a,b\n")

    monkeypatch.setenv("HOME", str(fake_home))
    monkeypatch.setenv("USERPROFILE", str(fake_home))
    monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(tmp_path / "studio_home"))
    monkeypatch.delenv("UNSLOTH_STUDIO_SANDBOX_HOME", raising = False)

    tools = _shared_setup_6()

    attempts = []
    real_move = tools.shutil.move

    def failing_move(source, target):
        attempts.append(source)
        if len(attempts) == 1:
            raise OSError(13, "in use by another process")
        return real_move(source, target)

    monkeypatch.setattr(tools.shutil, "move", failing_move)
    root = tools.sandbox_root()
    tools._migrate_legacy_sandbox(root)
    assert tools._legacy_sandbox_migrated is False, "gave up on a retryable failure"

    tools._migrate_legacy_sandbox(root)
    assert tools._legacy_sandbox_migrated is True
    assert (Path(root) / "__LOCALID_locked1" / "results.csv").read_text() == "a,b\n"


def test_a_collision_is_not_a_retryable_failure(tmp_path, monkeypatch):
    """A collision in the new root leaves the legacy copy for the user, and the migration is still done."""
    fake_home = tmp_path / "userprofile"
    legacy = fake_home / "studio_sandbox" / "__LOCALID_dupe123"
    legacy.mkdir(parents = True)
    (legacy / "old.csv").write_text("old\n")

    monkeypatch.setenv("HOME", str(fake_home))
    monkeypatch.setenv("USERPROFILE", str(fake_home))
    monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(tmp_path / "studio_home"))
    monkeypatch.delenv("UNSLOTH_STUDIO_SANDBOX_HOME", raising = False)

    tools = _shared_setup_6()
    root = Path(tools.sandbox_root())
    existing = root / "__LOCALID_dupe123"
    existing.mkdir(parents = True)
    (existing / tools._SANDBOX_MARKER).write_text("__LOCALID_dupe123", encoding = "utf-8")

    tools._migrate_legacy_sandbox(str(root))
    assert tools._legacy_sandbox_migrated is True
    assert (legacy / "old.csv").is_file(), "the legacy copy was not left behind"


def test_deleting_a_symlinked_session_spares_the_chat_it_points_at(tmp_path, monkeypatch):
    """realpath containment passes for a sibling, so a link could take another chat's files."""
    monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(tmp_path / "home"))
    tools = _shared_setup_7(monkeypatch)
    victim = Path(tools.get_sandbox_workdir("__LOCALID_victim1"))
    (victim / "report.csv").write_text("a,b\n")
    link = victim.parent / "__LOCALID_link111"
    link.symlink_to(victim)

    assert tools.remove_session_sandbox("__LOCALID_link111", delete_files = True) is True
    assert not link.exists(), "the stale link stayed behind"
    assert (victim / "report.csv").read_text() == "a,b\n", "the other chat lost its files"
    assert victim.is_dir()


def test_the_marker_survives_a_cache_clear(tmp_path, monkeypatch):
    """The marker must survive a cache clear, or our own cache is demoted to 'shared'."""
    monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(tmp_path / "home"))
    cache_cleanup, pinned = _shared_setup_13(monkeypatch)
    (pinned / "unsloth_compiled_module_gemma3.py").write_text("x = 1\n", encoding = "utf-8")

    cache_cleanup.clear_unsloth_compiled_cache()
    assert (pinned / cache_cleanup.CACHE_MARKER).is_file()
    assert not (pinned / "unsloth_compiled_module_gemma3.py").exists()

    (pinned / "__pycache__").mkdir()
    (pinned / "UnslothSFTTrainer.py").write_text("trainer\n", encoding = "utf-8")
    cache_cleanup.clear_unsloth_compiled_cache(preserve_patterns = ["Unsloth*Trainer.py"])
    assert not (pinned / "__pycache__").exists()
    assert (pinned / "UnslothSFTTrainer.py").is_file()
    assert (pinned / cache_cleanup.CACHE_MARKER).is_file()


def test_an_unrelated_cache_named_folder_in_the_cwd_is_not_ours(tmp_path, monkeypatch):
    """Unsloth runs from any working directory, so a name alone cannot license an rmtree."""
    launch_dir = tmp_path / "someproject"
    cache = launch_dir / "unsloth_compiled_cache"
    cache.mkdir(parents = True)
    (cache / "notes.txt").write_text("keep me")
    cache_cleanup = _shared_setup_21(launch_dir, monkeypatch)

    cache_cleanup.clear_unsloth_compiled_cache()
    assert (cache / "notes.txt").read_text() == "keep me"


def test_a_marked_cwd_cache_is_still_cleared(tmp_path, monkeypatch):
    launch_dir = tmp_path / "studioproject"
    cache = launch_dir / "unsloth_compiled_cache"
    cache.mkdir(parents = True)
    cache_cleanup = _shared_setup_21(launch_dir, monkeypatch)

    (cache / cache_cleanup.CACHE_MARKER).touch()
    (cache / "unsloth_compiled_module_gemma3.py").write_text("x = 1\n")
    cache_cleanup.clear_unsloth_compiled_cache()
    assert not (cache / "unsloth_compiled_module_gemma3.py").exists()


def test_a_user_python_file_is_never_executor_scratch(tmp_path, monkeypatch):
    """The executor's own file no longer lives in the sandbox, so no filename
    is reserved and studio_exec_results.py is just a file."""
    inference, tools = _shared_setup_16(monkeypatch, tmp_path)
    session = "__LOCALID_userpy1"
    result = tools._python_exec(
        "open('studio_exec_results.py','w').write('x = 1\\n')", session_id = session
    )
    assert "studio_exec_results.py" in result, result

    workdir = Path(tools.get_sandbox_workdir(session))
    assert sorted(
        p.name
        for p in workdir.iterdir()
        if p.name not in tools._INTERNAL_SANDBOX_FILES
        and p.name not in (tools._SANDBOX_TEMP_DIRNAME, ".cache")
    ) == ["studio_exec_results.py"]
    assert inference._sandbox_listing_names(str(workdir)) == ["studio_exec_results.py"]
    assert tools.remove_session_sandbox(session) is False
    assert (workdir / "studio_exec_results.py").is_file()


def test_a_sandbox_root_at_a_filesystem_root_still_isolates_chats(tmp_path, monkeypatch):
    """A root ending in a separator, like /, made every chat fail containment into _invalid."""
    from core.inference import tools

    assert tools._contained_in_root("/srv/chat", "/") is True
    assert tools._contained_in_root("/", "/") is False
    assert tools._contained_in_root("/etc/passwd", str(tmp_path)) is False

    root = tmp_path / "sb"
    monkeypatch.setenv("UNSLOTH_STUDIO_SANDBOX_HOME", str(root))
    tools._workdirs.clear()
    first = Path(tools.get_sandbox_workdir("__LOCALID_alpha1"))
    second = Path(tools.get_sandbox_workdir("__LOCALID_beta22"))
    assert first != second
    assert first.name != "_invalid" and second.name != "_invalid"


def test_containment_is_not_fooled_by_a_shared_name_prefix(tmp_path):
    """The old prefix test would also have accepted a sibling like sb-evil."""
    from core.inference import tools

    (tmp_path / "sb").mkdir()
    (tmp_path / "sb-evil").mkdir()
    assert tools._contained_in_root(str(tmp_path / "sb-evil"), str(tmp_path / "sb")) is False
    assert tools._contained_in_root(str(tmp_path / "sb" / "chat"), str(tmp_path / "sb")) is True


def test_studios_own_sandbox_bookkeeping_is_not_a_user_file(tmp_path, monkeypatch):
    """Studio's remap sidecar is not a user file; reporting it made streamed results differ."""
    inference, tools = _shared_setup_16(monkeypatch, tmp_path)
    workdir = Path(tools.get_sandbox_workdir("__LOCALID_remap01"))
    (workdir / ".unsloth_sandbox_remap.json").write_text("{}")
    (workdir / ".gitignore").write_text("*.pyc\n")

    snapshot = tools._snapshot_workdir_files(str(workdir))
    assert ".unsloth_sandbox_remap.json" not in snapshot
    assert ".gitignore" in snapshot
    assert inference._sandbox_listing_names(str(workdir)) == [".gitignore"]


def test_a_module_written_by_an_earlier_call_is_importable(tmp_path, monkeypatch):
    """The scratch script is sys.path[0], so moving it out of the sandbox broke import helper."""
    tools = _shared_setup_1(monkeypatch, tmp_path)
    session = "__LOCALID_import1"
    tools._python_exec('open("helper.py", "w").write("VALUE = 42")', session_id = session)
    result = tools._python_exec("import helper; print(helper.VALUE)", session_id = session)
    assert "42" in result, result

    where = tools._python_exec("print(__file__)", session_id = session)
    workdir = tools.get_sandbox_workdir(session)
    assert workdir in where, where


def test_the_scratch_script_is_never_reported_as_a_file(tmp_path, monkeypatch):
    """Excluded by its exact name for this one call, so a tool writing
    studio_exec_results.py still keeps it."""
    tools = _shared_setup_1(monkeypatch, tmp_path)
    session = "__LOCALID_scratch2"
    result = tools._python_exec(
        'open("studio_exec_results.py", "w").write("x = 1")', session_id = session
    )
    files = result.split("__FILES__:")[1]
    assert "studio_exec_results.py" in files
    workdir = Path(tools.get_sandbox_workdir(session))
    assert sorted(
        p.name
        for p in workdir.iterdir()
        if p.name not in tools._INTERNAL_SANDBOX_FILES
        and p.name not in (tools._SANDBOX_TEMP_DIRNAME, ".cache")
    ) == ["studio_exec_results.py"]
    assert json.loads(files) == [{"name": "studio_exec_results.py", "size": 5}]


def test_a_turn_without_a_chat_id_reports_no_files(tmp_path, monkeypatch):
    """Chat-less turns share _default; a card pinned there would expose the next chat's files."""
    tools = _shared_setup_1(monkeypatch, tmp_path)
    result = tools._python_exec('open("first.csv", "w").write("a")')
    assert "__FILES__" not in result, result
    assert "__IMAGES__" not in result
    bash_result = tools._bash_exec("printf a > second.csv")
    assert "__FILES__" not in bash_result, bash_result


def test_an_unowned_cwd_cache_is_not_put_on_the_import_path(tmp_path, monkeypatch):
    """Registering it would shadow real dependencies for every spawned worker."""
    launch_dir = tmp_path / "someproject"
    cache = launch_dir / "unsloth_compiled_cache"
    cache.mkdir(parents = True)
    (cache / "numpy.py").write_text("raise SystemExit('shadowed')\n")
    monkeypatch.chdir(launch_dir)
    monkeypatch.delenv("UNSLOTH_COMPILE_LOCATION", raising = False)
    monkeypatch.setenv("PYTHONPATH", "")

    from utils import cache_cleanup

    before = list(sys.path)
    try:
        cache_cleanup.register_compiled_cache_on_path()
        assert str(cache.resolve()) not in sys.path
        assert str(cache.resolve()) not in os.environ.get("PYTHONPATH", "")
    finally:
        sys.path[:] = before


def test_a_marked_cwd_cache_is_still_registered(tmp_path, monkeypatch):
    launch_dir = tmp_path / "studioproject"
    cache = launch_dir / "unsloth_compiled_cache"
    cache.mkdir(parents = True)
    cache_cleanup = _shared_setup_21(launch_dir, monkeypatch)

    (cache / cache_cleanup.CACHE_MARKER).touch()
    before = list(sys.path)
    try:
        cache_cleanup.register_compiled_cache_on_path()
        assert str(cache.resolve()) in sys.path
    finally:
        sys.path[:] = before


def test_a_chat_deleted_mid_call_keeps_its_sandbox(tmp_path, monkeypatch):
    """Deleting the folder mid-call would unlink the running tool's cwd and fail every write."""
    import threading

    tools = _shared_setup_1(monkeypatch, tmp_path)
    session = "__LOCALID_busy111"
    workdir = Path(tools.get_sandbox_workdir(session))
    started, may_finish = threading.Event(), threading.Event()
    removed = {}

    def run_tool():
        with tools._session_in_flight(session):
            started.set()
            may_finish.wait(timeout = 10)

    worker = threading.Thread(target = run_tool)
    worker.start()
    try:
        assert started.wait(timeout = 10)
        removed["during"] = tools.remove_session_sandbox(session)
        assert removed["during"] is False, "the sandbox went out from under a running tool"
        assert workdir.is_dir()
    finally:
        may_finish.set()
        worker.join(timeout = 10)

    assert not workdir.exists()


def test_the_executor_marks_its_session_busy():
    """The guard has to sit on the dispatch both executors go through."""
    import inspect

    from core.inference import tools

    source = inspect.getsource(tools.execute_tool)
    for call in ("_python_exec(", "_bash_exec("):
        before = source.split(call)[0]
        assert "_session_in_flight" in before.rsplit("if name ==", 1)[-1], call


def test_a_pre_existing_compile_directory_is_never_marked_as_ours(tmp_path, monkeypatch):
    """mkdir(exist_ok=True) passes on a pre-existing dir, which must never get our marker."""
    monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(tmp_path / "studio"))
    monkeypatch.delenv("UNSLOTH_COMPILE_LOCATION", raising = False)

    from utils import cache_cleanup
    from utils.paths import storage_roots

    pinned = Path(storage_roots.cache_root()).parent / "compiled_cache"
    pinned.mkdir(parents = True)
    (pinned / "someones_notes.txt").write_text("keep me")

    storage_roots.setup_cache_env()
    assert Path(os.environ["UNSLOTH_COMPILE_LOCATION"]) == pinned
    assert not (pinned / cache_cleanup.CACHE_MARKER).exists(), "claimed a directory it found"

    cache_cleanup.clear_unsloth_compiled_cache()
    assert (pinned / "someones_notes.txt").read_text() == "keep me"


def test_a_directory_studio_creates_is_marked(tmp_path, monkeypatch):
    monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(tmp_path / "fresh"))
    cache_cleanup, pinned = _shared_setup_13(monkeypatch)
    assert (pinned / cache_cleanup.CACHE_MARKER).is_file()


def test_removal_and_the_busy_check_are_one_decision(tmp_path, monkeypatch):
    """The busy check and the removal must be one decision, or a tool can start in between."""
    import threading
    import time

    tools = _shared_setup_1(monkeypatch, tmp_path)
    session = "__LOCALID_race111"
    workdir = Path(tools.get_sandbox_workdir(session))
    started = threading.Event()
    entered = threading.Event()

    real_rmtree = tools.shutil.rmtree

    def slow_rmtree(path, **kwargs):
        entered.set()
        time.sleep(0.3)
        return real_rmtree(path, **kwargs)

    monkeypatch.setattr(tools.shutil, "rmtree", slow_rmtree)
    result = {}

    def remover():
        result["removed"] = tools.remove_session_sandbox(session)

    def tool():
        entered.wait(timeout = 5)
        with tools._session_in_flight(session):
            started.set()
            time.sleep(0.05)

    threads = [threading.Thread(target = remover), threading.Thread(target = tool)]
    for t in threads:
        t.start()
    for t in threads:
        t.join(timeout = 10)

    assert started.is_set()
    assert result["removed"] is True
    assert tools._active_sessions == {}


def test_the_delete_dialog_offers_the_same_choice_for_a_chat():
    """Without the delete switch, every deleted chat that wrote files strands its sandbox."""
    root = Path(__file__).resolve().parents[2] / "frontend" / "src"
    sidebar = (root / "components" / "app-sidebar.tsx").read_text(encoding = "utf-8")
    assert "shouldDeleteChatFiles" in sidebar
    assert "deleteChatWithCleanup(target.item, {" in sidebar

    api = (root / "features" / "chat" / "api" / "chat-api.ts").read_text(encoding = "utf-8")
    assert "delete_files: !!args.deleteFiles" in api


def test_a_delete_during_a_call_happens_once_the_call_ends(tmp_path, monkeypatch):
    """The thread is already gone, so a dropped deferred removal would strand the folder."""
    tools = _shared_setup_1(monkeypatch, tmp_path)
    tools._pending_removals.clear()
    session = "__LOCALID_defer11"
    workdir = Path(tools.get_sandbox_workdir(session))
    (workdir / "report.csv").write_text("a,b\n")

    with tools._session_in_flight(session):
        assert tools.remove_session_sandbox(session, delete_files = True) is False
        assert workdir.is_dir(), "removed under a running tool"
    assert not workdir.exists()
    assert tools._pending_removals == {}


def test_a_queued_delete_keeps_the_strongest_request(tmp_path, monkeypatch):
    tools = _shared_setup_1(monkeypatch, tmp_path)
    tools._pending_removals.clear()
    session = "__LOCALID_defer22"
    workdir = Path(tools.get_sandbox_workdir(session))
    (workdir / "report.csv").write_text("a,b\n")

    with tools._session_in_flight(session):
        tools.remove_session_sandbox(session, delete_files = False)
        tools.remove_session_sandbox(session, delete_files = True)
    assert not workdir.exists()


def test_a_symlinked_cache_location_is_still_cleared(tmp_path, monkeypatch):
    """rmtree refuses a symlink, and ignore_errors made that silent."""
    real = tmp_path / "real_cache"
    real.mkdir()
    link = tmp_path / "link_cache"
    link.symlink_to(real, target_is_directory = True)

    from utils import cache_cleanup

    (real / cache_cleanup.CACHE_MARKER).touch()
    (real / "unsloth_compiled_module_gemma3.py").write_text("x = 1\n")
    monkeypatch.setenv("UNSLOTH_COMPILE_LOCATION", str(link))

    cache_cleanup.clear_unsloth_compiled_cache()
    assert not (real / "unsloth_compiled_module_gemma3.py").exists()
    assert (real / cache_cleanup.CACHE_MARKER).is_file(), "still ours next time"


def test_the_delete_switch_only_appears_where_it_works():
    """A training run has no sandbox of its own; a chat always does."""
    sidebar = (
        Path(__file__).resolve().parents[2] / "frontend" / "src" / "components" / "app-sidebar.tsx"
    ).read_text(encoding = "utf-8")
    assert "function deleteTargetHasFiles" in sidebar
    assert '"training"' not in sidebar.split("function deleteTargetHasFiles")[1][:400]
    assert "{deleteTargetHasFiles(confirmingDelete) ? (" in sidebar
    assert "function openDeleteDialog" in sidebar
    assert "setConfirmingDelete({ kind:" not in sidebar


def test_a_symlinked_builtin_cache_is_not_deleted_through(tmp_path, monkeypatch):
    """A built-in path proves ownership of the directory, not of a link's target."""
    victim = tmp_path / "someones_files"
    victim.mkdir()
    (victim / "thesis.txt").write_text("years of work")

    from utils import cache_cleanup

    link = tmp_path / "unsloth_compiled_cache"
    link.symlink_to(victim, target_is_directory = True)
    monkeypatch.setattr(cache_cleanup, "_CACHE_DIRS", [link])
    monkeypatch.delenv("UNSLOTH_COMPILE_LOCATION", raising = False)
    monkeypatch.chdir(tmp_path / "..")

    cache_cleanup.clear_unsloth_compiled_cache()
    assert (victim / "thesis.txt").read_text() == "years of work"


def test_a_symlinked_builtin_cache_with_the_marker_is_cleared(tmp_path, monkeypatch):
    real = tmp_path / "real_cache"
    real.mkdir()

    from utils import cache_cleanup

    (real / cache_cleanup.CACHE_MARKER).touch()
    (real / "unsloth_compiled_module_gemma3.py").write_text("x = 1\n")
    link = tmp_path / "unsloth_compiled_cache"
    link.symlink_to(real, target_is_directory = True)
    monkeypatch.setattr(cache_cleanup, "_CACHE_DIRS", [link])
    monkeypatch.delenv("UNSLOTH_COMPILE_LOCATION", raising = False)

    cache_cleanup.clear_unsloth_compiled_cache()
    assert not (real / "unsloth_compiled_module_gemma3.py").exists()


def test_the_invalid_fallback_cannot_be_pointed_out_of_the_sandbox(tmp_path, monkeypatch):
    """Tool code runs inside the sandbox, so it can plant this link; every
    unchecked request would then read from wherever it points."""
    outside = tmp_path / "outside"
    outside.mkdir()
    (outside / "id_rsa").write_text("PRIVATE KEY")
    root = tmp_path / "sb"
    root.mkdir()
    (root / "_invalid").symlink_to(outside, target_is_directory = True)
    tools = _shared_setup_3(monkeypatch, root)
    resolved = Path(tools.resolve_sandbox_workdir("../escape"))
    assert not (resolved / "id_rsa").exists(), resolved
    assert outside.is_dir() and (outside / "id_rsa").is_file(), "the target was touched"

    executing = Path(tools.get_sandbox_workdir("../escape"))
    assert not (executing / "id_rsa").exists(), executing


def test_the_default_fallback_is_contained_too(tmp_path, monkeypatch):
    outside = tmp_path / "elsewhere"
    outside.mkdir()
    (outside / "secret.txt").write_text("TOPSECRET")
    root = tmp_path / "sb"
    root.mkdir()
    (root / "_default").symlink_to(outside, target_is_directory = True)
    tools = _shared_setup_3(monkeypatch, root)
    resolved = Path(tools.resolve_sandbox_workdir(None))
    assert not (resolved / "secret.txt").exists(), resolved


def test_deleting_a_big_sandbox_does_not_hold_the_tool_lock(tmp_path, monkeypatch):
    """rmtree of a large sandbox must run outside the tool lock, or every tool start blocks."""
    import threading
    import time

    tools = _shared_setup_1(monkeypatch, tmp_path)
    session = "__LOCALID_bigdel1"
    workdir = Path(tools.get_sandbox_workdir(session))
    (workdir / "data.bin").write_bytes(b"x" * 1024)

    slow = threading.Event()
    real_rmtree = tools.shutil.rmtree

    def slow_rmtree(path, **kw):
        slow.set()
        time.sleep(0.5)
        return real_rmtree(path, **kw)

    monkeypatch.setattr(tools.shutil, "rmtree", slow_rmtree)
    assert tools.remove_session_sandbox(session, delete_files = True) is True
    assert not workdir.exists(), "the session directory is gone immediately"

    assert slow.wait(timeout = 5)
    started = time.monotonic()
    with tools._session_in_flight("__LOCALID_other12"):
        pass
    assert time.monotonic() - started < 0.3, "a tool start waited for the delete"


def test_a_chat_whose_id_looks_like_a_project_is_still_cleaned(tmp_path, monkeypatch):
    """A chat id with the project prefix but no project still gets an ordinary sandbox dir."""
    tools = _shared_setup_1(monkeypatch, tmp_path)
    monkeypatch.setattr(tools, "_get_project_workdir", lambda session_id: None)
    session = f"{tools._PROJECT_SESSION_PREFIX}notreal123"
    workdir = Path(tools.get_sandbox_workdir(session))
    assert workdir.is_dir()

    assert tools.remove_session_sandbox(session) is True
    assert not workdir.exists()


def test_a_real_project_workspace_is_still_left_alone(tmp_path, monkeypatch):
    tools = _shared_setup_1(monkeypatch, tmp_path)
    shared = tmp_path / "project_workspace"
    shared.mkdir()
    monkeypatch.setattr(tools, "_get_project_workdir", lambda session_id: str(shared))
    session = f"{tools._PROJECT_SESSION_PREFIX}real123"
    assert tools.remove_session_sandbox(session, delete_files = True) is False
    assert shared.is_dir(), "a shared project workspace was removed"


def test_a_foreign_tool_result_keeps_its_own_fields():
    """A foreign tool result with text and sessionId must not be reduced to its text."""
    contract = (
        Path(__file__).resolve().parents[2]
        / "frontend"
        / "src"
        / "components"
        / "assistant-ui"
        / "sandbox-files.ts"
    ).read_text(encoding = "utf-8")
    predicate = contract.split("export function isSandboxToolResult(", 1)[1].split("\n}", 1)[0]
    assert "Array.isArray(v.images)" in predicate, predicate


def test_a_shared_roots_own_folder_is_never_deleted(tmp_path, monkeypatch):
    """An id may name a shared root's own folder that we did not create, so never delete it."""
    root = tmp_path / "shared"
    root.mkdir()
    theirs = root / "invoices"
    theirs.mkdir()
    (theirs / "2026.pdf").write_text("money", encoding = "utf-8")
    tools = _shared_setup_3(monkeypatch, root)
    assert tools.remove_session_sandbox("invoices", delete_files = True) is False
    assert (theirs / "2026.pdf").is_file()

    (root / "empty").mkdir()
    assert tools.remove_session_sandbox("empty") is False
    assert (root / "empty").is_dir()


def test_a_sandbox_we_created_in_a_shared_root_is_still_removable(tmp_path, monkeypatch):
    root = tmp_path / "shared"
    root.mkdir()
    tools = _shared_setup_3(monkeypatch, root)
    workdir = Path(tools.get_sandbox_workdir("__LOCALID_mine11"))
    assert (workdir / tools._SANDBOX_MARKER).is_file()
    assert tools.remove_session_sandbox("__LOCALID_mine11") is True
    assert not workdir.exists()

    workdir = Path(tools.get_sandbox_workdir("__LOCALID_mine22"))
    (workdir / "report.csv").write_text("a,b\n", encoding = "utf-8")
    assert tools.remove_session_sandbox("__LOCALID_mine22", delete_files = True) is True


def test_two_ids_differing_only_in_case_share_the_busy_check(tmp_path, monkeypatch):
    """Case-only variants are one directory on Windows and macOS, so they share a busy check."""
    tools = _shared_setup_1(monkeypatch, tmp_path)
    tools._pending_removals.clear()
    workdir = Path(tools.get_sandbox_workdir("ChatCase"))
    (workdir / "report.csv").write_text("a,b\n", encoding = "utf-8")

    with tools._session_in_flight("chatcase"):
        assert tools.remove_session_sandbox("ChatCase", delete_files = True) is False
        assert workdir.is_dir(), "removed under a running tool"
    assert not workdir.exists()
    assert tools._pending_removals == {}


def test_an_existing_folder_in_a_shared_root_is_never_claimed(tmp_path, monkeypatch):
    """A chat id can name something already in there, and running a tool in it
    must not make it ours to delete."""
    root = tmp_path / "shared"
    root.mkdir()
    theirs = root / "taxes"
    theirs.mkdir()
    (theirs / "2026.pdf").write_text("money", encoding = "utf-8")
    tools = _shared_setup_3(monkeypatch, root)
    workdir = Path(tools.get_sandbox_workdir("taxes"))
    assert workdir != theirs, "ran the tool inside a folder we did not create"
    assert workdir.name.startswith("taxes-")
    assert not (theirs / tools._SANDBOX_MARKER).exists(), "claimed a folder we did not create"
    assert tools.remove_session_sandbox("taxes", delete_files = True) is True
    assert (theirs / "2026.pdf").is_file()


def test_both_case_variants_are_removed_when_their_calls_end(tmp_path, monkeypatch):
    """Case variants share one key but are two directories on case-sensitive disks; keep both."""
    import time

    tools = _shared_setup_1(monkeypatch, tmp_path)
    tools._pending_removals.clear()
    lower = Path(tools.get_sandbox_workdir("casepair"))
    upper = Path(tools.get_sandbox_workdir("CasePair"))
    if lower == upper:
        pytest.skip("case-insensitive filesystem: one directory, nothing to strand")
    (lower / "a.csv").write_text("a\n", encoding = "utf-8")
    (upper / "b.csv").write_text("b\n", encoding = "utf-8")

    with tools._session_in_flight("casepair"), tools._session_in_flight("CasePair"):
        assert tools.remove_session_sandbox("casepair", delete_files = True) is False
        assert tools.remove_session_sandbox("CasePair", delete_files = True) is False
    for _ in range(50):
        if not lower.exists() and not upper.exists():
            break
        time.sleep(0.05)
    assert not lower.exists() and not upper.exists()
    assert tools._pending_removals == {}


def test_a_foreign_fallback_link_is_left_where_it_stands(tmp_path, monkeypatch):
    """At a user-chosen root, _default is their entry: never follow it, and never unlink it."""
    root = tmp_path / "shared"
    root.mkdir()
    outside = tmp_path / "elsewhere"
    outside.mkdir()
    (outside / "secret.txt").write_text("TOPSECRET", encoding = "utf-8")
    (root / "_default").symlink_to(outside, target_is_directory = True)
    tools = _shared_setup_3(monkeypatch, root)
    resolved = Path(tools.resolve_sandbox_workdir(None))
    assert not (resolved / "secret.txt").exists(), resolved
    assert (root / "_default").is_symlink(), "an entry we do not own was unlinked"

    workdir = Path(tools.get_sandbox_workdir(None))
    assert not (workdir / "secret.txt").exists(), workdir
    assert (root / "_default").is_symlink()


def test_our_own_fallback_link_is_still_dropped(tmp_path, monkeypatch):
    """At our own root, nothing else put the link there, so it is dropped and the name kept."""
    monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(tmp_path / "home"))
    tools = _shared_setup_7(monkeypatch)
    root = Path(tools.sandbox_root())
    root.mkdir(parents = True, exist_ok = True)
    outside = tmp_path / "elsewhere"
    outside.mkdir()
    (root / "_default").symlink_to(outside, target_is_directory = True)

    workdir = Path(tools.get_sandbox_workdir(None))
    assert workdir == root / "_default"
    assert workdir.is_dir() and not workdir.is_symlink()


def test_a_pre_existing_folder_keeps_its_permissions(tmp_path, monkeypatch):
    """Running a tool in a shared root must not re-permission someone's folder."""
    if os.name == "nt":
        pytest.skip("POSIX permission bits")
    root = tmp_path / "shared"
    root.mkdir()
    theirs = root / "team"
    theirs.mkdir()
    os.chmod(theirs, 0o755)
    tools = _shared_setup_3(monkeypatch, root)
    assert Path(tools.get_sandbox_workdir("team")) != theirs
    assert oct(theirs.stat().st_mode)[-3:] == "755", "an unowned folder was locked down"

    mine = Path(tools.get_sandbox_workdir("__LOCALID_mine33"))
    assert oct(mine.stat().st_mode)[-3:] == "700"


def test_a_link_in_a_shared_root_is_left_alone_by_a_delete(tmp_path, monkeypatch):
    """A chat id can name a user's entry in a shared root, which a delete must not remove."""
    root = tmp_path / "shared"
    root.mkdir()
    elsewhere = tmp_path / "elsewhere"
    elsewhere.mkdir()
    (root / "notes").symlink_to(elsewhere, target_is_directory = True)
    tools = _shared_setup_3(monkeypatch, root)
    assert tools.remove_session_sandbox("notes", delete_files = True) is False
    assert (root / "notes").is_symlink(), "an entry we did not create was unlinked"
    assert elsewhere.is_dir()


def test_a_case_variant_chat_gets_its_own_directory(tmp_path, monkeypatch):
    """One name on Windows and on a default macOS volume: sharing it means
    either chat's delete takes the other's files."""
    tools = _shared_setup_1(monkeypatch, tmp_path)
    first = Path(tools.get_sandbox_workdir("CaseOwn"))
    (first / "report.csv").write_text("a,b\n", encoding = "utf-8")
    assert (first / tools._SANDBOX_MARKER).read_text(encoding = "utf-8") == "CaseOwn"

    root = first.parent
    collision = root / "caseown"
    if not collision.exists():
        collision.mkdir()
        (collision / tools._SANDBOX_MARKER).write_text("CaseOwn", encoding = "utf-8")
        (collision / "report.csv").write_text("a,b\n", encoding = "utf-8")

    second = Path(tools._session_dir(str(root), "caseown"))
    assert second.name == "caseown-" + hashlib.sha256(b"caseown").hexdigest()[:8]

    assert tools.remove_session_sandbox("caseown", delete_files = True) is False
    assert (collision / "report.csv").is_file()
    assert tools.remove_session_sandbox("CaseOwn", delete_files = True) is True


def test_the_delete_switch_does_not_promise_project_files():
    """Chat deletion does not touch the project workspace, so the copy must not claim it."""
    # The copy is the contract: check where the sidebar copy lives, not across the whole tree.
    assert "This chat's own sandbox folder is removed from disk." in _frontend_copy_text()
    assert "Anything this chat's tools wrote is removed from disk." not in _frontend_src_text()


def test_a_tool_cannot_forge_its_way_into_owning_a_folder(tmp_path, monkeypatch):
    """The marker is writable by sandboxed code, so never run in a pre-existing folder."""
    root = tmp_path / "shared"
    import time

    root.mkdir()
    theirs = root / "photos"
    theirs.mkdir()
    (theirs / "wedding.jpg").write_text("jpeg", encoding = "utf-8")
    tools = _shared_setup_3(monkeypatch, root)
    workdir = Path(tools.get_sandbox_workdir("photos"))
    assert workdir != theirs, "ran the tool inside a folder we did not create"
    assert not (theirs / tools._SANDBOX_MARKER).exists()
    (workdir / "plot.png").write_text("png", encoding = "utf-8")
    assert tools.remove_session_sandbox("photos", delete_files = True) is True
    for _ in range(50):
        if not workdir.exists():
            break
        time.sleep(0.05)
    assert not workdir.exists()
    assert (theirs / "wedding.jpg").is_file()


def test_two_ids_racing_for_one_name_do_not_share_it(tmp_path, monkeypatch):
    """Both can see an unowned name before either writes its marker."""
    tools = _shared_setup_1(monkeypatch, tmp_path)
    root = tools.sandbox_root()
    os.makedirs(root, exist_ok = True)
    first = Path(tools._ensure_session_dir(root, "RaceId"))
    plain = Path(root) / "raceid"
    if not plain.exists():
        plain.mkdir()
        (plain / tools._SANDBOX_MARKER).write_text("RaceId", encoding = "utf-8")
    second = Path(tools._ensure_session_dir(root, "raceid"))
    assert second != first and second != plain
    assert (second / tools._SANDBOX_MARKER).read_text(encoding = "utf-8") == "raceid"


def test_a_migrated_sandbox_stays_deletable_in_an_overridden_root(tmp_path, monkeypatch):
    """The move must record that the folder came from us, or the chat can never remove it."""
    home = tmp_path / "userhome"
    home.mkdir()
    monkeypatch.setenv("HOME", str(home))
    monkeypatch.setenv("USERPROFILE", str(home))
    legacy = home / "studio_sandbox" / "__LOCALID_moved11"
    legacy.mkdir(parents = True)
    (legacy / "notes.txt").write_text("mine", encoding = "utf-8")
    root = tmp_path / "shared"
    root.mkdir()
    tools = _shared_setup_3(monkeypatch, root)
    tools._legacy_sandbox_migrated = False
    tools._migrate_legacy_sandbox(str(root))

    moved = root / "__LOCALID_moved11"
    assert (moved / "notes.txt").is_file()
    assert (moved / tools._SANDBOX_MARKER).is_file(), "the migrated sandbox lost its claim"
    assert tools.remove_session_sandbox("__LOCALID_moved11", delete_files = True) is True


def test_a_name_the_download_url_cannot_carry_is_not_advertised():
    """A non-UTF-8 byte arrives as a lone surrogate, which encodeURIComponent throws on."""
    from core.inference import tools

    assert tools._servable_segment("report.csv")
    assert not tools._servable_segment("bad\udcffname.csv")
    assert not tools._servable_segment("\ud800.txt")


def test_a_tool_writing_over_the_marker_does_not_lose_its_files(tmp_path, monkeypatch):
    """A tool can overwrite the marker; a restart must not strand files written there."""
    monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(tmp_path / "home"))
    tools = _shared_setup_7(monkeypatch)
    workdir = Path(tools.get_sandbox_workdir("__LOCALID_clob11"))
    (workdir / "results.csv").write_text("a,b\n", encoding = "utf-8")
    (workdir / tools._SANDBOX_MARKER).write_text("Traceback: not an id\n", encoding = "utf-8")

    tools._workdirs.clear()
    again = Path(tools.get_sandbox_workdir("__LOCALID_clob11"))
    assert again == workdir, "the chat was sent to a new directory"
    assert (again / "results.csv").is_file()
    assert (again / tools._SANDBOX_MARKER).read_text(encoding = "utf-8") == "__LOCALID_clob11"
    assert Path(tools.resolve_sandbox_workdir("__LOCALID_clob11")) == workdir


def test_a_legacy_name_taken_in_a_shared_root_moves_beside_it(tmp_path, monkeypatch):
    """A legacy name taken by someone's folder moves the session beside it, not into it."""
    fake_home = tmp_path / "userprofile"
    legacy = fake_home / "studio_sandbox" / "notes"
    legacy.mkdir(parents = True)
    (legacy / "mine.csv").write_text("mine", encoding = "utf-8")

    monkeypatch.setenv("HOME", str(fake_home))
    monkeypatch.setenv("USERPROFILE", str(fake_home))
    root = tmp_path / "shared"
    root.mkdir()
    theirs = root / "notes"
    theirs.mkdir()
    (theirs / "theirs.txt").write_text("theirs", encoding = "utf-8")
    tools = _shared_setup_3(monkeypatch, root)
    tools._legacy_sandbox_migrated = False
    tools._migrate_legacy_sandbox(str(root))

    assert (theirs / "theirs.txt").is_file()
    assert not (theirs / "mine.csv").exists(), "merged into a folder we do not own"
    moved = Path(tools.get_sandbox_workdir("notes"))
    assert moved != theirs
    assert (moved / "mine.csv").read_text(encoding = "utf-8") == "mine"


def test_an_empty_compile_location_is_not_an_override(tmp_path, monkeypatch):
    """An inherited KEY= would pin the cache to "", which puts an empty entry on
    sys.path and sends the compiler to the system temp directory."""
    monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(tmp_path / "home"))
    monkeypatch.setenv("UNSLOTH_COMPILE_LOCATION", "")

    from utils.paths import storage_roots

    storage_roots.setup_cache_env()
    pinned = os.environ["UNSLOTH_COMPILE_LOCATION"]
    assert pinned.strip(), "the cache was left unpinned"
    assert Path(pinned).is_dir()


def test_an_id_the_filesystem_cannot_hold_still_gets_its_own_directory(tmp_path, monkeypatch):
    """Unusable API-client ids each get their own directory, not one shared bucket."""
    monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(tmp_path / "home"))
    tools = _shared_setup_1(monkeypatch, tmp_path)
    first = Path(tools.get_sandbox_workdir("chat.one"))
    second = Path(tools.get_sandbox_workdir("chat.two"))
    assert first != second
    assert first.name.startswith("_id-") and second.name.startswith("_id-")
    (first / "mine.csv").write_text("mine", encoding = "utf-8")

    tools._workdirs.clear()
    assert Path(tools.resolve_sandbox_workdir("chat.one")) == first
    escape = Path(tools.get_sandbox_workdir("../../etc"))
    assert escape.parent == Path(tools.sandbox_root())


def test_a_foreign_folder_is_not_taken_for_an_interrupted_move(tmp_path, monkeypatch):
    """Name overlap is not provenance; only the record written before a move shows ownership."""
    fake_home = tmp_path / "userprofile"
    legacy = fake_home / "studio_sandbox" / "shared_name"
    legacy.mkdir(parents = True)
    (legacy / "notes.txt").write_text("mine", encoding = "utf-8")

    monkeypatch.setenv("HOME", str(fake_home))
    monkeypatch.setenv("USERPROFILE", str(fake_home))
    monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(tmp_path / "home"))
    root = tmp_path / "shared"
    root.mkdir()
    theirs = root / "shared_name"
    theirs.mkdir()
    (theirs / "notes.txt").write_text("theirs", encoding = "utf-8")
    tools = _shared_setup_3(monkeypatch, root)
    tools._legacy_sandbox_migrated = False
    tools._migrate_legacy_sandbox(str(root))

    assert not (theirs / tools._SANDBOX_MARKER).exists(), "claimed a folder we never made"
    assert (theirs / "notes.txt").read_text(encoding = "utf-8") == "theirs"


def test_a_literal_id_cannot_take_a_derived_name(tmp_path, monkeypatch):
    """A literal id must not take the _id-<hash> name that unusable ids resolve to."""
    monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(tmp_path / "home"))
    tools = _shared_setup_1(monkeypatch, tmp_path)
    derived = tools._sandbox_name("chat.one")
    assert derived.startswith(tools._DERIVED_PREFIX)
    assert tools._sandbox_name(derived) != derived, "a literal id took a derived name"

    first = Path(tools.get_sandbox_workdir("chat.one"))
    (first / "mine.csv").write_text("mine", encoding = "utf-8")
    second = Path(tools.get_sandbox_workdir(derived))
    assert second != first
    assert not (second / "mine.csv").exists()


def test_a_link_inside_the_root_is_stepped_around(tmp_path, monkeypatch):
    """A contained link target passes containment, so claiming through it marks a foreign dir."""
    root = tmp_path / "shared"
    root.mkdir()
    foreign = root / "foreign"
    foreign.mkdir()
    (foreign / "theirs.txt").write_text("theirs", encoding = "utf-8")
    (root / "chat").symlink_to(foreign, target_is_directory = True)
    monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(tmp_path / "home"))
    tools = _shared_setup_3(monkeypatch, root)
    workdir = Path(tools.get_sandbox_workdir("chat"))
    assert workdir.resolve() != foreign.resolve(), workdir
    assert not (foreign / tools._SANDBOX_MARKER).exists(), "claimed through a link"
    assert (foreign / "theirs.txt").is_file()


def test_an_unowned_cache_of_trainers_is_not_put_on_sys_path(tmp_path, monkeypatch):
    """Unsloth*Trainer.py is a name a user's own subclass carries, and anything
    else in that directory would then shadow real modules for every worker."""
    import sys as _sys

    theirs = tmp_path / "unsloth_compiled_cache"
    theirs.mkdir()
    (theirs / "UnslothCustomTrainer.py").write_text("class X: pass\n", encoding = "utf-8")
    (theirs / "numpy.py").write_text("raise SystemExit('shadowed')\n", encoding = "utf-8")
    monkeypatch.delenv("UNSLOTH_COMPILE_LOCATION", raising = False)
    monkeypatch.chdir(tmp_path)
    monkeypatch.setenv("PYTHONPATH", "")

    from utils import cache_cleanup

    before = list(_sys.path)
    cache_cleanup.register_compiled_cache_on_path()
    assert str(theirs.resolve()) not in _sys.path, "an unowned directory went on sys.path"
    assert str(theirs.resolve()) not in os.environ.get("PYTHONPATH", "")
    _sys.path[:] = before

    (theirs / "unsloth_compiled_module_gemma3.py").write_text("x = 1\n", encoding = "utf-8")
    cache_cleanup.register_compiled_cache_on_path()
    assert str(theirs.resolve()) in _sys.path
    _sys.path[:] = before


def test_the_delete_switch_reaches_a_chat_moved_into_a_project():
    """Files from before a project move sit in the chat's own sandbox, not the project."""
    body = _sidebar_function_body("deleteTargetHasFiles")
    assert "projectId" not in body, (
        "a chat moved into a project still owns the sandbox it wrote before the move, "
        "so the switch must not be gated on project membership"
    )
    # Pin the direction: negative checks alone passed the exact inversion `kind === "run"`.
    assert re.search(r'kind\s*!==\s*"run"', body) or all(
        f'"{kind}"' in body for kind in ("chat", "chats", "project", "projects")
    ), (
        "the body must either exclude run by negation or name every kind that keeps its sandbox; "
        "as written it does neither, so it cannot say which targets reach the switch"
    )
    assert not re.search(r'return\s+target\.kind\s*===\s*"run"\s*;', body), (
        "inverted: this returns the delete switch for training runs only, and hides it for "
        "every chat and project"
    )
    for kind in ('"chat"', '"project"'):
        assert (
            f"kind !== {kind}" not in body and f"kind === {kind} ? false" not in body
        ), f"{kind} targets must keep reaching the delete switch"


def test_a_persisted_files_value_that_is_not_a_list_is_not_a_wrapper():
    """The cards map over it, so anything else takes the chat view down."""
    root = Path(__file__).resolve().parents[2] / "frontend" / "src"
    contract = (root / "components" / "assistant-ui" / "sandbox-files.ts").read_text(
        encoding = "utf-8"
    )
    python_card = (root / "components" / "assistant-ui" / "tool-ui-python.tsx").read_text(
        encoding = "utf-8"
    )
    assert "export function isSandboxFileList" in contract
    assert 'typeof (entry as { name?: unknown }).name === "string"' in contract
    for source in (contract, python_card):
        assert "isSandboxFileList(v.files)" in source


def test_a_sandbox_of_empty_directories_is_still_reclaimed(tmp_path, monkeypatch):
    """A sandbox of only empty directories must still be reclaimed once its record is gone."""
    monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(tmp_path / "home"))
    tools = _shared_setup_1(monkeypatch, tmp_path)
    session = "__LOCALID_dirs111"
    workdir = Path(tools.get_sandbox_workdir(session))
    (workdir / "outputs" / "runs").mkdir(parents = True)

    assert tools.remove_session_sandbox(session) is True
    assert not workdir.exists()

    other = Path(tools.get_sandbox_workdir("__LOCALID_dirs222"))
    (other / "outputs").mkdir()
    (other / "outputs" / "report.csv").write_text("a,b\n", encoding = "utf-8")
    assert tools.remove_session_sandbox("__LOCALID_dirs222") is False
    assert (other / "outputs" / "report.csv").is_file()


def test_a_symlinked_builtin_cache_is_left_usable(tmp_path, monkeypatch):
    """Clearing must keep the link itself, or the next compile cannot write through it."""
    real = tmp_path / "real_cache"
    real.mkdir()

    from utils import cache_cleanup

    (real / cache_cleanup.CACHE_MARKER).touch()
    (real / "unsloth_compiled_module_gemma3.py").write_text("x = 1\n", encoding = "utf-8")
    link = tmp_path / "unsloth_compiled_cache"
    link.symlink_to(real, target_is_directory = True)
    monkeypatch.setattr(cache_cleanup, "_CACHE_DIRS", [link])
    monkeypatch.delenv("UNSLOTH_COMPILE_LOCATION", raising = False)

    cache_cleanup.clear_unsloth_compiled_cache()
    assert real.is_dir(), "the link was left dangling"
    assert (real / cache_cleanup.CACHE_MARKER).is_file()
    assert not (real / "unsloth_compiled_module_gemma3.py").exists()
    os.makedirs(link, exist_ok = True)


def test_the_sandbox_listing_runs_in_a_worker():
    """It walks up to a couple of thousand entries and stats each one."""
    import inspect

    from routes import inference as inference_routes

    source = inspect.getsource(inference_routes.list_sandbox_files)
    assert "_sandbox_listing(sandbox_dir)" in source
    assert "run_in_threadpool(_resolve_and_list)" in source
    assert "os.stat" not in source


def test_a_marker_replaced_by_a_link_is_not_written_through(tmp_path, monkeypatch):
    """The marker sits where tool code runs, so a link there would redirect our write."""
    monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(tmp_path / "home"))
    tools = _shared_setup_1(monkeypatch, tmp_path)
    session = "__LOCALID_link911"
    workdir = Path(tools.get_sandbox_workdir(session))
    victim = tmp_path / "notes.txt"
    victim.write_text("years of notes", encoding = "utf-8")

    marker = workdir / tools._SANDBOX_MARKER
    marker.unlink()
    marker.symlink_to(victim)
    assert tools._marker_owner(str(workdir)) is None, "followed a link to read"

    tools._mark_sandbox(str(workdir), session)
    assert victim.read_text(encoding = "utf-8") == "years of notes", "wrote through the link"
    assert not marker.is_symlink()
    assert marker.read_text(encoding = "utf-8") == session


def test_a_cached_path_swapped_for_another_chats_directory_is_dropped(tmp_path, monkeypatch):
    """A tool needs only cd .., mv and ln -s, and containment accepts a link to a sibling."""
    monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(tmp_path / "home"))
    tools = _shared_setup_1(monkeypatch, tmp_path)
    victim = Path(tools.get_sandbox_workdir("__LOCALID_victim2"))
    (victim / "private.csv").write_text("theirs", encoding = "utf-8")
    attacker = Path(tools.get_sandbox_workdir("__LOCALID_attack2"))

    shutil.rmtree(attacker)
    attacker.symlink_to(victim, target_is_directory = True)

    again = Path(tools.get_sandbox_workdir("__LOCALID_attack2"))
    assert again.resolve() != victim.resolve(), again
    assert not (again / "private.csv").exists()
    resolved = Path(tools.resolve_sandbox_workdir("__LOCALID_attack2"))
    assert not (resolved / "private.csv").exists(), resolved


def test_a_default_folder_that_was_already_there_is_not_run_in(tmp_path, monkeypatch):
    """A call without a session id lands in _default, which may be the user's own folder."""
    root = tmp_path / "shared"
    root.mkdir()
    theirs = root / "_default"
    theirs.mkdir()
    (theirs / "notes.txt").write_text("theirs", encoding = "utf-8")
    monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(tmp_path / "home"))
    tools = _shared_setup_3(monkeypatch, root)
    workdir = Path(tools.get_sandbox_workdir(None))
    assert workdir != theirs, workdir
    assert not (workdir / "notes.txt").exists()
    assert (theirs / "notes.txt").read_text(encoding = "utf-8") == "theirs"
    assert not (theirs / tools._SANDBOX_MARKER).exists()

    tools._workdirs.clear()
    assert Path(tools.get_sandbox_workdir(None)) == workdir


def test_an_id_a_path_segment_cannot_carry_rides_in_the_query():
    """ASGI decodes %2F before routing, so an id with a slash must travel in the query."""
    root = Path(__file__).resolve().parents[2] / "frontend" / "src"
    helper = (root / "components" / "assistant-ui" / "sandbox-files.ts").read_text(encoding = "utf-8")
    assert "export function sandboxRoutePrefix" in helper
    assert "?session=${encodeURIComponent(sessionId)}" in helper

    from routes import inference as inference_routes
    import inspect

    for route in (inference_routes.list_sandbox_files, inference_routes.serve_sandbox_file):
        source = inspect.getsource(route)
        assert "session: Optional[str] = None" in source
        assert "session or session_id" in source


def test_clearing_every_chat_reports_what_it_deleted(tmp_path, monkeypatch):
    """A thread added after the listing must still have its sandbox cleaned up with the rest."""
    chat_history, inspect, studio_db = _shared_setup_17()

    # The body lives in the _with_replay_status variant.
    source = inspect.getsource(studio_db.clear_chat_history_with_replay_status)
    assert "SELECT id FROM chat_threads" in source
    assert source.index("SELECT id FROM chat_threads") < source.index("DELETE FROM chat_threads")
    assert "return removed, active_runs, False" in source

    route = inspect.getsource(chat_history.clear_history)
    assert "cleared, cleared_runs, cleared_chat_runs = clear_chat_history(" in route
    assert "cleared" in route.split("_remove_sandboxes(", 1)[1].split(")", 1)[0]


def test_a_symlinked_cache_marker_does_not_license_a_delete(tmp_path, monkeypatch):
    """exists() follows links, so a marker pointing at a user's path could license rmtree."""
    from utils import cache_cleanup

    theirs = tmp_path / "unsloth_compiled_cache"
    theirs.mkdir()
    (theirs / "notes.txt").write_text("years of notes", encoding = "utf-8")
    (theirs / cache_cleanup.CACHE_MARKER).symlink_to(tmp_path / "anything")
    (tmp_path / "anything").write_text("x", encoding = "utf-8")

    assert cache_cleanup._is_dedicated_cache(theirs) is False

    monkeypatch.setenv("UNSLOTH_COMPILE_LOCATION", str(theirs))
    monkeypatch.chdir(tmp_path)
    cache_cleanup.clear_unsloth_compiled_cache()
    assert (theirs / "notes.txt").is_file(), "deleted a directory it does not own"


def test_a_real_cache_marker_still_counts(tmp_path):
    """The other half: a directory Unsloth made is still cleaned out."""
    from utils import cache_cleanup

    ours = tmp_path / "unsloth_compiled_cache"
    ours.mkdir()
    (ours / cache_cleanup.CACHE_MARKER).touch()
    assert cache_cleanup._is_dedicated_cache(ours) is True


def test_an_id_with_a_lone_surrogate_still_gets_a_directory(tmp_path, monkeypatch):
    """Lone surrogates come from JSON or surrogateescape names; a strict encode would raise."""
    monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(tmp_path / "home"))
    tools = _shared_setup_1(monkeypatch, tmp_path)
    odd = "chat-\ud800-one"
    name = tools._sandbox_name(odd)
    assert name.startswith(tools._DERIVED_PREFIX)
    # surrogatepass keeps bad ids distinct; a replacing policy would fold them onto one directory.
    assert name != tools._sandbox_name("chat-\ud801-one")

    workdir = Path(tools.get_sandbox_workdir(odd))
    assert workdir.is_dir()
    assert Path(tools.resolve_sandbox_workdir(odd)) == workdir


def test_a_legacy_entry_that_is_a_symlink_is_left_alone(tmp_path, monkeypatch):
    """move() keeps the symlink, so the marker would land in its target, outside both roots."""
    fake_home = tmp_path / "userprofile"
    legacy = fake_home / "studio_sandbox"
    legacy.mkdir(parents = True)
    outside = tmp_path / "outside"
    outside.mkdir()
    (outside / "theirs.txt").write_text("mine", encoding = "utf-8")
    (legacy / "__LOCALID_link111").symlink_to(outside, target_is_directory = True)
    (legacy / "__LOCALID_real111").mkdir()
    (legacy / "__LOCALID_real111" / "results.csv").write_text("a,b\n", encoding = "utf-8")

    _shared_setup_11(fake_home, monkeypatch, tmp_path)

    tools = _shared_setup_6()
    root = Path(tools.sandbox_root())
    tools._migrate_legacy_sandbox(str(root))

    assert not (outside / tools._SANDBOX_MARKER).exists(), "wrote outside both roots"
    assert (outside / "theirs.txt").is_file()
    assert not (root / "__LOCALID_link111").exists()
    assert (root / "__LOCALID_real111" / "results.csv").is_file()


def test_a_first_tool_call_does_not_wait_for_the_whole_legacy_tree(tmp_path, monkeypatch):
    """Migration across filesystems takes minutes; a call for its own folder must not queue."""
    fake_home = tmp_path / "userprofile"
    legacy = fake_home / "studio_sandbox"
    (legacy / "__LOCALID_mine111").mkdir(parents = True)
    (legacy / "__LOCALID_mine111" / "results.csv").write_text("a,b\n", encoding = "utf-8")
    (legacy / "__LOCALID_huge111").mkdir()

    _shared_setup_11(fake_home, monkeypatch, tmp_path)

    tools = _shared_setup_6()
    held = threading.Event()
    monkeypatch.setattr(
        tools,
        "migrate_legacy_sandbox_in_background",
        lambda: threading.Thread(target = held.wait),
    )

    class _Blocked:
        def __enter__(self):
            assert held.wait(0), "the request path took the whole-tree lock"

        def __exit__(self, *exc):
            return False

    monkeypatch.setattr(tools, "_legacy_sandbox_lock", _Blocked())
    workdir = Path(tools.get_sandbox_workdir("__LOCALID_mine111"))
    held.set()

    assert (workdir / "results.csv").is_file(), "this chat's own files were left behind"


def test_deleting_a_chat_stops_a_generation_that_would_recreate_it(monkeypatch):
    """A queued request would run its tool after the delete and write files no chat can reach."""
    import routes.chat_history as chat_history

    cancelled = []
    from state import active_generations

    monkeypatch.setattr(active_generations, "cancel_thread", lambda tid: cancelled.append(tid) or 1)

    chat_history._cancel_active_generations(["__LOCALID_gone111", "__LOCALID_gone222"])
    assert cancelled == ["__LOCALID_gone111", "__LOCALID_gone222"]

    import inspect

    for route in (chat_history.delete_threads, chat_history.clear_history):
        source = inspect.getsource(route)
        assert "_cancel_active_generations" in source, route.__name__
        assert source.index("_cancel_active_generations") < source.index("_remove_sandboxes")


def test_a_large_file_is_streamed_rather_than_buffered():
    """Streaming avoids a Blob and an IPC copy of a multi-gigabyte artifact in the renderer."""
    view = (
        Path(__file__).resolve().parents[2]
        / "frontend/src/components/assistant-ui/sandbox-files-view.tsx"
    ).read_text(encoding = "utf-8")

    assert "downloadUrlStreaming" in view
    assert "response.blob()" not in view
    # The streaming path sends no headers, so the bearer travels as a query parameter.
    assert "token=" in view


def test_a_same_size_overwrite_is_still_reported(tmp_path, monkeypatch):
    """Coarse-timestamp volumes let a same-length rewrite in one tick match on mtime and size."""
    tools = _shared_setup_1(monkeypatch, tmp_path)
    workdir = Path(tools.get_sandbox_workdir("__LOCALID_same111"))
    monkeypatch.setattr(tools, "_volume_timestamps_finely", lambda _: False)
    report = workdir / "report.csv"
    report.write_text("a,b\n1,2\n", encoding = "utf-8")

    before = tools._snapshot_workdir_files(str(workdir))
    report.write_text("a,b\n3,4\n", encoding = "utf-8")
    os.utime(report, ns = (before["report.csv"][0], before["report.csv"][0]))

    after = tools._snapshot_workdir_files(str(workdir))
    assert after["report.csv"][:2] == before["report.csv"][:2], "the test lost its premise"
    assert after["report.csv"] != before["report.csv"], "a same-size rewrite looked unchanged"

    sentinel = tools._created_file_sentinels(str(workdir), before)
    assert "report.csv" in (sentinel or ""), sentinel


def test_a_file_too_big_to_read_is_still_snapshotted(tmp_path, monkeypatch):
    """Digests are capped; hashing every artifact twice per call costs too much for this case."""
    tools = _shared_setup_1(monkeypatch, tmp_path)
    workdir = Path(tools.get_sandbox_workdir("__LOCALID_big1111"))
    big = workdir / "model.bin"
    big.write_bytes(b"0" * (tools._MAX_HASHED_SNAPSHOT_BYTES + 1))

    entry = tools._snapshot_workdir_files(str(workdir))["model.bin"]
    assert entry[1] == tools._MAX_HASHED_SNAPSHOT_BYTES + 1
    assert entry[2] is None, "hashed a file past the cap"


def test_deleting_a_big_sandbox_does_not_hold_up_other_chats(tmp_path, monkeypatch):
    """Every tool start takes this lock, so an rmtree under it would stall unrelated chats."""
    tools = _shared_setup_1(monkeypatch, tmp_path)
    session = "__LOCALID_big2222"
    workdir = Path(tools.get_sandbox_workdir(session))
    (workdir / "data.csv").write_text("a,b\n", encoding = "utf-8")

    started = threading.Event()
    release = threading.Event()
    real_rmtree = tools.shutil.rmtree

    detached_prefix = os.path.basename(str(workdir)) + tools._DETACHED_SUFFIX

    def slow_rmtree(path, **kwargs):
        # rmtree is patched process-wide, so only this chat's detached tree is the delete under
        # test; other deletes (legacy migration, background cleanup) must not set started before the
        # rename, which would fail the name check below.
        if not os.path.basename(os.fspath(path)).startswith(detached_prefix):
            return real_rmtree(path, **kwargs)
        started.set()
        release.wait(10)
        return real_rmtree(path, **kwargs)

    monkeypatch.setattr(tools.shutil, "rmtree", slow_rmtree)
    outcome = []
    deleter = threading.Thread(
        target = lambda: outcome.append(tools.remove_session_sandbox(session, delete_files = True)),
    )
    try:
        deleter.start()
        assert started.wait(5), "the delete never started"
        # This is the lock every tool start takes; holding it here would stall every other chat.
        assert tools._active_sessions_lock.acquire(timeout = 2), "held the tool lock"
        tools._active_sessions_lock.release()
        assert not workdir.exists(), "the name is still there"
    finally:
        release.set()
        deleter.join(15)
        monkeypatch.setattr(tools.shutil, "rmtree", real_rmtree)
    assert outcome == [True], outcome
    for _ in range(100):
        leftovers = [n for n in os.listdir(tools.sandbox_root()) if tools._DETACHED_SUFFIX in n]
        if not leftovers:
            break
        time.sleep(0.05)
    assert leftovers == [], leftovers


def test_an_interrupted_move_is_not_read_as_a_collision(tmp_path, monkeypatch):
    """A killed cross-filesystem copy leaves a partial directory while the source remains."""
    fake_home = tmp_path / "userprofile"
    legacy = fake_home / "studio_sandbox" / "__LOCALID_part111"
    legacy.mkdir(parents = True)
    (legacy / "results.csv").write_text("a,b\n", encoding = "utf-8")
    (legacy / "second.csv").write_text("c,d\n", encoding = "utf-8")

    _shared_setup_11(fake_home, monkeypatch, tmp_path)

    tools = _shared_setup_6()
    root = Path(tools.sandbox_root())
    root.mkdir(parents = True, exist_ok = True)

    real_move = tools.shutil.move

    def half_copied(source, target):
        os.makedirs(target, exist_ok = True)
        shutil.copy2(os.path.join(source, "results.csv"), os.path.join(target, "results.csv"))
        raise OSError(5, "interrupted")

    monkeypatch.setattr(tools.shutil, "move", half_copied)
    tools._migrate_legacy_sandbox(str(root))
    monkeypatch.setattr(tools.shutil, "move", real_move)

    assert not (root / "__LOCALID_part111").exists(), "left a partial copy at the real name"
    assert [n for n in os.listdir(root) if tools._STAGING_SUFFIX in n] == []
    assert (legacy / "second.csv").is_file(), "lost the source"

    tools._legacy_sandbox_migrated = False
    tools._migrate_legacy_sandbox(str(root))
    assert (root / "__LOCALID_part111" / "second.csv").is_file(), "the retry never happened"


@pytest.mark.parametrize("dir_fd_writes", [True, False])
def test_attachments_are_copied_into_the_sandbox_once(tmp_path, monkeypatch, dir_fd_writes):
    from core import chat_originals

    tools = _shared_setup_1(monkeypatch, tmp_path)
    monkeypatch.setattr(tools, "_DIR_FD_WRITES", tools._DIR_FD_WRITES and dir_fd_writes)
    monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(tmp_path / "home"))
    sheet, _ = chat_originals.save([b"a,b\n"])
    deck, _ = chat_originals.save([b"slides"], chat_originals.max_bytes("deck.odp"))
    session = "__LOCALID_attach1"
    tools.materialize_sandbox_attachments(
        session, [(sheet, "data.csv"), (deck, "../deck.odp"), ("0" * 64, "gone.csv")]
    )
    long_name = "季度" * 45 + ".xlsx"
    tools.materialize_sandbox_attachments(session, [(sheet, long_name)])
    workdir = Path(tools.get_sandbox_workdir(session))
    copy = workdir / tools.sandbox_attachment_path(sheet, "data.csv")
    assert copy.read_bytes() == b"a,b\n"
    assert tools.sandbox_attachment_path(deck, "../deck.odp").endswith(f"{deck[:12]}/_deck.odp")
    assert (workdir / tools.sandbox_attachment_path(deck, "../deck.odp")).read_bytes() == b"slides"
    long_copy = workdir / tools.sandbox_attachment_path(sheet, long_name)
    assert long_copy.read_bytes() == b"a,b\n" and long_copy.suffix == ".xlsx"
    assert 70 < len(long_copy.name.encode()) <= 80
    assert tools.session_sandbox_has_files(session) is False
    copy.write_bytes(b"edited")
    tools.materialize_sandbox_attachments(session, [(sheet, "data.csv")])
    assert copy.read_bytes() == b"edited"
    assert tools.session_sandbox_has_files(session) is True
    outside = tmp_path / "outside"
    outside.mkdir()
    other = "__LOCALID_attach2"
    linked = Path(tools.get_sandbox_workdir(other)) / tools._ATTACHMENTS_DIR
    linked.symlink_to(outside, target_is_directory = True)
    tools.materialize_sandbox_attachments(other, [(sheet, "data.csv")])
    assert list(outside.iterdir()) == []


def test_a_python_call_that_edits_an_attachment_reports_it(tmp_path, monkeypatch):
    from core import chat_originals

    tools = _shared_setup_1(monkeypatch, tmp_path)
    monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(tmp_path / "home"))
    sheet, _ = chat_originals.save([b"a,b\n1,2\n"])
    session = "__LOCALID_attachedit"
    tools.materialize_sandbox_attachments(session, [(sheet, "report.csv")])
    path = tools.sandbox_attachment_path(sheet, "report.csv")
    beside = path.replace("report.csv", "report_filled.csv")

    read_only = tools._python_exec(f"print(open({path!r}).read())", session_id = session)
    assert "__FILES__" not in read_only

    result = tools._python_exec(
        f"open({path!r}, 'a').write('3,4\\n'); open({beside!r}, 'w').write('x\\n')",
        session_id = session,
    )
    files = json.loads(result.split("__FILES__:")[1].split("\n")[0])
    assert {entry["name"] for entry in files} == {path, beside}


def test_an_attachment_copied_in_during_a_call_is_not_claimed_by_it(tmp_path, monkeypatch):
    from core import chat_originals

    tools = _shared_setup_1(monkeypatch, tmp_path)
    monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(tmp_path / "home"))
    session = "__LOCALID_sharedattach"
    kept, _ = chat_originals.save([b"a,b\n1,2\n"])
    tools.materialize_sandbox_attachments(session, [(kept, "kept.csv")])
    workdir = tools._get_workdir(session)

    def call(during):
        token = tools._call_started(workdir)
        before = tools._snapshot_workdir_files(workdir)
        during()
        try:
            return tools._created_file_sentinels(workdir, before, None, token)
        finally:
            tools._call_finished(token)

    def recopy_and_write():
        tools.materialize_sandbox_attachments(session, [(kept, "kept.csv")])
        with open(os.path.join(workdir, "out.txt"), "w") as handle:
            handle.write("x")

    result = call(recopy_and_write)
    files = json.loads(result.split("__FILES__:")[1].split("\n")[0])
    assert [entry["name"] for entry in files] == ["out.txt"]

    other, _ = chat_originals.save([b"c,d\n"])
    assert (
        call(lambda: tools.materialize_sandbox_attachments(session, [(other, "other.csv")])) == ""
    )
    assert os.path.isfile(os.path.join(workdir, tools.sandbox_attachment_path(other, "other.csv")))


def test_sandbox_attachment_paths_match_the_frontend():
    """same table as sandbox-attachments.test.ts because the client notes these paths to the model."""
    from core.inference.tools import sandbox_attachment_path

    sha = "ab" * 32
    for name, base in (
        ("data.csv", "data.csv"),
        ("../deck?.pptx", "_deck_.pptx"),
        (" .hidden. ", "hidden"),
        ("季度" * 45 + ".xlsx", "季度" * 12 + "季.xlsx"),
        ("a" * 79 + " ." + "x" * 20, "a" * 79),
        ("b" * 10 + "." + "x" * 100, "b" * 10),
        ("..", "attachment"),
        ("e" * 100, "e" * 80),
        ("c" * 78 + ". ." + "z" * 20, "c" * 78),
        ("CON.csv", "_CON.csv"),
        ("nul.tar.gz", "_nul.tar.gz"),
        ("com1", "_com1"),
        ("CONSOLE.txt", "CONSOLE.txt"),
    ):
        path = sandbox_attachment_path(sha, name)
        assert path == f".unsloth_attachments/abababababab/{base}", name
        assert sandbox_attachment_path(sha, base) == path, name


def test_attachments_are_copied_only_for_the_python_tool(monkeypatch):
    import asyncio
    import inspect
    from types import SimpleNamespace

    from core.inference import tools
    from routes import inference

    calls = []
    monkeypatch.setattr(tools, "materialize_sandbox_attachments", lambda *args: calls.append(args))
    item = SimpleNamespace(sha256 = "a" * 64, name = "data.csv")
    for enable_tools, enabled_tools, attachments in (
        (True, None, [item]),
        (True, ["python"], [item]),
        (True, ["web_search"], [item]),
        (False, None, [item]),
        (True, None, None),
    ):
        payload = SimpleNamespace(
            enable_tools = enable_tools,
            enabled_tools = enabled_tools,
            sandbox_attachments = attachments,
            session_id = "s",
        )
        asyncio.run(inference._materialize_sandbox_attachments(payload))
    assert calls == [("s", [("a" * 64, "data.csv")])] * 2
    source = inspect.getsource(inference.produce_openai_chat_completions)
    assert source.count("await _materialize_sandbox_attachments(payload)") == 1
    assert source.index("_materialize_sandbox_attachments") < source.index(
        "_proxy_to_external_provider"
    )


def test_a_delete_without_the_switch_says_what_it_kept(tmp_path, monkeypatch):
    """Other surfaces never offer the choice, so the route must report the folder it kept."""
    tools = _shared_setup_1(monkeypatch, tmp_path)
    kept = "__LOCALID_keeps11"
    empty = "__LOCALID_empty11"
    workdir = Path(tools.get_sandbox_workdir(kept))
    (workdir / "results.csv").write_text("a,b\n", encoding = "utf-8")
    Path(tools.get_sandbox_workdir(empty))

    assert tools.session_sandbox_has_files(kept) is True
    assert tools.session_sandbox_has_files(empty) is False
    assert tools.session_sandbox_has_files("__LOCALID_never11") is False

    import asyncio

    import routes.chat_history as chat_history

    removed, still_there = (
        asyncio.get_event_loop_policy()
        .new_event_loop()
        .run_until_complete(chat_history._remove_sandboxes([kept, empty], False))
    )
    assert removed == 1, removed
    assert still_there == [kept], still_there
    assert (workdir / "results.csv").is_file()


def test_every_delete_surface_can_still_reach_the_files():
    """The offer is made where every delete surface goes through, not only the switch dialog."""
    src = Path(__file__).resolve().parents[2] / "frontend/src"
    hook = (src / "features/chat/hooks/use-chat-sidebar-items.ts").read_text(
        encoding = "utf-8",
    )
    offer = (src / "features/chat/utils/offer-kept-sandbox-files.ts").read_text(
        encoding = "utf-8",
    )

    body = hook[hook.index("export async function deleteChatItem") :]
    assert "offerToDeleteKeptSandboxes(kept)" in body
    assert "toast(" in offer, "nothing tells the user the files are still there"
    assert "deleteFiles: true" in offer


def test_a_fallback_name_already_in_a_shared_root_is_not_taken(tmp_path, monkeypatch):
    """Both names may be the user's, and a delete with the switch would remove their files."""
    root = tmp_path / "shared"
    root.mkdir()
    monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(tmp_path / "home"))
    tools = _shared_setup_3(monkeypatch, root)
    session = "__LOCALID_both111"
    name = tools._sandbox_name(session)
    theirs = root / name
    theirs.mkdir()
    (theirs / "plain.txt").write_text("mine", encoding = "utf-8")
    fallback = root / f"{name}-{tools._name_suffix(session)}"
    fallback.mkdir()
    (fallback / "also-mine.txt").write_text("mine", encoding = "utf-8")

    workdir = Path(tools.get_sandbox_workdir(session))
    assert workdir not in (theirs, fallback), workdir
    assert not (theirs / tools._SANDBOX_MARKER).exists()
    assert not (fallback / tools._SANDBOX_MARKER).exists()
    assert tools._marker_owner(str(workdir)) == name

    tools.remove_session_sandbox(session, delete_files = True)
    assert (theirs / "plain.txt").is_file()
    assert (fallback / "also-mine.txt").is_file()


def test_a_symlinked_sandbox_marker_does_not_make_a_directory_ours(tmp_path, monkeypatch):
    """isfile() follows links, so a symlinked marker would make another directory look ours."""
    root = tmp_path / "shared"
    root.mkdir()
    monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(tmp_path / "home"))
    tools = _shared_setup_3(monkeypatch, root)
    session = "__LOCALID_fake111"
    theirs = root / tools._sandbox_name(session)
    theirs.mkdir()
    (theirs / "notes.txt").write_text("years of notes", encoding = "utf-8")
    (root / "anything").write_text("x", encoding = "utf-8")
    (theirs / tools._SANDBOX_MARKER).symlink_to(root / "anything")

    assert tools._sandbox_is_ours(str(theirs)) is False
    assert tools.remove_session_sandbox(session, delete_files = True) is False
    assert (theirs / "notes.txt").is_file(), "deleted a directory it does not own"


def test_a_chat_that_owns_nothing_never_reads_from_the_shared_root(tmp_path, monkeypatch):
    """A reserved-looking path inside the user's own root would list and serve their folder."""
    root = tmp_path / "shared"
    root.mkdir()
    monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(tmp_path / "home"))
    tools = _shared_setup_3(monkeypatch, root)
    session = "__LOCALID_none111"
    theirs = root / tools._sandbox_name(session)
    theirs.mkdir()
    (theirs / "private.csv").write_text("theirs", encoding = "utf-8")
    planted = root / "_unowned" / tools._sandbox_name(session)
    planted.mkdir(parents = True)
    (planted / "also-theirs.csv").write_text("theirs", encoding = "utf-8")

    fallback = root / f"{tools._sandbox_name(session)}-{tools._name_suffix(session)}"
    fallback.mkdir()
    (fallback / "third.csv").write_text("theirs", encoding = "utf-8")

    resolved = Path(tools.resolve_sandbox_workdir(session))
    assert not str(resolved).startswith(str(root)), resolved
    assert not (resolved / "also-theirs.csv").exists()
    assert not (resolved / "private.csv").exists()
    assert not (resolved / "third.csv").exists()


def test_a_request_path_move_lands_where_the_resolver_says(tmp_path, monkeypatch):
    """Stopping at a name collision left files at the old root, not the resolver's sandbox."""
    fake_home = tmp_path / "userprofile"
    legacy = fake_home / "studio_sandbox" / "__LOCALID_taken11"
    legacy.mkdir(parents = True)
    (legacy / "results.csv").write_text("a,b\n", encoding = "utf-8")

    root = tmp_path / "shared"
    root.mkdir()
    monkeypatch.setenv("HOME", str(fake_home))
    monkeypatch.setenv("USERPROFILE", str(fake_home))
    monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(tmp_path / "home"))
    tools = _shared_setup_3(monkeypatch, root)
    tools._legacy_sandbox_migrated = False
    theirs = root / "__LOCALID_taken11"
    theirs.mkdir()
    (theirs / "plain.txt").write_text("mine", encoding = "utf-8")

    tools._migrate_one_legacy_session(str(root), "__LOCALID_taken11")

    assert (theirs / "plain.txt").is_file(), "moved into the user's own folder"
    landed = Path(tools.get_sandbox_workdir("__LOCALID_taken11"))
    assert (landed / "results.csv").is_file(), f"the chat's files were stranded: {landed}"


def test_deleting_a_project_takes_its_chats_sandboxes(tmp_path, monkeypatch):
    """Deleting a project drops the only records of chats that wrote files before joining it."""
    import inspect

    from routes import chat_history

    source = inspect.getsource(chat_history.delete_project)
    assert "_remove_sandboxes(member_ids" in source
    assert "_cancel_active_generations(member_ids)" in source
    assert source.index("delete_chat_project(") < source.index(
        "_cancel_active_generations(member_ids)"
    )


def test_a_native_download_gets_an_absolute_url():
    """Native download rejects relative URLs, so a bare /api path failed before the request."""
    view = (
        Path(__file__).resolve().parents[2]
        / "frontend/src/components/assistant-ui/sandbox-files-view.tsx"
    ).read_text(encoding = "utf-8")

    assert "apiUrl(" in view
    body = view[view.index("const save = useCallback") :]
    assert body.index("apiUrl(") < body.index("downloadUrlStreaming(")


def _forget_sandbox_state(tools):
    """Everything a fresh process would not remember."""
    tools._workdirs.clear()
    getattr(tools, "_claimed_here", set()).clear()


def _shared_root(tmp_path, monkeypatch):
    root = tmp_path / "shared"
    root.mkdir()
    monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(tmp_path / "home"))
    monkeypatch.setenv("UNSLOTH_STUDIO_SANDBOX_HOME", str(root))
    return root


def test_a_fallback_with_a_random_name_is_found_again(tmp_path, monkeypatch):
    """Without the marker, a random fallback name is lost, so each launch makes a new folder."""
    root, tools = _shared_setup_5(monkeypatch, tmp_path)
    session = "__LOCALID_rand111"
    name = tools._sandbox_name(session)
    for taken in (root / name, root / f"{name}-{tools._name_suffix(session)}"):
        taken.mkdir()

    first = Path(tools.get_sandbox_workdir(session))
    (first / "report.csv").write_text("a,b\n", encoding = "utf-8")
    assert tools._marker_owner(str(first)) == name

    _forget_sandbox_state(tools)
    assert Path(tools.get_sandbox_workdir(session)) == first
    assert Path(tools.resolve_sandbox_workdir(session)) == first

    assert tools.remove_session_sandbox(session, delete_files = True) is True
    assert not first.exists(), "the delete could not reach the fallback"


def test_a_marker_a_tool_deleted_is_written_again(tmp_path, monkeypatch):
    """A deleted marker is rewritten, not read as someone else's, or the files are stranded."""
    root, tools = _shared_setup_5(monkeypatch, tmp_path)
    session = "__LOCALID_clob111"
    workdir = Path(tools.get_sandbox_workdir(session))
    (workdir / "plot.png").write_bytes(b"x")
    (workdir / tools._SANDBOX_MARKER).unlink()

    again = Path(tools.get_sandbox_workdir(session))
    assert again == workdir, "the chat walked away from its own files"
    assert tools._marker_owner(str(workdir)) == tools._sandbox_name(session)
    assert (again / "plot.png").is_file()

    theirs = root / "not-ours"
    theirs.mkdir()
    tools._workdirs[("__LOCALID_other11", None)] = str(theirs)
    assert Path(tools.get_sandbox_workdir("__LOCALID_other11")) != theirs
    assert not (theirs / tools._SANDBOX_MARKER).exists()


def test_an_interrupted_delete_is_finished_on_the_next_launch(tmp_path, monkeypatch):
    """A kill after the rename but before rmtree leaves a full copy that nothing resolves to."""
    root = _shared_root(tmp_path, monkeypatch)

    from core.inference import tools

    tools._workdirs.clear()
    tombstone = root / f"__LOCALID_gone111{tools._DETACHED_SUFFIX}0123abcd"
    tombstone.mkdir()
    (tombstone / "secret.csv").write_text("rows", encoding = "utf-8")
    (tombstone / tools._SANDBOX_MARKER).write_text(
        "__LOCALID_gone111",
        encoding = "utf-8",
    )
    theirs = root / "report.deleting-old"
    theirs.mkdir()
    (theirs / "keep.txt").write_text("mine", encoding = "utf-8")

    tools.sweep_detached_sandboxes(str(root))

    assert not tombstone.exists(), "an interrupted delete left the files behind"
    assert (theirs / "keep.txt").is_file(), "swept a folder it does not own"


def test_a_legacy_chat_named_like_a_derived_id_is_migrated(tmp_path, monkeypatch):
    """A legacy folder is named by the literal id, so a hash-only lookup misses it."""
    fake_home = tmp_path / "userprofile"
    session = tools_derived_id = "_id-0123456789abcdef"
    legacy = fake_home / "studio_sandbox" / session
    legacy.mkdir(parents = True)
    (legacy / "old.csv").write_text("a,b\n", encoding = "utf-8")

    monkeypatch.setenv("HOME", str(fake_home))
    monkeypatch.setenv("USERPROFILE", str(fake_home))
    root = _shared_root(tmp_path, monkeypatch)

    from core.inference import tools

    assert tools_derived_id.startswith(tools._DERIVED_PREFIX)
    _forget_sandbox_state(tools)
    tools._legacy_sandbox_migrated = False

    landed = Path(tools.get_sandbox_workdir(session))
    assert (landed / "old.csv").is_file(), f"files stranded at the legacy root: {landed}"


def test_clearing_every_chat_reports_the_files_it_kept():
    """Without the switch, folders survive the clear and no card is left to reach them."""
    src = Path(__file__).resolve().parents[2] / "frontend/src"

    api = (src / "features/chat/api/chat-api.ts").read_text(encoding = "utf-8")
    clear = api[api.index("export async function clearBackendChats") :]
    clear = clear[: clear.index("\nexport ")]
    assert "deletedThreadIds: string[]" in clear
    assert "sandboxesKept: string[]" in clear
    assert "sandboxes_kept" in clear

    storage = (src / "features/chat/utils/chat-history-storage.ts").read_text(
        encoding = "utf-8",
    )
    assert "sandboxesKept" in storage
    assert "result.sandboxesKept = backendResult.sandboxesKept" in storage

    tab = (src / "features/settings/tabs/data-tab.tsx").read_text(encoding = "utf-8")
    assert "offerToDeleteKeptSandboxes(result.sandboxesKept)" in tab

    offer = (src / "features/chat/utils/offer-kept-sandbox-files.ts").read_text(
        encoding = "utf-8",
    )
    assert "deleteFiles: true" in offer
    hook = (src / "features/chat/hooks/use-chat-sidebar-items.ts").read_text(
        encoding = "utf-8",
    )
    assert "offerToDeleteKeptSandboxes(kept)" in hook


def test_a_lone_surrogate_id_can_still_step_aside(tmp_path, monkeypatch):
    """Collision re-encodes the id; a strict encode would raise before the chat steps aside."""
    root, tools = _shared_setup_5(monkeypatch, tmp_path)
    session = "chat-\udce2-1"
    theirs = root / tools._sandbox_name(session)
    theirs.mkdir()
    (theirs / "theirs.txt").write_text("mine", encoding = "utf-8")

    workdir = Path(tools.get_sandbox_workdir(session))
    assert workdir != theirs
    assert tools._marker_owner(str(workdir)) == tools._sandbox_name(session)


def test_a_legacy_move_lands_when_both_names_are_taken(tmp_path, monkeypatch):
    """With both derived names taken, the legacy files must still move, or nothing ever will."""
    fake_home = tmp_path / "userprofile"
    session = "__LOCALID_both222"
    legacy = fake_home / "studio_sandbox" / session
    legacy.mkdir(parents = True)
    (legacy / "results.csv").write_text("a,b\n", encoding = "utf-8")

    monkeypatch.setenv("HOME", str(fake_home))
    monkeypatch.setenv("USERPROFILE", str(fake_home))
    root, tools = _shared_setup_5(monkeypatch, tmp_path)
    tools._legacy_sandbox_migrated = False
    taken = [root / session, root / f"{session}-{tools._name_suffix(session)}"]
    for directory in taken:
        directory.mkdir()
        (directory / "theirs.txt").write_text("mine", encoding = "utf-8")

    tools._migrate_one_legacy_session(str(root), session)

    landed = Path(tools.get_sandbox_workdir(session))
    assert (landed / "results.csv").is_file(), f"files stranded at the legacy root: {landed}"
    for directory in taken:
        assert (directory / "theirs.txt").is_file(), "moved into the user's own folder"


def test_a_read_finds_the_marked_fallback_after_a_restart(tmp_path, monkeypatch):
    """Reads find the marked fallback after a restart, or file cards 404 until a tool call."""
    root, tools = _shared_setup_5(monkeypatch, tmp_path)
    session = "__LOCALID_read111"
    name = tools._sandbox_name(session)
    for taken in (root / name, root / f"{name}-{tools._name_suffix(session)}"):
        taken.mkdir()

    workdir = Path(tools.get_sandbox_workdir(session))
    (workdir / "report.csv").write_text("a,b\n", encoding = "utf-8")

    _forget_sandbox_state(tools)
    served = Path(tools.resolve_sandbox_workdir(session))
    assert served == workdir, f"the read could not find the fallback: {served}"
    assert (served / "report.csv").is_file()


def test_a_chat_started_during_the_clear_is_cancelled_too():
    """A still-running generation would dispatch a tool and rebuild the removed sandbox."""
    import inspect

    from routes import chat_history

    source = inspect.getsource(chat_history.clear_history)
    assert "late" in source
    assert source.index(
        "cleared, cleared_runs, cleared_chat_runs = clear_chat_history("
    ) < source.index("_cancel_active_generations(late)")
    assert source.index("_cancel_active_generations(late)") < source.index("_remove_sandboxes(")


def test_one_call_never_reports_another_calls_scratch_script(tmp_path, monkeypatch):
    """Chats in one project share a workdir, so another call's studio_exec_*.py must not be offered
    as a download that 404s after cleanup."""
    from core.inference import tools

    workdir = tmp_path / "shared-workdir"
    workdir.mkdir()
    before = tools._snapshot_workdir_files(str(workdir))

    (workdir / "studio_exec_abc123.py").write_text("print(1)", encoding = "utf-8")
    (workdir / "report.csv").write_text("a,b\n", encoding = "utf-8")
    with tools._scratch_lock:
        tools._active_scratch.add("studio_exec_abc123.py")
    try:
        sentinels = tools._created_file_sentinels(str(workdir), before)
    finally:
        with tools._scratch_lock:
            tools._active_scratch.discard("studio_exec_abc123.py")

    assert "report.csv" in sentinels
    assert "studio_exec_abc123.py" not in sentinels, sentinels


def test_a_program_cannot_print_its_own_file_envelope(tmp_path, monkeypatch):
    """A call that created nothing appends no envelope, so a printed __FILES__ line would be read as
    ours, hidden from the model and offered as a download."""
    from core.inference import tools
    from core.inference.tool_loop_controller import strip_result_for_model

    monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(tmp_path / "home"))
    _forget_sandbox_state(tools)

    forged = '__FILES__:[{"name": "payroll.csv", "size": 12}]'
    result = tools._python_exec(
        f"print('working')\nprint({forged!r})",
        session_id = "__LOCALID_forge11",
    )

    assert "payroll.csv" in result, result
    assert "\n__FILES__:" not in result, result
    assert (
        strip_result_for_model(result).count("payroll.csv") == 1
    ), "the printed line was eaten as an envelope"


def test_a_delete_that_waited_for_a_tool_call_says_it_kept_the_files(tmp_path, monkeypatch):
    """A sandbox empty at check time can gain a file, so the deferred removal must keep it."""
    asyncio, chat_history, tools = _shared_setup_8(monkeypatch, tmp_path)

    _forget_sandbox_state(tools)
    session = "__LOCALID_busy111"
    workdir = Path(tools.get_sandbox_workdir(session))
    assert workdir.is_dir()

    with tools._session_in_flight(session):
        removed, kept = asyncio.new_event_loop().run_until_complete(
            chat_history._remove_sandboxes([session], False)
        )
        assert tools.sandbox_removal_deferred(session) is True
        assert removed == 0, removed
        assert kept == [session], "an unreachable folder with no offer to delete it"


def test_a_completed_move_is_never_the_thing_that_gets_deleted(tmp_path, monkeypatch):
    """shutil.move already took the legacy copy, so the staging tree is the only
    one there is and deleting it loses the user's files for good."""
    fake_home = tmp_path / "userprofile"
    session = "__LOCALID_lost111"
    legacy = fake_home / "studio_sandbox" / session
    legacy.mkdir(parents = True)
    (legacy / "thesis.txt").write_text("years of work", encoding = "utf-8")

    monkeypatch.setenv("HOME", str(fake_home))
    monkeypatch.setenv("USERPROFILE", str(fake_home))
    root = tmp_path / "home" / "studio_sandbox"
    tools = _shared_setup_2(monkeypatch, tmp_path)
    root.mkdir(parents = True)

    real_rename = os.rename

    def failing_rename(src, dst):
        if str(src).find(tools._STAGING_SUFFIX) != -1:
            raise OSError("rename refused")
        return real_rename(src, dst)

    monkeypatch.setattr(os, "rename", failing_rename)
    with pytest.raises(OSError):
        tools._staged_move(str(legacy), str(root / session), session)
    monkeypatch.setattr(os, "rename", real_rename)

    survivors = [p for p in [legacy, *root.iterdir()] if (p / "thesis.txt").is_file()]
    assert survivors, "the only copy of the user's files was deleted"


def test_a_symlinked_directory_counts_as_a_file_of_the_users(tmp_path, monkeypatch):
    """os.walk lists a symlink in dirs, so a files-only emptiness check would remove the link."""
    tools = _shared_setup_2(monkeypatch, tmp_path)
    session = "__LOCALID_link111"
    workdir = Path(tools.get_sandbox_workdir(session))
    elsewhere = tmp_path / "data"
    elsewhere.mkdir()
    (workdir / "dataset").symlink_to(elsewhere, target_is_directory = True)

    assert tools._holds_no_user_files(str(workdir)) is False
    assert tools.remove_session_sandbox(session, delete_files = False) is False
    assert (workdir / "dataset").is_symlink(), "removed something the tool made"
    assert tools.session_sandbox_has_files(session) is True


def test_a_project_delete_uses_the_membership_it_really_deleted():
    """Membership comes from the transaction: a chat moved in after the listing is deleted too."""
    chat_history, inspect, studio_db = _shared_setup_17()

    storage = inspect.getsource(studio_db.delete_chat_project)
    assert 'project["memberIds"] = sorted(thread_ids)' in storage

    route = inspect.getsource(chat_history.delete_project)
    assert 'member_ids = list(project.get("memberIds") or [])' in route
    assert "list_chat_threads(project_id" not in route
    assert route.index("_cancel_active_generations(member_ids)") < route.index("_remove_sandboxes(")
    assert "sandboxes_kept = await _remove_sandboxes(member_ids" in route
    assert "ChatProjectDeleted(**project, sandboxes_kept = sandboxes_kept)" in route


def test_closing_an_incognito_chat_cleans_up_its_sandbox():
    """Its id is the tool call's sandbox session, so a folder can exist with no history row."""
    src = Path(__file__).resolve().parents[2] / "frontend/src"

    storage = (src / "features/chat/utils/chat-history-storage.ts").read_text(
        encoding = "utf-8",
    )
    body = storage[storage.index("export async function deleteStoredChatThreads") :]
    body = body[: body.index("\nexport ")]
    assert "deleteChatThreads(idsToDelete" in body, "incognito ids never reach the backend"
    assert "isThreadIncognito" in body

    projects = (src / "features/chat/hooks/use-chat-projects.ts").read_text(
        encoding = "utf-8",
    )
    assert "offerToDeleteKeptSandboxes(kept)" in projects


def test_a_snapshot_stops_hashing_once_it_has_read_enough(tmp_path, monkeypatch):
    """Hashing stops at a byte budget too, since a file-count cap alone let thousands be read."""
    from core.inference import tools

    workdir = tmp_path / "artifacts"
    workdir.mkdir()
    chunk = b"x" * (1024 * 1024)
    for i in range(8):
        (workdir / f"run{i}.bin").write_bytes(chunk)

    monkeypatch.setattr(tools, "_MAX_SNAPSHOT_HASH_BYTES", 3 * 1024 * 1024)
    read = []
    real_content_key = tools._content_key

    def counting_content_key(path, size):
        read.append(size)
        return real_content_key(path, size)

    monkeypatch.setattr(tools, "_content_key", counting_content_key)
    snapshot = tools._snapshot_workdir_files(str(workdir))

    assert len(snapshot) == 8, snapshot
    assert sum(read) <= 3 * 1024 * 1024 + 1024 * 1024, sum(read)
    assert any(key[2] is None for key in snapshot.values()), "nothing fell back"


def test_a_call_that_shared_its_workdir_claims_nothing(tmp_path):
    """A shared workdir claims no outputs; a whole-tree diff exposes other calls' files."""
    from core.inference import tools

    workdir = tmp_path / "project-workspace"
    workdir.mkdir()

    theirs = tools._call_started(str(workdir))
    try:
        before = tools._snapshot_workdir_files(str(workdir))
        ours = tools._call_started(str(workdir))
        try:
            (workdir / "theirs.csv").write_text("a,b\n", encoding = "utf-8")
            sentinels = tools._created_file_sentinels(str(workdir), before, None, ours)
        finally:
            tools._call_finished(ours)
        assert ours["shared"] is True
        assert theirs["shared"] is True, "the call already running was not told"
    finally:
        tools._call_finished(theirs)

    assert sentinels == "", sentinels

    alone = tools._call_started(str(workdir))
    try:
        before = tools._snapshot_workdir_files(str(workdir))
        (workdir / "ours.csv").write_text("a,b\n", encoding = "utf-8")
        sentinels = tools._created_file_sentinels(str(workdir), before, None, alone)
    finally:
        tools._call_finished(alone)
    assert "ours.csv" in sentinels, sentinels


def test_a_read_serves_the_legacy_files_while_the_move_is_still_running(tmp_path, monkeypatch):
    """Moves take minutes, and a failed one leaves files there for the rest of the process."""
    fake_home = tmp_path / "userprofile"
    session = "__LOCALID_slow111"
    legacy = fake_home / "studio_sandbox" / session
    legacy.mkdir(parents = True)
    (legacy / "plot.png").write_bytes(b"png")

    monkeypatch.setenv("HOME", str(fake_home))
    monkeypatch.setenv("USERPROFILE", str(fake_home))
    tools = _shared_setup_2(monkeypatch, tmp_path)
    tools._legacy_sandbox_migrated = False

    served = Path(tools.resolve_sandbox_workdir(session))
    assert (served / "plot.png").is_file(), f"the card 404s until a later tool call: {served}"
    assert not (Path(tools.sandbox_root()) / session).exists()


def test_a_case_variant_id_cannot_delete_a_markerless_sandbox(tmp_path, monkeypatch):
    """Windows and default macOS fold Foo and foo together, so a case variant must not match."""
    tools = _shared_setup_2(monkeypatch, tmp_path)
    workdir = Path(tools.get_sandbox_workdir("Foo_chat1"))
    (workdir / "notes.txt").write_text("theirs", encoding = "utf-8")
    (workdir / tools._SANDBOX_MARKER).unlink()
    _forget_sandbox_state(tools)

    # Case-sensitive host: model the folding volume by resolving both ids to one directory.
    real_session_dir = tools._session_dir
    monkeypatch.setattr(
        tools,
        "_session_dir",
        lambda root, session_id: (
            str(workdir)
            if session_id.casefold() == "foo_chat1"
            else real_session_dir(root, session_id)
        ),
    )

    assert tools.remove_session_sandbox("foo_chat1", delete_files = True) is False
    assert (workdir / "notes.txt").is_file(), "deleted another chat's files"
    assert tools.remove_session_sandbox("Foo_chat1", delete_files = True) is True


def test_a_delete_moves_only_its_own_session_up(tmp_path, monkeypatch):
    """A delete must not wait behind the whole-tree cross-filesystem copy of every chat."""
    fake_home = tmp_path / "userprofile"
    legacy_root = fake_home / "studio_sandbox"
    session = "__LOCALID_quick11"
    (legacy_root / session).mkdir(parents = True)
    (legacy_root / session / "mine.txt").write_text("x", encoding = "utf-8")
    for other in range(3):
        (legacy_root / f"__LOCALID_other{other}").mkdir()

    monkeypatch.setenv("HOME", str(fake_home))
    monkeypatch.setenv("USERPROFILE", str(fake_home))
    tools = _shared_setup_2(monkeypatch, tmp_path)
    tools._legacy_sandbox_migrated = False
    monkeypatch.setattr(tools, "_start_legacy_migration", lambda: None)
    whole_tree = []
    monkeypatch.setattr(tools, "_migrate_legacy_sandbox", lambda root: whole_tree.append(root))

    tools.remove_session_sandbox(session, delete_files = True)

    assert whole_tree == [], "the delete waited for every other chat to move"
    assert not (legacy_root / session).exists(), "its own files were left behind"
    for other in range(3):
        assert (legacy_root / f"__LOCALID_other{other}").is_dir()


def test_a_deferred_removal_runs_outside_the_global_lock(tmp_path, monkeypatch):
    """The removal walks the tree, and other chats' tool calls wait on the global lock."""
    tools = _shared_setup_2(monkeypatch, tmp_path)
    session = "__LOCALID_defer11"
    tools.get_sandbox_workdir(session)

    held = []

    def slow_remove(session_id, delete_files):
        held.append(tools._active_sessions_lock.acquire(blocking = False))
        if held[-1]:
            tools._active_sessions_lock.release()
        return True

    monkeypatch.setattr(tools, "_remove_session_sandbox_locked", slow_remove)
    with tools._session_in_flight(session):
        tools.remove_session_sandbox(session, delete_files = True)

    assert held == [True], "the removal ran while holding the global lock"


def test_only_the_sandbox_tools_have_their_file_line_read(tmp_path):
    """MCP or fetched content may end in __FILES__; only sandbox tools have it stripped."""
    from core.inference.tool_loop_controller import strip_result_for_model

    printed = 'here is the manifest\n__FILES__:[{"name": "report.csv", "size": 1}]'

    assert "__FILES__" not in strip_result_for_model(printed, "python")
    assert "__FILES__" not in strip_result_for_model(printed, "terminal")
    assert strip_result_for_model(printed, "mcp__files__list") == printed
    assert strip_result_for_model(printed, "web_search") == printed


def test_a_migration_that_could_not_move_in_is_adopted(tmp_path, monkeypatch):
    """After a failed rename the marked staging directory is the only copy, so it is adopted."""
    tools = _shared_setup_2(monkeypatch, tmp_path)
    session = "__LOCALID_stage11"
    root = Path(tools.sandbox_root())
    root.mkdir(parents = True, exist_ok = True)
    stranded = root / f"{session}{tools._STAGING_SUFFIX}0123abcd"
    stranded.mkdir()
    (stranded / "thesis.txt").write_text("years of work", encoding = "utf-8")
    tools._mark_sandbox(str(stranded), session)

    served = Path(tools.resolve_sandbox_workdir(session))
    assert (served / "thesis.txt").is_file(), f"the only copy was unreachable: {served}"

    workdir = Path(tools.get_sandbox_workdir(session))
    assert (workdir / "thesis.txt").is_file(), "the next tool call started in an empty folder"


def test_bulk_deletes_share_one_sweeper(tmp_path, monkeypatch):
    """A clear-all hands over every chat, so one thread per chat would exhaust the process."""
    tools = _shared_setup_2(monkeypatch, tmp_path)
    started = []
    real_thread = threading.Thread

    class CountingThread(real_thread):
        def start(self):
            started.append(self.name)
            super().start()

    monkeypatch.setattr(threading, "Thread", CountingThread)
    monkeypatch.setattr(tools.threading, "Thread", CountingThread)
    tools._delete_worker = None

    for i in range(12):
        session = f"__LOCALID_bulk{i:03d}"
        workdir = Path(tools.get_sandbox_workdir(session))
        (workdir / "out.csv").write_text("a,b\n", encoding = "utf-8")
        assert tools.remove_session_sandbox(session, delete_files = True) is True

    deadline = time.monotonic() + 10
    while time.monotonic() < deadline:
        if not any(name.startswith(f"__LOCALID_bulk") for name in os.listdir(tools.sandbox_root())):
            break
        time.sleep(0.05)

    assert started.count("sandbox-delete") == 1, started
    for i in range(12):
        assert not (Path(tools.sandbox_root()) / f"__LOCALID_bulk{i:03d}").exists()


def test_one_chats_legacy_copy_does_not_hold_up_another(tmp_path, monkeypatch):
    """A single move lock made a tool call wait behind another chat's multi-gigabyte copy."""
    fake_home = tmp_path / "userprofile"
    legacy_root = fake_home / "studio_sandbox"
    for name in ("__LOCALID_huge111", "__LOCALID_tiny111"):
        (legacy_root / name).mkdir(parents = True)
        (legacy_root / name / "data.bin").write_bytes(b"x")

    monkeypatch.setenv("HOME", str(fake_home))
    monkeypatch.setenv("USERPROFILE", str(fake_home))
    tools = _shared_setup_2(monkeypatch, tmp_path)
    tools._legacy_sandbox_migrated = False
    getattr(tools, "_legacy_session_locks", {}).clear()

    started = threading.Event()
    release = threading.Event()
    real_move = tools._staged_move

    def slow_move(source, target, name):
        if "huge" in name:
            started.set()
            assert release.wait(timeout = 5)
        return real_move(source, target, name)

    monkeypatch.setattr(tools, "_staged_move", slow_move)

    big = threading.Thread(
        target = tools._migrate_one_legacy_session,
        args = (tools.sandbox_root(), "__LOCALID_huge111"),
    )
    big.start()
    try:
        assert started.wait(timeout = 5)
        small = threading.Thread(
            target = tools._migrate_one_legacy_session,
            args = (tools.sandbox_root(), "__LOCALID_tiny111"),
        )
        small.start()
        small.join(timeout = 5)
        assert not small.is_alive(), "the small chat waited on the big chat's copy"
        assert (Path(tools.sandbox_root()) / "__LOCALID_tiny111" / "data.bin").is_file()
    finally:
        release.set()
        big.join(timeout = 5)


def test_an_absolute_session_id_cannot_reach_outside_the_sentinel(tmp_path, monkeypatch):
    """A query-supplied id that is absolute makes os.path.join drop the sandbox root."""
    root, tools = _shared_setup_5(monkeypatch, tmp_path)
    tools._NOTHING_ROOT = None

    for hostile in ("/etc", "/", os.path.join("..", "..", "etc")):
        resolved = Path(tools._nothing_to_serve(hostile))
        assert not (resolved / "passwd").exists(), resolved
        assert str(resolved).startswith(tempfile.gettempdir()), resolved

    session = "/etc"
    theirs = root / tools._sandbox_name(session)
    theirs.mkdir()
    served = Path(tools.resolve_sandbox_workdir(session))
    assert not str(served).startswith("/etc"), served
    assert str(served) != "/", served


def test_a_legacy_copy_of_a_folder_we_already_moved_is_left_alone(tmp_path, monkeypatch):
    """The destination already has this chat's marker, so a second move would strand the copy."""
    fake_home = tmp_path / "userprofile"
    session = "__LOCALID_twice11"
    legacy = fake_home / "studio_sandbox" / session
    legacy.mkdir(parents = True)
    (legacy / "old.csv").write_text("old", encoding = "utf-8")

    monkeypatch.setenv("HOME", str(fake_home))
    monkeypatch.setenv("USERPROFILE", str(fake_home))
    root, tools = _shared_setup_5(monkeypatch, tmp_path)
    tools._legacy_sandbox_migrated = False
    getattr(tools, "_legacy_session_locks", {}).clear()
    moved = root / session
    moved.mkdir()
    (moved / "new.csv").write_text("new", encoding = "utf-8")
    tools._mark_sandbox(str(moved), session)

    tools._migrate_one_legacy_session(str(root), session)

    assert (legacy / "old.csv").is_file(), "the legacy copy went somewhere unreachable"
    assert Path(tools.resolve_sandbox_workdir(session)) == moved
    assert len(list(root.iterdir())) == 1, list(root.iterdir())


def test_an_interrupted_delete_is_swept_even_without_its_marker(tmp_path, monkeypatch):
    """A tool may delete the marker first; only this code names such a tombstone in our root."""
    tools = _shared_setup_2(monkeypatch, tmp_path)
    root = Path(tools.sandbox_root())
    root.mkdir(parents = True, exist_ok = True)
    tombstone = root / f"__LOCALID_gone222{tools._DETACHED_SUFFIX}0123abcd"
    tombstone.mkdir()
    (tombstone / "big.bin").write_bytes(b"x" * 16)

    tools.sweep_detached_sandboxes(str(root))
    assert not tombstone.exists(), "an unreachable tree was left on disk for good"


def test_the_startup_pass_lands_a_chat_whose_names_are_taken(tmp_path, monkeypatch):
    """Reporting complete after a skipped collision disables legacy reads and the retry."""
    fake_home = tmp_path / "userprofile"
    session = "__LOCALID_startup1"
    legacy = fake_home / "studio_sandbox" / session
    legacy.mkdir(parents = True)
    (legacy / "notes.csv").write_text("a,b\n", encoding = "utf-8")

    monkeypatch.setenv("HOME", str(fake_home))
    monkeypatch.setenv("USERPROFILE", str(fake_home))
    root, tools = _shared_setup_5(monkeypatch, tmp_path)
    tools._legacy_sandbox_migrated = False
    for taken in (root / session, root / f"{session}-{tools._name_suffix(session)}"):
        taken.mkdir()
        (taken / "theirs.txt").write_text("mine", encoding = "utf-8")

    tools._migrate_legacy_sandbox(str(root))

    assert tools._legacy_sandbox_migrated is True
    landed = Path(tools.resolve_sandbox_workdir(session))
    assert (landed / "notes.csv").is_file(), f"the chat's files were stranded: {landed}"


def test_a_fallback_is_found_in_a_root_full_of_other_folders(tmp_path, monkeypatch):
    """A bounded, sorted scan can miss the fallback in a big root, so launches made new ones."""
    root, tools = _shared_setup_5(monkeypatch, tmp_path)
    session = "zz_LOCALID_late11"
    name = tools._sandbox_name(session)
    for taken in (root / name, root / f"{name}-{tools._name_suffix(session)}"):
        taken.mkdir()

    workdir = Path(tools.get_sandbox_workdir(session))
    (workdir / "report.csv").write_text("a,b\n", encoding = "utf-8")

    monkeypatch.setattr(tools, "_MAX_SNAPSHOT_DIRS", 4)
    for i in range(40):
        (root / f"aaa{i:03d}").mkdir()

    _forget_sandbox_state(tools)
    assert Path(tools.resolve_sandbox_workdir(session)) == workdir
    assert Path(tools.get_sandbox_workdir(session)) == workdir
    assert tools.remove_session_sandbox(session, delete_files = True) is True


def test_a_staged_move_is_marked_before_the_rename(tmp_path, monkeypatch):
    """Once the legacy copy is gone, a kill before the marker leaves the only copy unfindable."""
    fake_home = tmp_path / "userprofile"
    session = "__LOCALID_kill111"
    legacy = fake_home / "studio_sandbox" / session
    legacy.mkdir(parents = True)
    (legacy / "thesis.txt").write_text("years of work", encoding = "utf-8")

    monkeypatch.setenv("HOME", str(fake_home))
    monkeypatch.setenv("USERPROFILE", str(fake_home))
    tools = _shared_setup_2(monkeypatch, tmp_path)
    root = Path(tools.sandbox_root())
    root.mkdir(parents = True, exist_ok = True)

    class Killed(RuntimeError):
        pass

    real_rename = os.rename

    def killed_rename(src, dst):
        if tools._STAGING_SUFFIX in str(dst):
            return real_rename(src, dst)
        raise Killed("the process went away here")

    monkeypatch.setattr(os, "rename", killed_rename)
    with pytest.raises(Killed):
        tools._staged_move(str(legacy), str(root / session), session)
    monkeypatch.undo()

    staged = [p for p in root.iterdir() if tools._STAGING_SUFFIX in p.name]
    assert staged, "the tree vanished"
    assert tools._marker_owner(str(staged[0])) == tools._sandbox_name(session)


def test_a_delete_finds_the_folder_this_run_made(tmp_path, monkeypatch):
    """With the marker removed, a delete must still find the folder this run made."""
    root, tools = _shared_setup_5(monkeypatch, tmp_path)
    session = "__LOCALID_cache11"
    (root / tools._sandbox_name(session)).mkdir()
    workdir = Path(tools.get_sandbox_workdir(session))
    (workdir / "report.csv").write_text("a,b\n", encoding = "utf-8")
    (workdir / tools._SANDBOX_MARKER).unlink()

    assert tools.session_sandbox_has_files(session) is True
    assert tools.remove_session_sandbox(session, delete_files = True) is True
    assert not workdir.exists(), "the folder was left behind with nobody able to reach it"


def test_a_chat_moved_out_of_a_project_survives_its_deletion():
    """Membership is read in the transaction; a stale listing would delete a chat moved out."""
    route = _shared_setup_9()
    assert "list_chat_threads(project_id" not in route
    assert 'member_ids = list(project.get("memberIds") or [])' in route
    assert route.index("delete_chat_project(") < route.index("member_ids = list(")


def test_the_client_reads_the_file_line_only_from_the_sandbox_tools():
    """Content from MCP or fetched pages ending in that line is not a sandbox file card."""
    src = Path(__file__).resolve().parents[2] / "frontend/src"

    files = (src / "components/assistant-ui/sandbox-files.ts").read_text(encoding = "utf-8")
    assert "SANDBOX_FILE_TOOLS" in files
    assert '"python", "terminal"' in files

    adapter = (src / "features/chat/api/chat-adapter.ts").read_text(encoding = "utf-8")
    guarded = adapter[adapter.index("const rawEvent = (toolEvent.result as string)") :]
    guarded = guarded[: guarded.index("const imgMarker")]
    assert "SANDBOX_FILE_TOOLS.has(" in guarded
    assert guarded.index("SANDBOX_FILE_TOOLS.has(") < guarded.index("extractCreatedFiles(")


def test_the_old_shared_bucket_is_read_but_never_moved(tmp_path, monkeypatch):
    """The old shared bucket belongs to no single chat, so it is read but never moved."""
    fake_home = tmp_path / "userprofile"
    bucket = fake_home / "studio_sandbox" / "_invalid"
    bucket.mkdir(parents = True)
    (bucket / "old.csv").write_text("a,b\n", encoding = "utf-8")

    monkeypatch.setenv("HOME", str(fake_home))
    monkeypatch.setenv("USERPROFILE", str(fake_home))
    tools = _shared_setup_2(monkeypatch, tmp_path)
    tools._legacy_sandbox_migrated = False
    session = "client.v1"
    assert tools._usable_session_id(session) is False

    served = Path(tools.resolve_sandbox_workdir(session))
    assert (served / "old.csv").is_file(), f"the chat lost its files: {served}"

    tools._migrate_legacy_sandbox(tools.sandbox_root())
    assert (bucket / "old.csv").is_file(), "a shared bucket was moved as one chat's"
    assert not (Path(tools.sandbox_root()) / "_invalid").exists()
    assert (Path(tools.resolve_sandbox_workdir(session)) / "old.csv").is_file()


def test_a_chat_cannot_claim_another_chats_directory(tmp_path, monkeypatch):
    """Tool code runs inside the sandbox and can write anything into the marker,
    so adopting on the marker alone hands one chat another chat's files."""
    root, tools = _shared_setup_5(monkeypatch, tmp_path)
    attacker, victim = "__LOCALID_aaa1111", "__LOCALID_bbb2222"
    theirs = Path(tools.get_sandbox_workdir(attacker))
    (theirs / "private.csv").write_text("the attacker's own", encoding = "utf-8")
    (theirs / tools._SANDBOX_MARKER).write_text(
        tools._sandbox_name(victim),
        encoding = "utf-8",
    )

    _forget_sandbox_state(tools)
    landed = Path(tools.get_sandbox_workdir(victim))
    assert landed != theirs, "one chat was handed another chat's directory"
    assert not (landed / "private.csv").exists()
    assert theirs.is_dir(), "and the other chat's folder was taken from it"


def test_a_users_own_marker_file_survives_the_migration(tmp_path, monkeypatch):
    """The name was not reserved before this change, so a chat that wrote its
    own .unsloth_sandbox has a real file there."""
    fake_home = tmp_path / "userprofile"
    session = "__LOCALID_marker1"
    legacy = fake_home / "studio_sandbox" / session
    legacy.mkdir(parents = True)
    (legacy / ".unsloth_sandbox").write_text("notes the user wrote", encoding = "utf-8")

    monkeypatch.setenv("HOME", str(fake_home))
    monkeypatch.setenv("USERPROFILE", str(fake_home))
    tools = _shared_setup_2(monkeypatch, tmp_path)
    tools._legacy_sandbox_migrated = False

    landed = Path(tools.get_sandbox_workdir(session))
    saved = list(landed.glob(".unsloth_sandbox.saved*"))
    assert saved, sorted(p.name for p in landed.iterdir())
    assert saved[0].read_text(encoding = "utf-8") == "notes the user wrote"
    assert tools._marker_owner(str(landed)) == tools._sandbox_name(session)


def test_a_project_workspace_goes_after_its_tools_are_stopped():
    """Member calls run with cwd in the workspace, so it must outlive their tools."""
    route = _shared_setup_9()
    assert "delete_chat_project(project_id, delete_files = False)" in route
    assert route.index("_cancel_active_generations(member_ids)") < route.index(
        "delete_project_workspace"
    )


def test_a_forked_chat_keeps_the_files_its_cards_point_at(tmp_path, monkeypatch):
    """Forks copy message content verbatim, so their cards still name the source sandbox."""
    asyncio, chat_history, tools = _shared_setup_8(monkeypatch, tmp_path)

    _forget_sandbox_state(tools)
    source = "__LOCALID_source1"
    workdir = Path(tools.get_sandbox_workdir(source))
    (workdir / "report.csv").write_text("a,b\n", encoding = "utf-8")

    import storage.studio_db as studio_db

    monkeypatch.setattr(
        studio_db, "sandbox_is_referenced_elsewhere", lambda session_id: session_id == source
    )

    removed, kept = asyncio.new_event_loop().run_until_complete(
        chat_history._remove_sandboxes([source], True)
    )

    assert removed == 0, removed
    assert kept == [source], kept
    assert (workdir / "report.csv").is_file(), "a surviving fork's cards now 404"


def test_the_research_loop_keeps_a_pages_file_line():
    """A resumed run reads back the excerpt, so a fetched page's final line stays content."""
    import inspect

    from core import research_runs

    source = inspect.getsource(research_runs)
    assert "strip_result_for_model(result)" not in source
    assert source.count('strip_result_for_model(result, "web_search")') == 2


def test_a_workspace_delete_waits_for_the_tool_calls_in_it(tmp_path, monkeypatch):
    """Cancel only asks; a call already in the executor keeps its cwd, so the delete waits."""
    tools = _shared_setup_2(monkeypatch, tmp_path)
    session = "project-abc123"
    assert tools.project_session_id("abc123") == session

    with tools._session_in_flight(session):
        assert tools.wait_for_sessions_idle([session], timeout = 0.2) is False
    assert tools.wait_for_sessions_idle([session], timeout = 0.2) is True

    route = _shared_setup_9()
    assert route.index("run_in_threadpool(wait_for_sessions_idle") < route.index(
        "run_in_threadpool(delete_project_workspace, project)"
    )


def test_a_reference_is_a_session_id_not_a_piece_of_prose(tmp_path, monkeypatch):
    """Short ids match ordinary prose, and JSON-escaped ids never match themselves."""
    monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(tmp_path / "home"))

    from storage import studio_db

    studio_db.upsert_chat_thread(
        {
            "id": "keeper",
            "title": "t",
            "modelType": "local",
            "modelId": "m",
            "createdAt": 1,
            "updatedAt": 1,
        }
    )
    quoted = 'client"v1'
    studio_db.upsert_chat_message(
        {
            "id": "m1",
            "threadId": "keeper",
            "role": "assistant",
            "content": [{"type": "text", "sessionId": quoted}],
            "createdAt": 1,
        }
    )
    studio_db.upsert_chat_message(
        {
            "id": "m2",
            "threadId": "keeper",
            "role": "assistant",
            "content": [{"type": "text", "text": "the report is in chat abc"}],
            "createdAt": 2,
        }
    )

    assert studio_db.sandbox_is_referenced_elsewhere(quoted) is True
    assert studio_db.sandbox_is_referenced_elsewhere("abc") is False


def test_a_project_workspace_a_fork_still_shows_is_kept():
    """A fork keeps cards for the shared workspace, so the workspace is not deleted."""
    route = _shared_setup_9()
    assert "project_session_id(project_id)" in route
    assert "sandbox_is_referenced_elsewhere, shared" in route
    assert route.index("sandbox_is_referenced_elsewhere, shared") < route.index(
        "run_in_threadpool(delete_project_workspace, project)"
    )


def test_a_project_delete_cancels_the_research_it_removed():
    """Research runs cascade with the threads, so their ids are read before the transaction."""
    chat_history, inspect, studio_db = _shared_setup_17()

    storage = inspect.getsource(studio_db.delete_chat_project)
    assert 'project["activeResearchRunIds"] = active_runs' in storage
    assert storage.index("SELECT id FROM research_runs") < storage.index("DELETE FROM chat_threads")

    route = inspect.getsource(chat_history.delete_project)
    assert '_cancel_research_runs(request, list(project.get("activeResearchRunIds")' in route
    assert "_cancel_active_research(request, member_ids)" not in route


def test_the_wait_covers_the_session_a_project_tool_runs_as():
    """A tool call in a project runs as project-<id>, not as the chat, so
    waiting on the member ids returned at once and the cwd went underneath."""
    route = _shared_setup_9()
    assert "wait_for_sessions_idle, [shared, *member_ids]" in route
    assert route.index("shared = project_session_id(project_id)") < route.index(
        "run_in_threadpool(wait_for_sessions_idle"
    )


def test_a_new_call_cannot_start_in_a_sandbox_being_removed(tmp_path, monkeypatch):
    """With the lock released, a call starting during removal must not get the directory."""
    tools = _shared_setup_2(monkeypatch, tmp_path)
    session = "__LOCALID_race111"
    tools.get_sandbox_workdir(session)

    queued, proceed = threading.Event(), threading.Event()
    removing, release, started = threading.Event(), threading.Event(), threading.Event()
    real_remove = tools._remove_session_sandbox_locked

    def slow_remove(session_id, delete_files):
        removing.set()
        assert release.wait(timeout = 5)
        return real_remove(session_id, delete_files)

    monkeypatch.setattr(tools, "_remove_session_sandbox_locked", slow_remove)

    def first_call():
        with tools._session_in_flight(session):
            queued.set()
            assert proceed.wait(timeout = 5)

    def second_call():
        with tools._session_in_flight(session):
            started.set()

    first = threading.Thread(target = first_call)
    first.start()
    try:
        assert queued.wait(timeout = 5)
        assert tools.remove_session_sandbox(session, delete_files = True) is False
        proceed.set()
        assert removing.wait(timeout = 5), "the deferred removal never ran"

        second = threading.Thread(target = second_call)
        second.start()
        assert not started.wait(timeout = 0.5), "a call started in the folder being removed"
        release.set()
        second.join(timeout = 5)
        assert started.is_set(), "and it never got to start afterwards"
    finally:
        release.set()
        proceed.set()
        first.join(timeout = 5)


def test_a_note_named_like_the_marker_is_kept(tmp_path, monkeypatch):
    """A short note is a valid session name, yet migration must not read it as metadata."""
    fake_home = tmp_path / "userprofile"
    session = "__LOCALID_notes11"
    legacy = fake_home / "studio_sandbox" / session
    legacy.mkdir(parents = True)
    (legacy / ".unsloth_sandbox").write_text("notes", encoding = "utf-8")

    monkeypatch.setenv("HOME", str(fake_home))
    monkeypatch.setenv("USERPROFILE", str(fake_home))
    tools = _shared_setup_2(monkeypatch, tmp_path)
    tools._legacy_sandbox_migrated = False

    landed = Path(tools.get_sandbox_workdir(session))
    saved = list(landed.glob(".unsloth_sandbox.saved*"))
    assert saved, sorted(p.name for p in landed.iterdir())
    assert saved[0].read_text(encoding = "utf-8") == "notes"
    assert tools._marker_owner(str(landed)) == tools._sandbox_name(session)


def test_a_kept_project_workspace_still_resolves(tmp_path, monkeypatch):
    """Kept, since a fork's cards resolve through the project row that is now gone."""
    tools = _shared_setup_22(monkeypatch, tmp_path)
    from utils.paths import project_workspaces_root

    _forget_sandbox_state(tools)
    project_id = "proj12345"
    suffix = project_id[:8]
    workspace = Path(project_workspaces_root()) / f"My Notes-{suffix}" / "sandbox"
    workspace.mkdir(parents = True)
    (workspace / "report.csv").write_text("a,b\n", encoding = "utf-8")

    served = Path(tools.resolve_sandbox_workdir(tools.project_session_id(project_id)))
    assert served == workspace.resolve(), served
    assert (served / "report.csv").is_file()


def test_clearing_every_chat_cancels_the_research_it_removed():
    """Runs cascade with threads, so their ids are read first, or the worker keeps running."""
    chat_history, inspect, studio_db = _shared_setup_17()

    storage = inspect.getsource(studio_db.clear_chat_history_with_replay_status)
    assert "SELECT id FROM research_runs" in storage
    assert storage.index("SELECT id FROM research_runs") < storage.index("DELETE FROM chat_threads")

    route = inspect.getsource(chat_history.clear_history)
    assert "_cancel_research_runs(request, cleared_runs)" in route


def test_the_supervisor_is_told_even_with_no_row_left():
    """A KeyError from request_cancel after a cascade must not skip telling the supervisor."""
    import inspect

    from routes import chat_history

    source = inspect.getsource(chat_history._cancel_research_runs)
    assert source.index("cancel_account_run(request, run_id,") < source.index(
        "research_runs_db.request_cancel(run_id)"
    )


def test_a_workspace_is_kept_when_the_wait_ran_out():
    """The bound exists so the request cannot hang, and past it the tool call
    is still there: removing its working directory is the worse outcome."""
    route = _shared_setup_9()
    assert "run_in_threadpool(wait_for_sessions_idle, [shared, *member_ids])" in route
    assert "if delete_files and idle and not referenced and not recreated:" in route
    assert route.index(
        "if delete_files and idle and not referenced and not recreated:"
    ) < route.index("run_in_threadpool(delete_project_workspace, project)")
    assert "finish_workspace_delete_when_idle(project_id)" in route


def test_a_kept_workspace_the_user_moved_still_resolves(tmp_path, monkeypatch):
    """A custom rootPath cannot be derived from the id, and the delete just removed its row."""
    tools = _shared_setup_22(monkeypatch, tmp_path)

    _forget_sandbox_state(tools)
    project_id = "proj98765"
    custom = tmp_path / "somewhere" / "of the user's" / "sandbox"
    custom.mkdir(parents = True)
    (custom / "report.csv").write_text("a,b\n", encoding = "utf-8")

    tools.record_orphaned_project(project_id, str(custom))
    served = Path(tools.resolve_sandbox_workdir(tools.project_session_id(project_id)))
    assert served == custom.resolve(), served
    assert (served / "report.csv").is_file()

    shutil.rmtree(custom)
    assert tools.list_orphaned_projects() == []


def test_the_last_fork_going_takes_the_kept_workspace(tmp_path, monkeypatch):
    """The user asked for the files, and nothing else would ever come back to that workspace."""
    asyncio, chat_history, tools = _shared_setup_8(monkeypatch, tmp_path)
    from storage import studio_db

    _forget_sandbox_state(tools)
    project_id = "proj55555"
    workspace = tmp_path / "Notes-proj5555"
    (workspace / "sandbox").mkdir(parents = True)
    (workspace / "report.csv").write_text("a,b\n", encoding = "utf-8")
    tools.record_orphaned_project(
        project_id,
        str(workspace / "sandbox"),
        True,
        str(workspace),
    )

    monkeypatch.setattr(studio_db, "sandbox_is_referenced_elsewhere", lambda s, e = None: True)
    asyncio.new_event_loop().run_until_complete(chat_history._remove_sandboxes([], True))
    assert (workspace / "report.csv").is_file()

    monkeypatch.setattr(studio_db, "sandbox_is_referenced_elsewhere", lambda s, e = None: False)
    asyncio.new_event_loop().run_until_complete(chat_history._remove_sandboxes([], True))
    assert not workspace.exists(), "the workspace was orphaned for good"
    assert tools.list_orphaned_projects() == []


def test_a_tool_renaming_the_marker_does_not_move_the_chat(tmp_path, monkeypatch):
    """A tool may rewrite the marker; the chat must keep its own directory, not follow the id."""
    root, tools = _shared_setup_5(monkeypatch, tmp_path)
    session = "__LOCALID_owned11"
    (root / tools._sandbox_name(session)).mkdir()
    workdir = Path(tools.get_sandbox_workdir(session))
    (workdir / "plot.png").write_bytes(b"x")
    (workdir / tools._SANDBOX_MARKER).write_text("notes", encoding = "utf-8")

    again = Path(tools.get_sandbox_workdir(session))
    assert again == workdir, "the chat walked away from the files it just wrote"
    assert tools._marker_owner(str(workdir)) == tools._sandbox_name(session)
    saved = list(workdir.glob(".unsloth_sandbox.saved*"))
    assert saved and saved[0].read_text(encoding = "utf-8") == "notes"


def test_an_empty_sandbox_scan_does_not_hold_the_global_lock(tmp_path, monkeypatch):
    """Scanning up to 2,000 directories must not hold the global lock every tool call needs."""
    tools = _shared_setup_2(monkeypatch, tmp_path)
    session = "__LOCALID_scan111"
    tools.get_sandbox_workdir(session)

    held = []

    def slow_scan(target, owner = None):
        held.append(tools._active_sessions_lock.acquire(blocking = False))
        if held[-1]:
            tools._active_sessions_lock.release()
        return True

    monkeypatch.setattr(tools, "_holds_no_user_files", slow_scan)
    tools.remove_session_sandbox(session, delete_files = False)

    assert held == [True], "the scan ran while holding the global lock"


def test_a_cache_directory_needs_a_generated_file_not_a_name(tmp_path, monkeypatch):
    """The directory is prepended to sys.path for every worker, so a folder
    that merely carries the name lets anything beside it shadow a real module."""
    from utils import cache_cleanup

    candidate = tmp_path / "unsloth_compiled_cache"
    (candidate / "unsloth_compiled_module_fake.py").mkdir(parents = True)
    (candidate / "numpy.py").write_text("raise SystemExit", encoding = "utf-8")

    assert cache_cleanup._holds_generated_modules(candidate) is False

    (candidate / "unsloth_compiled_module_real.py").write_text("x = 1", encoding = "utf-8")
    assert cache_cleanup._holds_generated_modules(candidate) is True


def test_a_kept_workspace_is_recorded_even_when_nothing_was_deleted():
    """The custom-path row is gone either way, and a fork's cards still name that session."""
    route = _shared_setup_9()
    assert 'if project.get("sandboxPath"):' in route
    assert "if not delete_files:" in route
    body = route[route.index("if not delete_files:") :]
    assert "record_orphaned_project," in body[:400]
    assert "False," in body[:400], "a keep must not be recorded as pending deletion"


def test_only_a_pending_record_is_ever_collected(tmp_path, monkeypatch):
    """Records a fork's cards need were never asked to be deleted, so they stay."""
    studio_db, tools = _shared_setup_4(monkeypatch, tmp_path)
    monkeypatch.setattr(studio_db, "sandbox_is_referenced_elsewhere", lambda s, e = None: False)

    keep, go = tmp_path / "Keep-projkeep", tmp_path / "Go-projgone"
    (keep / "sandbox").mkdir(parents = True)
    (go / "sandbox").mkdir(parents = True)
    tools.record_orphaned_project("projkeep1", str(keep / "sandbox"), False, str(keep))
    tools.record_orphaned_project("projgone1", str(go / "sandbox"), True, str(go))

    tools.collect_orphaned_project_workspaces()

    assert keep.is_dir(), "deleted a workspace the user kept"
    assert not go.exists(), "left one the user asked to delete"


def test_a_workspace_delete_finishes_once_the_tool_call_ends(tmp_path, monkeypatch):
    """Afterwards the delete dialog's promise is pending, and nothing else would resolve it."""
    studio_db, tools = _shared_setup_4(monkeypatch, tmp_path)
    monkeypatch.setattr(studio_db, "sandbox_is_referenced_elsewhere", lambda s, e = None: False)

    project_id = "projbusy1"
    workspace = tmp_path / "Busy-projbusy"
    (workspace / "sandbox").mkdir(parents = True)
    (workspace / "out.csv").write_text("a,b\n", encoding = "utf-8")
    tools.record_orphaned_project(
        project_id,
        str(workspace / "sandbox"),
        True,
        str(workspace),
    )

    with tools._session_in_flight(tools.project_session_id(project_id)):
        tools.collect_orphaned_project_workspaces()
        assert workspace.is_dir()
        finisher = tools.finish_workspace_delete_when_idle(project_id, timeout = 5.0)
        time.sleep(0.2)
        assert workspace.is_dir(), "removed a workspace a tool call was using"

    finisher.join(timeout = 5)
    assert not workspace.exists(), "the promise was never finished"
    assert tools.list_orphaned_projects() == []


def test_a_pending_workspace_is_collected_by_a_plain_delete(tmp_path, monkeypatch):
    """Deleting the last chat that references a pending workspace must still collect it."""
    asyncio, chat_history, tools = _shared_setup_8(monkeypatch, tmp_path)
    from storage import studio_db

    _forget_sandbox_state(tools)
    monkeypatch.setattr(studio_db, "sandbox_is_referenced_elsewhere", lambda s, e = None: False)

    workspace = tmp_path / "Promised-projplai"
    (workspace / "sandbox").mkdir(parents = True)
    (workspace / "out.csv").write_text("a,b\n", encoding = "utf-8")
    tools.record_orphaned_project(
        "projplain",
        str(workspace / "sandbox"),
        True,
        str(workspace),
    )

    asyncio.new_event_loop().run_until_complete(chat_history._remove_sandboxes([], False))

    assert not workspace.exists(), "the promised delete never happened"


def test_a_nested_file_named_like_the_marker_is_a_file(tmp_path, monkeypatch):
    """Only the sandbox's own bookkeeping is hidden: a tool that wrote
    archive/.unsloth_sandbox made an ordinary file."""
    tools = _shared_setup_2(monkeypatch, tmp_path)
    session = "__LOCALID_nested1"
    workdir = Path(tools.get_sandbox_workdir(session))
    before = tools._snapshot_workdir_files(str(workdir))
    (workdir / "archive").mkdir()
    (workdir / "archive" / tools._SANDBOX_MARKER).write_text("theirs", encoding = "utf-8")

    sentinels = tools._created_file_sentinels(str(workdir), before)
    assert "archive/.unsloth_sandbox" in sentinels, sentinels
    assert tools._SANDBOX_MARKER not in sentinels.replace("archive/.unsloth_sandbox", "")

    listing = tools._snapshot_workdir_files(str(workdir))
    assert "archive/.unsloth_sandbox" in listing
    assert tools._SANDBOX_MARKER not in listing


def test_the_listing_route_hides_the_same_names_as_the_snapshot():
    """The two walks have to agree, or a card names a file the download route
    refuses, or the other way round."""
    import inspect

    from routes import inference

    source = inspect.getsource(inference._sandbox_listing_names)
    assert "base == sandbox_dir and entry in _INTERNAL_SANDBOX_FILES" in source


def test_the_orphan_records_live_under_the_studio_home(tmp_path, monkeypatch):
    """Orphan records go under the studio home, since a sandbox root may be unwritable."""
    home = tmp_path / "home"
    readonly_parent = tmp_path / "readonly"
    sandbox = readonly_parent / "sandboxes"
    sandbox.mkdir(parents = True)
    monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(home))
    monkeypatch.setenv("UNSLOTH_STUDIO_SANDBOX_HOME", str(sandbox))

    from core.inference import tools

    _forget_sandbox_state(tools)
    records = Path(tools._orphan_records_dir())
    assert str(records).startswith(str(home)), records

    workspace = tmp_path / "workspace"
    workspace.mkdir()
    readonly_parent.chmod(0o500)
    try:
        tools.record_orphaned_project("projro123", str(workspace), True)
        assert [name for name, *_rest in tools.list_orphaned_projects()] == ["projro123"]
    finally:
        readonly_parent.chmod(0o700)


def test_an_image_below_a_subdirectory_keeps_its_separators():
    """Proxies refuse encoded slashes, so an image below a subdirectory must keep real ones."""
    view = (
        Path(__file__).resolve().parents[2]
        / "frontend/src/components/assistant-ui/python-tool-image-path.ts"
    ).read_text(encoding = "utf-8")

    assert "sandboxFilePath(sessionId, filename)" in view
    assert "encodeURIComponent(filename)" not in view


def test_an_ordinary_chat_never_reads_the_old_shared_bucket(tmp_path, monkeypatch):
    """That bucket holds every rejected id's files, so an ordinary chat must never read it."""
    fake_home = tmp_path / "userprofile"
    bucket = fake_home / "studio_sandbox" / "_invalid"
    bucket.mkdir(parents = True)
    (bucket / "someone-elses.csv").write_text("private", encoding = "utf-8")

    monkeypatch.setenv("HOME", str(fake_home))
    monkeypatch.setenv("USERPROFILE", str(fake_home))
    tools = _shared_setup_2(monkeypatch, tmp_path)
    tools._legacy_sandbox_migrated = False

    ordinary = "__LOCALID_plain11"
    assert tools._usable_session_id(ordinary) is True
    served = Path(tools.resolve_sandbox_workdir(ordinary))
    assert not (served / "someone-elses.csv").exists(), served

    rejected = "client.v1"
    assert tools._usable_session_id(rejected) is False
    served = Path(tools.resolve_sandbox_workdir(rejected))
    assert (served / "someone-elses.csv").is_file()


def test_a_case_variant_cannot_read_a_markerless_sandbox(tmp_path, monkeypatch):
    """Without a marker, a case variant like Foo matches foo on Windows and default macOS."""
    tools = _shared_setup_2(monkeypatch, tmp_path)
    workdir = Path(tools.get_sandbox_workdir("Bar_chat1"))
    (workdir / "notes.txt").write_text("theirs", encoding = "utf-8")
    (workdir / tools._SANDBOX_MARKER).unlink()

    assert tools._owned_by_session(str(workdir), "Bar_chat1") is True
    assert tools._owned_by_session(str(workdir), "bar_chat1") is False


def test_a_failed_workspace_delete_stays_pending(tmp_path, monkeypatch):
    """A locked file on Windows must keep the record, or the user's delete request is lost."""
    studio_db, tools = _shared_setup_4(monkeypatch, tmp_path)
    monkeypatch.setattr(studio_db, "sandbox_is_referenced_elsewhere", lambda s, e = None: False)

    workspace = tmp_path / "Stuck-projstuc"
    (workspace / "sandbox").mkdir(parents = True)
    (workspace / "locked.bin").write_bytes(b"x")
    tools.record_orphaned_project(
        "projstuck",
        str(workspace / "sandbox"),
        True,
        str(workspace),
    )

    monkeypatch.setattr(studio_db.shutil, "rmtree", lambda *a, **k: None)
    tools.collect_orphaned_project_workspaces()

    assert workspace.is_dir()
    assert [name for name, _p, _r, pending, _c in tools.list_orphaned_projects() if pending] == [
        "projstuck"
    ], "the record was forgotten with the workspace still there"

    monkeypatch.undo()
    monkeypatch.setattr(studio_db, "sandbox_is_referenced_elsewhere", lambda s, e = None: False)
    monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(tmp_path / "home"))
    tools.collect_orphaned_project_workspaces()
    assert not workspace.exists(), "the retry never happened"


def test_a_deferred_delete_removes_the_whole_workspace(tmp_path, monkeypatch):
    """The immediate path removes the whole workspace, so the deferred one must too."""
    tools = _shared_setup_22(monkeypatch, tmp_path)
    from storage import studio_db

    _forget_sandbox_state(tools)
    monkeypatch.setattr(studio_db, "sandbox_is_referenced_elsewhere", lambda s, e = None: False)

    project_id = "projwhole"
    root = Path(tmp_path / "projects" / f"My Notes-{project_id[:8]}")
    sandbox = root / "sandbox"
    sandbox.mkdir(parents = True)
    (sandbox / "out.csv").write_text("a,b\n", encoding = "utf-8")
    (root / "notes.md").write_text("beside the sandbox", encoding = "utf-8")

    tools.record_orphaned_project(project_id, str(sandbox), True, str(root))
    tools.collect_orphaned_project_workspaces()

    assert not root.exists(), sorted(p.name for p in root.iterdir()) if root.exists() else None


def _deleted_project(tmp_path, monkeypatch, project_id, workspace):
    """Drive delete_project for a project whose only member is itself."""
    import asyncio

    from routes import chat_history

    project = {
        "id": project_id,
        "name": "Notes",
        "createdAt": 1,
        "updatedAt": 1,
        "sandboxPath": str(workspace / "sandbox"),
        "rootPath": str(workspace),
        "memberIds": [],
        "activeResearchRunIds": [],
    }
    monkeypatch.setattr(
        chat_history, "delete_chat_project", lambda pid, delete_files: dict(project)
    )
    monkeypatch.setattr(chat_history, "_cancel_research_runs", lambda request, ids: None)
    monkeypatch.setattr(chat_history, "_cancel_active_generations", lambda ids: None)
    return asyncio.new_event_loop().run_until_complete(
        chat_history.delete_project(
            project_id,
            request = None,
            delete_files = True,
            current_subject = "test",
        )
    )


def test_a_workspace_delete_that_declined_can_still_be_retried(tmp_path, monkeypatch):
    """Rows are gone by then, so a declined delete must keep the record to retry."""
    studio_db, tools = _shared_setup_4(monkeypatch, tmp_path)
    project_id = "proj31415"
    workspace = tmp_path / "Notes-proj3141"
    (workspace / "sandbox").mkdir(parents = True)
    (workspace / "sandbox" / "report.csv").write_text("a,b\n", encoding = "utf-8")

    monkeypatch.setattr(studio_db, "delete_project_workspace", lambda project: None)
    monkeypatch.setattr(studio_db, "sandbox_is_referenced_elsewhere", lambda s, e = None: False)
    _deleted_project(tmp_path, monkeypatch, project_id, workspace)

    records = tools.list_orphaned_projects()
    assert records == [
        (
            project_id,
            str((workspace / "sandbox").resolve()),
            str(workspace.resolve()),
            True,
            False,
        )
    ], records

    monkeypatch.undo()
    monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(tmp_path / "home"))
    monkeypatch.setattr(studio_db, "sandbox_is_referenced_elsewhere", lambda s, e = None: False)
    tools.collect_orphaned_project_workspaces()
    assert not workspace.exists()


def test_a_half_deleted_workspace_keeps_its_record(tmp_path, monkeypatch):
    """Only the sandbox went; dropping the record would lose the path and the user's request."""
    tools = _shared_setup_2(monkeypatch, tmp_path)
    project_id = "proj27182"
    workspace = tmp_path / "Notes-proj2718"
    sandbox = workspace / "sandbox"
    sandbox.mkdir(parents = True)
    tools.record_orphaned_project(project_id, str(sandbox), True, str(workspace))

    shutil.rmtree(sandbox)
    (workspace / "datasets").mkdir()
    records = tools.list_orphaned_projects()
    assert [(r[0], r[3]) for r in records] == [(project_id, True)], records
    assert tools._recorded_project_workdir(project_id) is None

    shutil.rmtree(workspace)
    assert tools.list_orphaned_projects() == []


def test_a_database_that_will_not_answer_keeps_the_files(monkeypatch):
    """False reads as "no other chat shows these files" and the caller then
    deletes them; a locked database is not that answer."""
    import sqlite3

    from storage import studio_db

    class _Broken:
        def execute(self, *args, **kwargs):
            raise sqlite3.OperationalError("database is locked")

        def close(self):
            pass

    monkeypatch.setattr(studio_db, "get_connection", lambda: _Broken())
    assert studio_db.sandbox_is_referenced_elsewhere("thread-1") is True


def test_the_default_sandbox_never_lands_in_a_directory_of_theirs(tmp_path, monkeypatch):
    """A session-less call falls back to _default, and in a shared root both
    that name and the derived one can already be the user's."""
    root, tools = _shared_setup_5(monkeypatch, tmp_path)
    (root / "_default").mkdir()
    theirs = root / f"_default_{tools._name_suffix('_default')}"
    theirs.mkdir()
    (theirs / "thesis.tex").write_text("x", encoding = "utf-8")

    workdir = Path(tools._sandbox_fallback(str(root), "_default", create = True))
    assert workdir != theirs, "a tool call would have run in the user's directory"
    assert tools._marker_owner(str(workdir)) == tools._sandbox_name("_default")
    assert not (workdir / "thesis.tex").exists()
    assert Path(tools._sandbox_fallback(str(root), "_default")) == workdir


def test_the_default_sandbox_is_not_created_through_a_link(tmp_path, monkeypatch):
    """makedirs(exist_ok) follows a directory symlink, so the marker would land in its target."""
    root, tools = _shared_setup_5(monkeypatch, tmp_path)
    (root / "_default").mkdir()
    outside = tmp_path / "elsewhere"
    outside.mkdir()
    (root / f"_default_{tools._name_suffix('_default')}").symlink_to(outside)

    workdir = Path(tools._sandbox_fallback(str(root), "_default", create = True))
    assert workdir.resolve() != outside.resolve(), "the call ran through the link"
    assert not (outside / tools._SANDBOX_MARKER).exists()


def test_a_sandbox_listing_does_not_resolve_on_the_event_loop(tmp_path, monkeypatch):
    """Resolving scans the root and the legacy tree, so it must not block the event loop."""
    import asyncio
    import threading

    monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(tmp_path / "home"))

    from routes import inference

    ran_on = []

    def slow_resolve(session_id, create = False):
        ran_on.append(threading.get_ident())
        return str(tmp_path / "nowhere")

    monkeypatch.setattr(inference, "_sandbox_dir_for", slow_resolve)
    monkeypatch.setattr(inference, "_authenticate_header_or_query", _noop_async)

    loop_thread = threading.get_ident()
    asyncio.new_event_loop().run_until_complete(
        inference.list_sandbox_files("thread-1", request = None, token = None, session = None)
    )
    assert ran_on and loop_thread not in ran_on, "resolution ran on the event loop"


def test_revealing_a_sandbox_opens_the_directory_it_resolved(tmp_path, monkeypatch):
    """The route hands the file manager the resolved sandbox, not the raw id."""
    import asyncio

    from pathlib import Path as _Path

    from routes import inference

    path_utils, sandbox = _shared_setup_23(tmp_path)
    opened = []

    monkeypatch.setattr(inference, "_sandbox_dir_for", lambda session_id, create: str(sandbox))
    monkeypatch.setattr(inference, "_authenticate_header_or_query", _noop_async)
    _shared_setup_20(monkeypatch, opened, path_utils)

    result = asyncio.new_event_loop().run_until_complete(
        inference.reveal_sandbox_dir("thread-1", request = None, token = None, session = None)
    )
    assert result["path"] == str(sandbox)
    assert opened == [_Path(str(sandbox))]


def test_a_sandbox_deleted_mid_request_does_not_reveal_the_root(tmp_path, monkeypatch):
    """The Linux branch opens the parent when the target is not a directory, and
    a sandbox's parent is the root holding every other chat's."""
    HTTPException, asyncio, inference = _shared_setup_14()

    root = tmp_path / "sandbox"
    sandbox = root / "thread-1"
    sandbox.mkdir(parents = True)
    opened = []

    def resolve_then_delete(session_id, create):
        shutil.rmtree(sandbox, ignore_errors = True)
        return str(sandbox)

    monkeypatch.setattr(inference, "_sandbox_dir_for", resolve_then_delete)
    monkeypatch.setattr(inference, "_authenticate_header_or_query", _noop_async)
    monkeypatch.setattr(subprocess, "Popen", lambda cmd, **kw: opened.append(list(cmd)))

    caught = _shared_setup_18(HTTPException, asyncio, inference)
    assert caught.value.status_code == 404
    assert opened == [], "the parent directory must never be opened"


def test_revealing_a_sandbox_that_was_never_created_is_a_404(tmp_path, monkeypatch):
    """Revealing a never-created sandbox is a 404, so no directory is materialised."""
    HTTPException, asyncio, inference = _shared_setup_14()
    from utils.paths import path_utils

    missing = tmp_path / "sandbox" / "thread-1"
    opened = []

    monkeypatch.setattr(inference, "_sandbox_dir_for", lambda session_id, create: str(missing))
    monkeypatch.setattr(inference, "_authenticate_header_or_query", _noop_async)
    _shared_setup_20(monkeypatch, opened, path_utils)

    caught = _shared_setup_18(HTTPException, asyncio, inference)
    assert caught.value.status_code == 404
    assert not missing.exists()
    assert opened == []


def test_a_missing_file_manager_is_not_reported_as_a_missing_folder(tmp_path, monkeypatch):
    """``xdg-open`` is absent on a headless host, and ``Popen`` then raises the
    same ``FileNotFoundError`` as a missing target. Conflating them tells a user
    whose files are right there that the chat has no folder."""
    HTTPException, asyncio, inference = _shared_setup_14()
    path_utils, sandbox = _shared_setup_23(tmp_path)

    def no_launcher(path, expect_dir = False):
        raise FileNotFoundError(2, "No such file or directory", "xdg-open")

    monkeypatch.setattr(inference, "_sandbox_dir_for", lambda session_id, create: str(sandbox))
    monkeypatch.setattr(inference, "_authenticate_header_or_query", _noop_async)
    monkeypatch.setattr(path_utils, "reveal_in_file_manager", no_launcher)

    caught = _shared_setup_18(HTTPException, asyncio, inference)
    assert caught.value.status_code == 500
    assert sandbox.is_dir(), "the folder was there the whole time"


def test_revealing_a_sandbox_demands_a_directory(tmp_path, monkeypatch):
    """Revealing a sandbox demands a directory, since a tool can swap it for a file before the open."""
    import asyncio
    import inspect

    from routes import inference

    path_utils, sandbox = _shared_setup_23(tmp_path)
    seen = []

    monkeypatch.setattr(inference, "_sandbox_dir_for", lambda session_id, create: str(sandbox))
    monkeypatch.setattr(inference, "_authenticate_header_or_query", _noop_async)
    monkeypatch.setattr(
        path_utils,
        "reveal_in_file_manager",
        lambda path, expect_dir = False: seen.append((path, expect_dir)),
    )

    asyncio.new_event_loop().run_until_complete(
        inference.reveal_sandbox_dir("thread-1", request = None, token = None, session = None)
    )
    assert seen and seen[0][1] is True, "the sandbox reveal must demand a directory"

    from routes import models

    # The cached-model reveal shares the helper and points at a file, so it must not inherit the flag.
    assert "expect_dir" not in inspect.getsource(models.reveal_cached_model)


def test_revealing_reads_the_session_query_the_frontend_falls_back_to(tmp_path, monkeypatch):
    """ASGI decodes ``%2F`` before routing, so a slashed id travels as
    ``/sandbox/_/reveal?session=``. The listing route already reads it and this
    one must agree, or such a chat silently reveals the wrong folder."""
    import asyncio

    from routes import inference
    from utils.paths import path_utils

    asked = []
    sandbox = tmp_path / "sandbox" / "resolved"
    sandbox.mkdir(parents = True)

    def resolve(session_id, create):
        asked.append(session_id)
        return str(sandbox)

    monkeypatch.setattr(inference, "_sandbox_dir_for", resolve)
    monkeypatch.setattr(inference, "_authenticate_header_or_query", _noop_async)
    monkeypatch.setattr(path_utils, "reveal_in_file_manager", lambda path, expect_dir = False: None)

    asyncio.new_event_loop().run_until_complete(
        inference.reveal_sandbox_dir("_", request = None, token = None, session = "thread/with/slashes")
    )
    assert asked == ["thread/with/slashes"], "the placeholder segment was resolved instead"


def test_revealing_authenticates_before_it_touches_the_filesystem(tmp_path, monkeypatch):
    """Resolution scans the sandbox root, so auth must run before any filesystem access."""
    HTTPException, asyncio, inference = _shared_setup_14()

    resolved = []
    opened = []

    async def refuse(request, token):
        raise HTTPException(status_code = 401, detail = "Not authenticated")

    monkeypatch.setattr(inference, "_authenticate_header_or_query", refuse)
    monkeypatch.setattr(inference, "_sandbox_dir_for", lambda *a, **k: resolved.append(a) or "")
    monkeypatch.setattr(subprocess, "Popen", lambda cmd, **kw: opened.append(list(cmd)))

    caught = _shared_setup_18(HTTPException, asyncio, inference)
    assert caught.value.status_code == 401
    assert resolved == [] and opened == []


def test_a_traversal_id_stays_inside_the_sandbox_root_and_opens_nothing(tmp_path, monkeypatch):
    """Traversal ids derive a name inside the root and launch nothing; an id is never a path."""
    import asyncio

    from fastapi import HTTPException

    # Fake home: the legacy root reads expanduser("~"), so a real _invalid bucket would answer 200.
    _shared_setup_11(tmp_path / "fake-home", monkeypatch, tmp_path)
    legacy_bucket = tmp_path / "fake-home" / "studio_sandbox" / "_invalid"
    assert (
        not legacy_bucket.exists()
    ), "the refusals below only hold while the legacy bucket is absent"

    from routes import inference

    monkeypatch.setattr(inference, "_authenticate_header_or_query", _noop_async)
    opened = []
    monkeypatch.setattr(subprocess, "Popen", lambda cmd, **kw: opened.append(list(cmd)))

    for probe in (
        "../../../../etc",
        "..",
        "/etc",
        "thread/../../../etc",
        "..\\..\\windows",
        "C:\\Windows",
        "con",
        "x" * 300,
    ):
        with pytest.raises(HTTPException) as caught:
            asyncio.new_event_loop().run_until_complete(
                inference.reveal_sandbox_dir("_", request = None, token = None, session = probe)
            )
        assert caught.value.status_code == 404, probe
        resolved = inference._sandbox_dir_for(probe, create = False)
        assert not os.path.exists("/etc/.unsloth_sandbox")
        assert (
            Path(resolved).is_relative_to(tmp_path / "home") or not Path(resolved).exists()
        ), probe
    assert opened == [], "a refused id must never reach the file manager"


def test_an_unusable_id_still_reads_the_legacy_shared_bucket(tmp_path, monkeypatch):
    """Unusable ids still read the legacy bucket, so those chats' files stay reachable after the upgrade."""
    from core.inference import tools

    _shared_setup_11(tmp_path / "fake-home", monkeypatch, tmp_path)
    bucket = tmp_path / "fake-home" / "studio_sandbox" / tools._LEGACY_SHARED_BUCKET
    bucket.mkdir(parents = True)

    assert tools.resolve_sandbox_workdir("../../../../etc") == str(bucket)
    assert tools.resolve_sandbox_workdir("thread-1") != str(bucket)


def test_a_sandbox_file_named_reveal_is_still_served(tmp_path):
    """The POST route shares the path the download route matches with
    ``{filename:path}``. Being POST-only keeps a file called ``reveal``
    reachable, which widening the method list later would silently undo."""
    from routes.inference import router

    scope = {
        "type": "http",
        "method": "GET",
        "path": "/sandbox/thread-1/reveal",
        "headers": [],
        "root_path": "",
    }
    matched = [r for r in router.routes if r.matches(scope)[0].name == "FULL"]
    assert [r.endpoint.__name__ for r in matched] == ["serve_sandbox_file"]

    scope["method"] = "POST"
    matched = [r for r in router.routes if r.matches(scope)[0].name == "FULL"]
    assert [r.endpoint.__name__ for r in matched] == ["reveal_sandbox_dir"]


def test_the_cached_model_reveal_still_goes_through_the_moved_helper(tmp_path, monkeypatch):
    """The moved helper must still serve its one caller, including symlinked HF cache entries."""
    import asyncio

    from routes import models
    from utils.paths import path_utils

    real = tmp_path / "blobs" / "model.gguf"
    real.parent.mkdir(parents = True)
    real.write_bytes(b"gguf")
    link = tmp_path / "snapshot" / "model.gguf"
    link.parent.mkdir(parents = True)
    link.symlink_to(real)

    opened = []
    monkeypatch.setattr(models, "_resolve_cached_model_path", lambda repo_id, variant: link)
    _shared_setup_20(monkeypatch, opened, path_utils)

    result = asyncio.new_event_loop().run_until_complete(
        models.reveal_cached_model(
            repo_id = "unsloth/Llama-3.2-1B", variant = None, current_subject = "unsloth"
        )
    )
    assert result == {"status": "ok", "path": str(link)}
    assert opened == [link], "a symlinked cache entry must still be revealed"


def test_a_kept_sandbox_is_offered_even_when_deletion_was_asked_for():
    """Undeletable sandboxes come back as kept, and the offer is the only notice and retry."""
    src = Path(__file__).resolve().parents[2] / "frontend/src"
    for hook in (
        "features/chat/hooks/use-chat-sidebar-items.ts",
        "features/chat/hooks/use-chat-projects.ts",
    ):
        text = (src / hook).read_text(encoding = "utf-8")
        assert "offerToDeleteKeptSandboxes(kept)" in text, hook
        assert "!args.deleteFiles) offerToDeleteKeptSandboxes" not in text, hook


async def _noop_async(*args, **kwargs):
    return None


def test_a_read_waits_for_the_move_of_the_tree_it_would_return(tmp_path, monkeypatch):
    """A legacy path handed back mid-move lists nothing and 404s every card in the transcript."""
    import threading

    monkeypatch.setenv("HOME", str(tmp_path / "userprofile"))
    monkeypatch.setenv("USERPROFILE", str(tmp_path / "userprofile"))
    tools = _shared_setup_2(monkeypatch, tmp_path)
    session = "chat-moving-1"
    legacy = Path(tools._legacy_sandbox_root()) / session
    legacy.mkdir(parents = True)
    (legacy / "report.csv").write_text("a,b\n", encoding = "utf-8")

    started = threading.Event()
    resolved = []

    def mover():
        with tools._legacy_lock_for(session):
            started.set()
            time.sleep(0.3)
            shutil.rmtree(legacy)

    thread = threading.Thread(target = mover)
    thread.start()
    started.wait(5)

    def reader():
        resolved.append(tools._legacy_session_dir(session))

    reading = threading.Thread(target = reader)
    reading.start()
    reading.join(10)
    thread.join(10)

    assert resolved == [None], f"a path in the middle of its move was served: {resolved}"


def test_a_detached_tree_that_would_not_delete_is_retried(tmp_path, monkeypatch):
    """A locked tree survives ignore_errors, yet the route already reported those files gone."""
    tools = _shared_setup_2(monkeypatch, tmp_path)
    monkeypatch.setattr(tools, "_DETACHED_RETRY_DELAY", 0.02)

    target = tmp_path / "chat-1.deleting-abcdef12"
    target.mkdir()
    (target / "report.csv").write_text("a,b\n", encoding = "utf-8")

    real_rmtree = shutil.rmtree
    attempts = []

    def flaky(path, **kwargs):
        attempts.append(str(path))
        if len(attempts) == 1:
            return
        real_rmtree(path, **kwargs)

    monkeypatch.setattr(tools.shutil, "rmtree", flaky)
    tools._queue_detached_delete(str(target))

    deadline = time.time() + 10
    while time.time() < deadline and target.exists():
        time.sleep(0.05)
    assert not target.exists(), f"the tree was left behind after {len(attempts)} attempt(s)"
    assert len(attempts) >= 2


def test_a_marker_a_tool_wrote_over_counts_as_the_user_s_file(tmp_path, monkeypatch):
    """Old installs never reserved the name; a marker a tool wrote may hold the user's files."""
    tools = _shared_setup_2(monkeypatch, tmp_path)
    session = "chat-marker-1"
    workdir = Path(tools.get_sandbox_workdir(session))
    assert tools._holds_no_user_files(str(workdir), tools._sandbox_name(session)) is True

    (workdir / tools._SANDBOX_MARKER).write_text("my notes", encoding = "utf-8")
    assert tools._holds_no_user_files(str(workdir), tools._sandbox_name(session)) is False

    assert tools.remove_session_sandbox(session, delete_files = False) is False
    saved = list(workdir.glob(".unsloth_sandbox.saved*"))
    assert saved and saved[0].read_text(encoding = "utf-8") == "my notes"
    assert tools._holds_no_user_files(str(workdir), tools._sandbox_name(session)) is False


def test_clearing_every_chat_builds_the_listed_set_once():
    """Rebuilding the set per chat makes a clear of thousands of threads quadratic."""
    import inspect

    from routes import chat_history

    route = inspect.getsource(chat_history.clear_history)
    assert "listed = set(thread_ids)" in route
    assert "not in set(thread_ids)" not in route


def test_a_call_that_starts_during_the_snapshot_costs_the_card(tmp_path, monkeypatch):
    """A call started mid-snapshot may write files this one names, so the card is dropped."""
    tools = _shared_setup_2(monkeypatch, tmp_path)
    workdir = str(tmp_path / "shared")
    os.makedirs(workdir)
    token = tools._call_started(workdir)
    before = tools._snapshot_workdir_files(workdir)

    real_snapshot = tools._snapshot_workdir_files

    def snapshot_with_a_late_arrival(target):
        tools._call_started(target)
        Path(target, "theirs.csv").write_text("a,b\n", encoding = "utf-8")
        return real_snapshot(target)

    monkeypatch.setattr(tools, "_snapshot_workdir_files", snapshot_with_a_late_arrival)
    sentinels = tools._created_file_sentinels(workdir, before, token = token)
    assert sentinels == "", f"another call's file was put on this card: {sentinels}"


def test_a_chat_called_default_does_not_take_the_anonymous_sandbox(tmp_path, monkeypatch):
    """An API client picks its own thread ids, and that folder holds every
    session-less call's files, which this chat's delete would take."""
    monkeypatch.setenv("HOME", str(tmp_path / "userprofile"))
    monkeypatch.setenv("USERPROFILE", str(tmp_path / "userprofile"))
    tools = _shared_setup_2(monkeypatch, tmp_path)
    anonymous = Path(tools.get_sandbox_workdir(None))
    (anonymous / "scratch.csv").write_text("a,b\n", encoding = "utf-8")

    chat = Path(tools.get_sandbox_workdir("_default"))
    assert chat != anonymous, "the chat is running in the session-less sandbox"
    assert not (chat / "scratch.csv").exists()

    tools.remove_session_sandbox("_default", delete_files = True)
    assert (anonymous / "scratch.csv").is_file()


def test_a_chat_called_default_is_not_served_the_old_shared_folder(tmp_path, monkeypatch):
    """Before the upgrade every call with no session id ran in that directory."""
    monkeypatch.setenv("HOME", str(tmp_path / "userprofile"))
    monkeypatch.setenv("USERPROFILE", str(tmp_path / "userprofile"))
    tools = _shared_setup_2(monkeypatch, tmp_path)
    for name in ("_default", "_invalid"):
        legacy = Path(tools._legacy_sandbox_root()) / name
        legacy.mkdir(parents = True, exist_ok = True)
        (legacy / "somebody-elses.csv").write_text("a,b\n", encoding = "utf-8")
        assert tools._legacy_session_dir(name) is None, name


def test_a_chat_recreated_under_the_same_id_keeps_its_sandbox(tmp_path, monkeypatch):
    """The row goes first, so another tab can upsert the same id before the
    cleanup runs, and that chat may have a tool call in there right now."""
    asyncio, chat_history, tools = _shared_setup_8(monkeypatch, tmp_path)

    _forget_sandbox_state(tools)
    thread_id = "chat-recreated-1"
    workdir = Path(tools.get_sandbox_workdir(thread_id))
    (workdir / "report.csv").write_text("a,b\n", encoding = "utf-8")

    monkeypatch.setattr(chat_history, "get_chat_thread", lambda tid: {"id": tid})
    removed, kept = asyncio.new_event_loop().run_until_complete(
        chat_history._remove_sandboxes([thread_id], True)
    )
    assert removed == 0, "the recreated chat's sandbox was deleted"
    assert (workdir / "report.csv").is_file()


def test_a_listing_follows_a_tree_moved_out_from_under_it(tmp_path, monkeypatch):
    """A rename between resolving and walking the tree would show the chat an empty sandbox."""
    asyncio, inference = _shared_setup_15(monkeypatch, tmp_path)

    moved = tmp_path / "moved-in"
    moved.mkdir()
    (moved / "report.csv").write_text("a,b\n", encoding = "utf-8")
    answers = [str(tmp_path / "gone"), str(moved)]

    monkeypatch.setattr(
        inference,
        "_sandbox_dir_for",
        lambda session_id, create = False: answers.pop(0),
    )
    monkeypatch.setattr(inference, "_authenticate_header_or_query", _noop_async)

    result = asyncio.new_event_loop().run_until_complete(
        inference.list_sandbox_files("thread-1", request = None, token = None, session = None)
    )
    assert [f["name"] for f in result["files"]] == ["report.csv"], result


def test_a_record_never_deletes_what_the_helper_refused(tmp_path, monkeypatch):
    """A stale or edited record must never make rmtree delete a directory the helper refused."""
    studio_db, tools = _shared_setup_4(monkeypatch, tmp_path)
    project_id = "proj16180"
    innocent = tmp_path / "Documents"
    innocent.mkdir()
    (innocent / "thesis.tex").write_text("x", encoding = "utf-8")
    tools.record_orphaned_project(project_id, str(innocent), True, str(innocent))

    refused = []
    real_delete = studio_db.delete_project_workspace
    monkeypatch.setattr(
        studio_db,
        "delete_project_workspace",
        lambda project: refused.append(project) or real_delete({"id": "x", "rootPath": None}),
    )
    monkeypatch.setattr(studio_db, "sandbox_is_referenced_elsewhere", lambda s, e = None: False)
    tools.collect_orphaned_project_workspaces()

    assert refused, "the validated helper was not consulted"
    assert (innocent / "thesis.tex").is_file(), "a refused delete was done anyway"


def test_the_last_fork_going_takes_the_source_chat_s_files(tmp_path, monkeypatch):
    """The source's delete kept files for the fork; only the last fork going cleans them up."""
    asyncio, chat_history, tools = _shared_setup_8(monkeypatch, tmp_path)
    from storage import studio_db

    _forget_sandbox_state(tools)
    source, fork = "chat-source-1", "chat-fork-1"
    workdir = Path(tools.get_sandbox_workdir(source))
    (workdir / "report.csv").write_text("a,b\n", encoding = "utf-8")

    monkeypatch.setattr(chat_history, "get_chat_thread", lambda tid: None)
    monkeypatch.setattr(
        studio_db,
        "sandbox_is_referenced_elsewhere",
        lambda s, e = None: s == source,
    )
    _removed, kept = asyncio.new_event_loop().run_until_complete(
        chat_history._remove_sandboxes([source], True)
    )
    assert kept == [source]
    assert (workdir / "report.csv").is_file()

    monkeypatch.setattr(studio_db, "sandbox_is_referenced_elsewhere", lambda s, e = None: False)
    asyncio.new_event_loop().run_until_complete(chat_history._remove_sandboxes([fork], True))
    assert not workdir.exists(), "the source's files were orphaned for good"


def test_a_chat_called_like_a_project_session_keeps_its_own_sandbox(tmp_path, monkeypatch):
    """A chat id shaped like a project session must not resolve to that project's workspace."""
    studio_db, tools = _shared_setup_4(monkeypatch, tmp_path)
    workspace = tmp_path / "Notes-proj7777"
    (workspace / "sandbox").mkdir(parents = True)
    _shared_setup_10(monkeypatch, studio_db, workspace)
    session = tools.project_session_id("proj7777")
    assert Path(tools.get_sandbox_workdir(session)) == (workspace / "sandbox").resolve()

    _forget_sandbox_state(tools)
    monkeypatch.setattr(studio_db, "get_chat_thread", lambda tid: {"id": tid})
    workdir = Path(tools.get_sandbox_workdir(session))
    assert workdir != (workspace / "sandbox").resolve(), "the chat took the project workspace"
    assert tools.remove_session_sandbox(session, delete_files = True) is True


def test_a_long_project_id_still_reaches_its_workspace(tmp_path, monkeypatch):
    """A long project id overflows a directory name, so the workspace is read from the row."""
    studio_db, tools = _shared_setup_4(monkeypatch, tmp_path)
    project_id = "p" * 60
    workspace = tmp_path / "Long-pppppppp"
    (workspace / "sandbox").mkdir(parents = True)
    _shared_setup_10(monkeypatch, studio_db, workspace)
    session = tools.project_session_id(project_id)
    assert not tools._usable_session_id(session), "the prefixed id fits after all"
    assert Path(tools.get_sandbox_workdir(session)) == (workspace / "sandbox").resolve()


def test_a_project_recreated_under_the_same_id_keeps_its_workspace(tmp_path, monkeypatch):
    """The row goes first, so a project recreated under that id may be writing when files go."""
    monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(tmp_path / "home"))

    from core.inference import tools
    from routes import chat_history
    from storage import studio_db

    _forget_sandbox_state(tools)
    project_id = "proj24242"
    workspace = tmp_path / "Notes-proj2424"
    (workspace / "sandbox").mkdir(parents = True)
    (workspace / "sandbox" / "fresh.csv").write_text("a,b\n", encoding = "utf-8")

    monkeypatch.setattr(chat_history, "get_chat_project", lambda pid: {"id": pid})
    monkeypatch.setattr(studio_db, "sandbox_is_referenced_elsewhere", lambda s, e = None: False)
    _deleted_project(tmp_path, monkeypatch, project_id, workspace)

    assert (workspace / "sandbox" / "fresh.csv").is_file(), "the new project's files went"
    assert tools.list_orphaned_projects() == [], "a live project was recorded as orphaned"


def test_a_download_serves_the_file_it_checked(tmp_path, monkeypatch):
    """A symlink swapped in after the containment check is followed by the download."""
    asyncio, inference = _shared_setup_15(monkeypatch, tmp_path)

    sandbox = tmp_path / "sandbox"
    sandbox.mkdir()
    (sandbox / "report.csv").write_text("a,b\n", encoding = "utf-8")
    secret = tmp_path / "secret.txt"
    secret.write_text("passwords", encoding = "utf-8")

    _shared_setup_12(inference, monkeypatch, sandbox)

    loop = asyncio.new_event_loop()
    response = loop.run_until_complete(
        inference.serve_sandbox_file(
            "thread-1",
            "report.csv",
            request = None,
            token = None,
            session = None,
        )
    )
    (sandbox / "report.csv").unlink()
    (sandbox / "report.csv").symlink_to(secret)

    body = b""

    async def drain():
        nonlocal body
        async for chunk in response.body_iterator:
            body += chunk

    loop.run_until_complete(drain())
    assert body == b"a,b\n", f"the response followed the link: {body!r}"


def test_a_download_refuses_a_file_swapped_for_a_link(tmp_path, monkeypatch):
    """The same swap, made before the open rather than after it."""
    import asyncio

    monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(tmp_path / "home"))

    from fastapi import HTTPException
    from routes import inference

    sandbox = tmp_path / "sandbox"
    sandbox.mkdir()
    secret = tmp_path / "secret.txt"
    secret.write_text("passwords", encoding = "utf-8")
    (sandbox / "report.csv").symlink_to(secret)

    _shared_setup_12(inference, monkeypatch, sandbox)

    with pytest.raises(HTTPException) as raised:
        asyncio.new_event_loop().run_until_complete(
            inference.serve_sandbox_file(
                "thread-1",
                "report.csv",
                request = None,
                token = None,
                session = None,
            )
        )
    assert raised.value.status_code in (403, 404)


def test_a_linked_sandbox_root_is_not_ours_to_delete_from(tmp_path, monkeypatch):
    """`<studio home>/sandbox` pointing into the user's own folder makes every
    directory under it theirs, whatever the environment says."""
    monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(tmp_path / "home"))
    monkeypatch.delenv("UNSLOTH_STUDIO_SANDBOX_HOME", raising = False)

    from core.inference import tools

    _forget_sandbox_state(tools)
    theirs = tmp_path / "Documents"
    (theirs / "chat-linked-1").mkdir(parents = True)
    (theirs / "chat-linked-1" / "thesis.tex").write_text("x", encoding = "utf-8")
    root = Path(tools.sandbox_root())
    if root.exists():
        shutil.rmtree(root)
    root.parent.mkdir(parents = True, exist_ok = True)
    root.symlink_to(theirs)

    assert tools._root_is_ours() is False
    workdir = Path(tools.get_sandbox_workdir("chat-linked-1"))
    assert not (workdir / "thesis.tex").exists(), "the chat ran in the user's directory"
    tools.remove_session_sandbox("chat-linked-1", delete_files = True)
    assert (theirs / "chat-linked-1" / "thesis.tex").is_file(), "deleted the user's files"


def test_a_collection_failure_names_the_record_it_was_on(tmp_path, monkeypatch):
    """An unbound loop name in the handler would turn a finished delete into a 500."""
    studio_db, tools = _shared_setup_4(monkeypatch, tmp_path)
    workspace = tmp_path / "Notes-projfail"
    (workspace / "sandbox").mkdir(parents = True)
    tools.record_orphaned_project("projfail1", str(workspace / "sandbox"), True, str(workspace))

    def boom(*args, **kwargs):
        raise RuntimeError("database is away")

    monkeypatch.setattr(studio_db, "sandbox_is_referenced_elsewhere", boom)
    tools.collect_orphaned_project_workspaces()
    assert workspace.is_dir()


def test_a_project_created_again_keeps_the_recorded_workspace(tmp_path, monkeypatch):
    """The collection runs minutes later, and the id is the client's to reuse."""
    studio_db, tools = _shared_setup_4(monkeypatch, tmp_path)
    project_id = "projagain"
    workspace = tmp_path / "Notes-projagai"
    (workspace / "sandbox").mkdir(parents = True)
    (workspace / "fresh.csv").write_text("a,b\n", encoding = "utf-8")
    tools.record_orphaned_project(project_id, str(workspace / "sandbox"), True, str(workspace))

    monkeypatch.setattr(studio_db, "sandbox_is_referenced_elsewhere", lambda s, e = None: False)
    monkeypatch.setattr(
        studio_db,
        "get_chat_project",
        lambda pid: {
            "id": pid,
            "rootPath": str(workspace),
            "sandboxPath": str(workspace / "sandbox"),
        },
    )
    tools.collect_orphaned_project_workspaces()
    assert (workspace / "fresh.csv").is_file(), "the new project's files went"

    monkeypatch.setattr(studio_db, "get_chat_project", lambda pid: None)
    tools.collect_orphaned_project_workspaces()
    assert not workspace.exists()


def test_a_chat_recreated_while_its_tool_ran_keeps_its_files(tmp_path, monkeypatch):
    """A delete queued behind a running call can fire after its id was reused by another chat."""
    studio_db, tools = _shared_setup_4(monkeypatch, tmp_path)
    session = "chat-requeued-1"
    workdir = Path(tools.get_sandbox_workdir(session))
    (workdir / "report.csv").write_text("a,b\n", encoding = "utf-8")

    monkeypatch.setattr(studio_db, "get_chat_thread", lambda tid: None)
    with tools._session_in_flight(session):
        assert tools.remove_session_sandbox(session, delete_files = True) is False
        assert tools.sandbox_removal_deferred(session)
        monkeypatch.setattr(studio_db, "get_chat_thread", lambda tid: {"id": tid})

    assert (workdir / "report.csv").is_file(), "the recreated chat's files went"


def test_a_file_outside_the_hash_budget_is_not_reported_as_written(tmp_path, monkeypatch):
    """Hashing has a byte budget, so an earlier write can push an untouched file out of it."""
    tools = _shared_setup_2(monkeypatch, tmp_path)
    monkeypatch.setattr(tools, "_volume_timestamps_finely", lambda _: False)
    workdir = tmp_path / "sandbox"
    workdir.mkdir()
    untouched = workdir / "z-report.csv"
    untouched.write_bytes(b"x" * 32)

    before = tools._snapshot_workdir_files(str(workdir))
    assert before["z-report.csv"][2] is not None, "the file was not hashed to begin with"

    (workdir / "a-new.bin").write_bytes(b"y" * 64)
    monkeypatch.setattr(tools, "_MAX_SNAPSHOT_HASH_BYTES", 0)
    sentinels = tools._created_file_sentinels(str(workdir), before)
    assert "a-new.bin" in sentinels
    assert "z-report.csv" not in sentinels, sentinels


def test_the_file_download_button_refreshes_the_session_first():
    """The URL carries the bearer, so an expired token would save a 401 body as the file."""
    src = Path(__file__).resolve().parents[2] / "frontend/src"
    view = (src / "components/assistant-ui/sandbox-files-view.tsx").read_text(encoding = "utf-8")
    assert 'authFetch(apiUrl(path), { method: "HEAD" })' in view
    assert view.index("authFetch(apiUrl(path)") < view.index("const token = getAuthToken()")


def test_the_download_route_answers_the_probe_the_button_sends(tmp_path, monkeypatch):
    """The download route must answer HEAD: FastAPI does not add it to a GET route."""
    asyncio, inference = _shared_setup_15(monkeypatch, tmp_path)

    methods = {
        frozenset(r.methods)
        for r in inference.router.routes
        if getattr(r, "path", "") == "/sandbox/{session_id}/{filename:path}"
    }
    assert methods == {frozenset({"GET", "HEAD"})}, methods

    sandbox = tmp_path / "sandbox"
    sandbox.mkdir()
    (sandbox / "report.csv").write_text("a,b\n", encoding = "utf-8")
    _shared_setup_12(inference, monkeypatch, sandbox)

    class _Head:
        method = "HEAD"

    response = asyncio.new_event_loop().run_until_complete(
        inference.serve_sandbox_file(
            "thread-1",
            "report.csv",
            request = _Head(),
            token = None,
            session = None,
        )
    )
    assert response.status_code == 200
    assert response.headers["content-length"] == "4"
    assert response.body == b"", "the file was read to answer a HEAD"


def test_a_project_id_a_filename_cannot_hold_is_still_recorded(tmp_path, monkeypatch):
    """Project ids are client-chosen; refusing an id a filename cannot hold loses the deferred delete."""
    tools = _shared_setup_2(monkeypatch, tmp_path)
    project_id = "Ünsloth/Notes " + "x" * 80
    workspace = tmp_path / "Notes-project"
    (workspace / "sandbox").mkdir(parents = True)
    tools.record_orphaned_project(project_id, str(workspace / "sandbox"), True, str(workspace))

    records = tools.list_orphaned_projects()
    assert [(r[0], r[3]) for r in records] == [(project_id, True)], records
    assert tools._recorded_project_workdir(project_id) == str((workspace / "sandbox").resolve())

    tools.forget_orphaned_project(project_id)
    assert tools.list_orphaned_projects() == []


def test_a_chat_and_a_project_with_one_id_keep_their_own_records(tmp_path, monkeypatch):
    """They are different tables, so the same client-supplied id can name both,
    and one record overwriting the other stranded a folder."""
    tools = _shared_setup_2(monkeypatch, tmp_path)
    shared_id = "notes1234"
    workspace = tmp_path / "Notes-notes123"
    (workspace / "sandbox").mkdir(parents = True)
    chat_dir = Path(tools.get_sandbox_workdir(shared_id))
    (chat_dir / "chat.csv").write_text("a,b\n", encoding = "utf-8")

    tools.record_orphaned_project(shared_id, str(workspace / "sandbox"), True, str(workspace))
    tools.record_kept_sandbox(shared_id)

    records = sorted(tools.list_orphaned_projects(), key = lambda r: r[4])
    assert [r[0] for r in records] == [shared_id, shared_id]
    assert [r[4] for r in records] == [False, True], "one record overwrote the other"
    assert records[0][1] == str((workspace / "sandbox").resolve())
    assert records[1][1] == str(chat_dir.resolve())
    assert tools._recorded_project_workdir(shared_id) == str((workspace / "sandbox").resolve())


def test_a_kept_workspace_is_found_past_a_crowd_of_records(tmp_path, monkeypatch):
    """Listing must not stop early, or a busy install's newest records hide its kept folders."""
    tools = _shared_setup_2(monkeypatch, tmp_path)
    crowd = tmp_path / "crowd"
    crowd.mkdir()
    for n in range(50):
        tools.record_orphaned_project(f"aaaa{n:04d}", str(crowd), False, None)

    project_id = "zzzz9999"
    workspace = tmp_path / "Notes-zzzz9999"
    (workspace / "sandbox").mkdir(parents = True)
    tools.record_orphaned_project(project_id, str(workspace / "sandbox"), True, str(workspace))

    monkeypatch.setattr(tools, "_MAX_ORPHAN_RECORDS", 4)
    assert tools._recorded_project_workdir(project_id) == str((workspace / "sandbox").resolve())


def test_a_project_created_during_the_record_write_keeps_its_files(tmp_path, monkeypatch):
    """A project created during the record write resolves to the same default workspace path."""
    monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(tmp_path / "home"))

    from core.inference import tools
    from routes import chat_history
    from storage import studio_db

    _forget_sandbox_state(tools)
    project_id = "proj13131"
    workspace = tmp_path / "Notes-proj1313"
    (workspace / "sandbox").mkdir(parents = True)
    (workspace / "sandbox" / "fresh.csv").write_text("a,b\n", encoding = "utf-8")

    # Created once the delete has looked and never removed again, like a real registry.
    looked = {"once": False}

    def created_after_the_first_look(pid):
        if not looked["once"]:
            looked["once"] = True
            return None
        return {"id": pid}

    monkeypatch.setattr(chat_history, "get_chat_project", created_after_the_first_look)
    monkeypatch.setattr(
        studio_db,
        "get_chat_project",
        lambda pid: {
            "id": pid,
            "rootPath": str(workspace),
            "sandboxPath": str(workspace / "sandbox"),
        },
    )
    monkeypatch.setattr(studio_db, "sandbox_is_referenced_elsewhere", lambda s, e = None: False)
    _deleted_project(tmp_path, monkeypatch, project_id, workspace)

    assert (workspace / "sandbox" / "fresh.csv").is_file(), "the new project's files went"
    assert tools.list_orphaned_projects() == [], "a live project was left recorded"


def test_a_project_created_inside_the_record_write_itself_keeps_its_files(tmp_path, monkeypatch):
    """A project created during record_orphaned_project's write keeps its files via the final recheck."""
    monkeypatch.setenv("UNSLOTH_STUDIO_HOME", str(tmp_path / "home"))

    from core.inference import tools
    from routes import chat_history
    from storage import studio_db

    _forget_sandbox_state(tools)
    project_id = "proj14141"
    workspace = tmp_path / "Notes-proj1414"
    (workspace / "sandbox").mkdir(parents = True)
    (workspace / "sandbox" / "fresh.csv").write_text("a,b\n", encoding = "utf-8")

    recreated = {"yet": False}
    real_record = tools.record_orphaned_project

    def record_then_recreate(*args, **kwargs):
        result = real_record(*args, **kwargs)
        recreated["yet"] = True
        return result

    monkeypatch.setattr(tools, "record_orphaned_project", record_then_recreate)
    monkeypatch.setattr(
        chat_history,
        "get_chat_project",
        lambda pid: {"id": pid} if recreated["yet"] else None,
    )
    monkeypatch.setattr(
        studio_db,
        "get_chat_project",
        lambda pid: {
            "id": pid,
            "rootPath": str(workspace),
            "sandboxPath": str(workspace / "sandbox"),
        },
    )
    monkeypatch.setattr(studio_db, "sandbox_is_referenced_elsewhere", lambda s, e = None: False)
    _deleted_project(tmp_path, monkeypatch, project_id, workspace)

    assert recreated["yet"], "the record was never written, so this proves nothing"
    assert (workspace / "sandbox" / "fresh.csv").is_file(), "the new project's files went"
    assert tools.list_orphaned_projects() == [], "a live project was left recorded"


def test_a_kept_workspace_resolves_for_any_project_id(tmp_path, monkeypatch):
    """Records are keyed by digest, so any id works; only the guessed directory name matters."""
    studio_db, tools = _shared_setup_4(monkeypatch, tmp_path)
    project_id = "Ünsloth/Notes " + "y" * 80
    workspace = tmp_path / "Notes-kept"
    (workspace / "sandbox").mkdir(parents = True)
    (workspace / "sandbox" / "report.csv").write_text("a,b\n", encoding = "utf-8")
    tools.record_orphaned_project(project_id, str(workspace / "sandbox"), False, str(workspace))

    monkeypatch.setattr(studio_db, "ensure_chat_project_workspace", lambda pid: None)
    served = Path(tools.resolve_sandbox_workdir(tools.project_session_id(project_id)))
    assert served == (workspace / "sandbox").resolve(), served
    assert (served / "report.csv").is_file()


def test_a_marker_rewritten_with_another_name_keeps_the_chat_s_files(tmp_path, monkeypatch):
    """The marker is tool-writable, so a valid-looking name in it made the files unreachable."""
    tools = _shared_setup_2(monkeypatch, tmp_path)
    session = "chat-marker-2"
    workdir = Path(tools.get_sandbox_workdir(session))
    (workdir / "report.csv").write_text("a,b\n", encoding = "utf-8")
    (workdir / tools._SANDBOX_MARKER).write_text("someone-else", encoding = "utf-8")

    assert Path(tools.resolve_sandbox_workdir(session)) == workdir, "the files were stranded"
    assert tools.session_sandbox_has_files(session) is True
    assert tools.remove_session_sandbox(session, delete_files = True) is True
    assert not workdir.exists()


def test_a_sandbox_lookup_lists_the_root_once(tmp_path, monkeypatch):
    """Listing the root per candidate name made one lookup 33 walks; the root is listed once."""
    root, tools = _shared_setup_5(monkeypatch, tmp_path)
    scans = []
    real_listdir = os.listdir

    def counting_listdir(path):
        scans.append(str(path))
        return real_listdir(path)

    monkeypatch.setattr(tools.os, "listdir", counting_listdir)
    assert tools._marked_sandbox_in(str(root), "chat-scan-1") is None
    assert len([s for s in scans if s == str(root)]) == 1, scans


def test_a_deferred_delete_keeps_the_files_when_the_check_cannot_be_made(tmp_path, monkeypatch):
    """If the row check cannot be made, a queued delete keeps the files."""
    studio_db, tools = _shared_setup_4(monkeypatch, tmp_path)
    session = "chat-unknown-1"
    workdir = Path(tools.get_sandbox_workdir(session))
    (workdir / "report.csv").write_text("a,b\n", encoding = "utf-8")

    def broken(thread_id):
        raise RuntimeError("database is away")

    monkeypatch.setattr(studio_db, "get_chat_thread", broken)
    with tools._session_in_flight(session):
        assert tools.remove_session_sandbox(session, delete_files = True) is False

    assert (workdir / "report.csv").is_file(), "files went on an unanswerable check"


def test_only_a_sandbox_tool_s_result_is_unwrapped_for_replay():
    """A custom tool answering with text, sessionId and images is someone
    else's result, and replaying only its text drops the rest."""
    src = Path(__file__).resolve().parents[2] / "frontend/src"
    adapter = (src / "features/chat/api/chat-adapter.ts").read_text(encoding = "utf-8")
    assert "function isSandboxWrapper(" in adapter
    assert "): result is { text: string; sessionId: string } {" in adapter
    assert "SANDBOX_FILE_TOOLS.has(toolName)" in adapter
    assert 'isSandboxWrapper(result, tc.toolName ?? "")' in adapter
    dialog = (src / "features/chat/prompt-storage/prompt-storage-dialog.tsx").read_text(
        encoding = "utf-8"
    )
    assert 'typeof p.toolName === "string" ? p.toolName : undefined,' in dialog
    assert "toolResultModelText(p.result, name)" in dialog


def test_a_project_remade_somewhere_else_does_not_strand_the_old_workspace(tmp_path, monkeypatch):
    """The default root carries the project's name, so a remade project may sit elsewhere."""
    studio_db, tools = _shared_setup_4(monkeypatch, tmp_path)
    project_id = "projmoved"
    old = tmp_path / "Notes-projmove"
    (old / "sandbox").mkdir(parents = True)
    (old / "report.csv").write_text("a,b\n", encoding = "utf-8")
    new = tmp_path / "Renamed-projmove"
    (new / "sandbox").mkdir(parents = True)
    tools.record_orphaned_project(project_id, str(old / "sandbox"), True, str(old))

    monkeypatch.setattr(studio_db, "sandbox_is_referenced_elsewhere", lambda s, e = None: False)
    monkeypatch.setattr(
        studio_db,
        "get_chat_project",
        lambda pid: {"id": pid, "rootPath": str(new), "sandboxPath": str(new / "sandbox")},
    )
    assert tools.live_project_owns(project_id, str(old / "sandbox"), str(old)) is False
    assert tools.live_project_owns(project_id, str(new / "sandbox"), str(new)) is True

    tools.collect_orphaned_project_workspaces()
    assert not old.exists(), "the old workspace was stranded"
    assert new.is_dir(), "the live project's workspace went"


def test_a_chat_named_like_a_project_session_still_loses_its_files(tmp_path, monkeypatch):
    """Its row goes first, so the id reads as a project's and the chat's folder stays."""
    studio_db, tools = _shared_setup_4(monkeypatch, tmp_path)
    workspace = tmp_path / "Shared-foo12345"
    (workspace / "sandbox").mkdir(parents = True)
    (workspace / "sandbox" / "shared.csv").write_text("a,b\n", encoding = "utf-8")
    _shared_setup_10(monkeypatch, studio_db, workspace)

    session = tools.project_session_id("foo12345")
    monkeypatch.setattr(studio_db, "get_chat_thread", lambda tid: {"id": tid})
    workdir = Path(tools.get_sandbox_workdir(session))
    (workdir / "mine.csv").write_text("a,b\n", encoding = "utf-8")
    assert workdir != (workspace / "sandbox").resolve()

    monkeypatch.setattr(studio_db, "get_chat_thread", lambda tid: None)
    assert tools.remove_session_sandbox(session, delete_files = True) is True
    assert not workdir.exists(), "the chat's own files were left behind"
    assert (workspace / "sandbox" / "shared.csv").is_file(), "the project's files went"


def test_a_download_sends_no_more_than_it_promised(tmp_path, monkeypatch):
    """Appends after the length is captured can make the body exceed Content-Length."""
    asyncio, inference = _shared_setup_15(monkeypatch, tmp_path)

    sandbox = tmp_path / "sandbox"
    sandbox.mkdir()
    (sandbox / "report.csv").write_text("a,b\n", encoding = "utf-8")
    _shared_setup_12(inference, monkeypatch, sandbox)

    loop = asyncio.new_event_loop()
    response = loop.run_until_complete(
        inference.serve_sandbox_file(
            "thread-1",
            "report.csv",
            request = None,
            token = None,
            session = None,
        )
    )
    declared = int(response.headers["content-length"])
    with open(sandbox / "report.csv", "a", encoding = "utf-8") as fh:
        fh.write("c,d\ne,f\n")

    body = b""

    async def drain():
        nonlocal body
        async for chunk in response.body_iterator:
            body += chunk

    loop.run_until_complete(drain())
    assert len(body) == declared, f"sent {len(body)} bytes for a declared {declared}"


def test_an_interrupted_delete_is_finished_when_studio_starts(tmp_path, monkeypatch):
    """It waited for the next Python or terminal call, and ordinary chat, a
    listing and a download never make one."""
    import inspect

    tools = _shared_setup_2(monkeypatch, tmp_path)
    tools._swept_detached = False
    root = Path(tools.sandbox_root())
    root.mkdir(parents = True, exist_ok = True)
    stranded = root / "chat-killed-1.deleting-abcdef12"
    stranded.mkdir()
    (stranded / "report.csv").write_text("a,b\n", encoding = "utf-8")

    thread = tools.start_sandbox_recovery()
    if thread is not None:
        thread.join(timeout = 10)
    assert not stranded.exists(), "the tree a killed run left was kept"

    import main

    assert "start_sandbox_recovery()" in inspect.getsource(main.lifespan)


def test_a_retry_that_still_keeps_the_files_offers_again():
    """The request succeeds and the files stay: a tool is still running, a fork
    still shows them, or the folder would not go."""
    src = Path(__file__).resolve().parents[2] / "frontend/src"
    offer = (src / "features/chat/utils/offer-kept-sandbox-files.ts").read_text(encoding = "utf-8")
    assert "if (stillKept.length > 0) offerToDeleteKeptSandboxes(stillKept);" in offer


def test_a_finely_timestamped_volume_is_not_read_twice_per_call(tmp_path, monkeypatch):
    """Hashing was most of a snapshot; where mtime separates the writes, nothing is read."""
    tools = _shared_setup_1(monkeypatch, tmp_path)
    workdir = Path(tools.get_sandbox_workdir("__LOCALID_fine1"))
    if not tools._volume_timestamps_finely(str(workdir)):
        pytest.skip("this filesystem records whole seconds, where digests are the point")
    (workdir / "report.csv").write_text("a,b\n", encoding = "utf-8")

    read = []
    real_key = tools._content_key
    monkeypatch.setattr(
        tools,
        "_content_key",
        lambda path, size: (read.append(path), real_key(path, size))[1],
    )
    snapshot = tools._snapshot_workdir_files(str(workdir))

    assert snapshot["report.csv"][2] is None, "the file was hashed anyway"
    assert not read, f"the snapshot read {read}"


def test_one_whole_second_stamp_is_not_taken_for_a_coarse_volume(tmp_path, monkeypatch):
    """One whole-second stamp is chance, not a coarse volume; mtime alone would pin hashing."""
    tools = _shared_setup_1(monkeypatch, tmp_path)
    workdir = Path(tools.get_sandbox_workdir("__LOCALID_stamp1"))
    if not tools._volume_timestamps_finely(str(workdir)):
        pytest.skip("this filesystem records whole seconds")
    tools._fine_mtime_devices.clear()
    real_stat = os.stat
    monkeypatch.setattr(
        os,
        "stat",
        lambda p, *a, **k: (
            _WholeSecondMtime(real_stat(p, *a, **k))
            if str(p) == str(workdir)
            else real_stat(p, *a, **k)
        ),
    )
    assert tools._volume_timestamps_finely(str(workdir)), "one whole second was enough"


class _WholeSecondMtime:
    """A stat result whose mtime lost its sub-second part, and nothing else."""

    def __init__(self, stat):
        self._stat = stat

    st_mtime_ns = property(lambda self: self._stat.st_mtime_ns // 10**9 * 10**9)

    def __getattr__(self, name):
        return getattr(self._stat, name)


def test_session_in_flight_does_not_swallow_tool_exceptions(tmp_path, monkeypatch):
    """A tool that raises must surface, not be reported as an unknown tool."""
    monkeypatch.setenv("UNSLOTH_STUDIO_SANDBOX_HOME", str(tmp_path / "sb"))

    from core.inference import tools

    session = "__LOCALID_raises1"
    with pytest.raises(ValueError, match = "boom"):
        with tools._session_in_flight(session):
            raise ValueError("boom")

    assert tools._session_key(session) not in tools._active_sessions


def test_execute_tool_reports_a_bad_arg_instead_of_unknown_tool(tmp_path, monkeypatch):
    monkeypatch.setenv("UNSLOTH_STUDIO_SANDBOX_HOME", str(tmp_path / "sb"))

    from core.inference import tools
    with pytest.raises(AttributeError):
        tools.execute_tool("python", {"code": 42}, session_id = "__LOCALID_badarg1")


def _sandbox_route_setup(tmp_path, monkeypatch):
    from routes import inference

    sandbox = tmp_path / "sandbox" / "thread-1"
    (sandbox / "outputs").mkdir(parents = True)
    monkeypatch.setattr(
        inference, "_sandbox_dir_for", lambda session_id, create = False: os.path.realpath(sandbox)
    )
    monkeypatch.setattr(inference, "_authenticate_header_or_query", _noop_async)
    monkeypatch.setattr("utils.paths.storage_roots.cache_root", lambda: tmp_path / "cache")
    launched = []
    monkeypatch.setattr(subprocess, "Popen", lambda cmd, **kw: launched.append(cmd))
    # Windows opens with os.startfile: recorded too, so a test never launches a real app.
    monkeypatch.setattr(os, "startfile", lambda path: launched.append([path]), raising = False)
    return inference, sandbox, launched


def _open(inference, name):
    import asyncio
    return asyncio.run(
        inference.open_sandbox_file("thread-1", request = None, file = name, token = None, session = None)
    )


def test_opening_a_sandbox_document_hands_it_to_the_default_app(tmp_path, monkeypatch):
    inference, sandbox, launched = _sandbox_route_setup(tmp_path, monkeypatch)
    (sandbox / "outputs" / "report.pdf").write_bytes(b"%PDF-1.4")
    assert _open(inference, "outputs/report.pdf") == {"status": "ok"}
    (opened,) = [cmd[-1] for cmd in launched]
    assert not opened.startswith(os.path.realpath(sandbox))
    assert os.path.basename(opened) == "report.pdf"
    assert Path(opened).read_bytes() == b"%PDF-1.4"


def test_opening_a_sandbox_file_keeps_the_chat_able_to_run_tools(tmp_path, monkeypatch):
    from core.inference import os_sandbox

    inference, sandbox, launched = _sandbox_route_setup(tmp_path, monkeypatch)
    (sandbox / "chart.png").write_bytes(b"\x89PNG")
    assert _open(inference, "chart.png") == {"status": "ok"}
    assert len(launched) == 1
    assert os_sandbox.scan_workdir_for_host_channels(str(sandbox)) == ()


def _stage_like_an_older_build(
    tmp_path,
    sandbox,
    name = "chart.png",
):
    linked = tmp_path / "cache" / "open-staging" / "linked"
    linked.mkdir(parents = True)
    (sandbox / name).write_bytes(b"\x89PNG")
    os.link(sandbox / name, linked / name)
    return linked


def test_opening_a_file_clears_old_staged_files(tmp_path, monkeypatch):
    inference, sandbox, launched = _sandbox_route_setup(tmp_path, monkeypatch)
    staging = tmp_path / "cache" / "open-staging"
    old, recent = staging / "old", staging / "recent"
    for entry in (old, recent):
        entry.mkdir(parents = True)
        (entry / "notes.txt").write_text("x", encoding = "utf-8")
    day_ago = time.time() - 25 * 60 * 60
    os.utime(old, (day_ago, day_ago))
    (sandbox / "report.pdf").write_bytes(b"%PDF-1.4")
    assert _open(inference, "report.pdf") == {"status": "ok"}
    assert not old.exists()
    assert recent.exists()


# The longest name a filesystem allows, so the swap cannot lengthen it.
@pytest.mark.parametrize("name", ["chart.png", "r" * 251 + ".pdf"])
def test_startup_clears_a_hard_link_an_older_build_staged(tmp_path, monkeypatch, name):
    import inspect

    from core.inference import os_sandbox
    from utils.paths import path_utils

    monkeypatch.setattr("utils.paths.storage_roots.cache_root", lambda: tmp_path / "cache")
    sandbox = tmp_path / "sandbox" / "thread-1"
    sandbox.mkdir(parents = True)
    linked = _stage_like_an_older_build(tmp_path, sandbox, name)
    path_utils.prune_open_staging()
    assert os.listdir(linked) == []
    assert os.stat(sandbox / name).st_nlink == 1
    assert (sandbox / name).read_bytes() == b"\x89PNG"
    assert os_sandbox.scan_workdir_for_host_channels(str(sandbox)) == ()

    import main

    source = inspect.getsource(main.lifespan)
    assert "prune_open_staging()" in source
    assert "threading.Thread(" in source and "target = _prune_open_staging_in_background" in source


def test_an_app_saving_while_a_legacy_link_is_replaced_keeps_its_save(tmp_path, monkeypatch):
    from core.inference import os_sandbox
    from utils.paths import path_utils

    monkeypatch.setattr("utils.paths.storage_roots.cache_root", lambda: tmp_path / "cache")
    sandbox = tmp_path / "sandbox" / "thread-1"
    sandbox.mkdir(parents = True)
    linked = _stage_like_an_older_build(tmp_path, sandbox)
    real_mkstemp = tempfile.mkstemp

    def app_saves_meanwhile(*args, **kwargs):
        saved = linked / "chart.png.saving"
        saved.write_bytes(b"edited in the app")
        os.replace(saved, linked / "chart.png")
        return real_mkstemp(*args, **kwargs)

    monkeypatch.setattr(tempfile, "mkstemp", app_saves_meanwhile)
    path_utils.prune_open_staging()
    assert (linked / "chart.png").read_bytes() == b"edited in the app"
    assert os.listdir(linked) == ["chart.png"]
    assert os_sandbox.scan_workdir_for_host_channels(str(sandbox)) == ()


def test_a_sibling_backend_breaking_legacy_links_is_not_raced(tmp_path, monkeypatch):
    from utils.cache_cleanup import _try_lock, _unlock
    from utils.paths import path_utils

    monkeypatch.setattr("utils.paths.storage_roots.cache_root", lambda: tmp_path / "cache")
    sandbox = tmp_path / "sandbox" / "thread-1"
    sandbox.mkdir(parents = True)
    _stage_like_an_older_build(tmp_path, sandbox)
    sibling = os.open(tmp_path / "cache" / "open-staging.lock", os.O_CREAT | os.O_RDWR, 0o600)
    _try_lock(sibling)
    path_utils.prune_open_staging()
    assert os.stat(sandbox / "chart.png").st_nlink == 2
    _unlock(sibling)
    path_utils.prune_open_staging()
    assert os.stat(sandbox / "chart.png").st_nlink == 1


def test_a_link_an_older_sibling_stages_after_startup_does_not_block_tools(tmp_path, monkeypatch):
    from core.inference import os_sandbox
    from utils.paths import path_utils

    monkeypatch.setattr("utils.paths.storage_roots.cache_root", lambda: tmp_path / "cache")
    sandbox = tmp_path / "sandbox" / "thread-1"
    sandbox.mkdir(parents = True)
    path_utils.prune_open_staging()
    linked = _stage_like_an_older_build(tmp_path, sandbox)
    assert os_sandbox.scan_workdir_for_host_channels(str(sandbox)) == ()
    assert os.listdir(linked) == []

    (tmp_path / "elsewhere").mkdir()
    os.link(sandbox / "chart.png", tmp_path / "elsewhere" / "chart.png")
    with pytest.raises(os_sandbox.WorkdirUnsafeError, match = "hard-linked from outside"):
        os_sandbox.scan_workdir_for_host_channels(str(sandbox))


def test_a_rescan_that_runs_out_of_time_keeps_the_hard_link_refusal(tmp_path, monkeypatch):
    from core.inference import os_sandbox

    monkeypatch.setattr("utils.paths.storage_roots.cache_root", lambda: tmp_path / "cache")
    sandbox = tmp_path / "sandbox" / "thread-1"
    sandbox.mkdir(parents = True)
    (sandbox / "chart.png").write_bytes(b"\x89PNG")
    (tmp_path / "elsewhere").mkdir()
    os.link(sandbox / "chart.png", tmp_path / "elsewhere" / "chart.png")
    real_scan = os_sandbox._hazard_within_wall_clock
    scans = []

    def second_scan_overruns(*args, **kwargs):
        scans.append(args)
        if len(scans) > 1:
            raise os_sandbox._ScanBudgetExceeded("over budget")
        return real_scan(*args, **kwargs)

    monkeypatch.setattr(os_sandbox, "_hazard_within_wall_clock", second_scan_overruns)
    with pytest.raises(os_sandbox.WorkdirUnsafeError, match = "hard-linked from outside"):
        os_sandbox.scan_workdir_for_host_channels(str(sandbox))
    assert len(scans) == 2


@pytest.mark.skipif(os.name == "nt", reason = "symlinked directories need privileges on Windows")
def test_a_symlinked_staging_entry_is_not_walked(tmp_path, monkeypatch):
    from utils.paths import path_utils

    monkeypatch.setattr("utils.paths.storage_roots.cache_root", lambda: tmp_path / "cache")
    outside = tmp_path / "outside"
    outside.mkdir()
    (outside / "a.txt").write_bytes(b"shared")
    os.link(outside / "a.txt", outside / "b.txt")
    (tmp_path / "cache" / "open-staging").mkdir(parents = True)
    (tmp_path / "cache" / "open-staging" / "redirect").symlink_to(outside, target_is_directory = True)
    path_utils.prune_open_staging()
    assert os.stat(outside / "a.txt").st_ino == os.stat(outside / "b.txt").st_ino
    assert sorted(os.listdir(outside)) == ["a.txt", "b.txt"]


@pytest.mark.skipif(os.name == "nt", reason = "symlinked directories need privileges on Windows")
def test_a_symlinked_staging_root_is_left_alone(tmp_path, monkeypatch):
    from utils.paths import path_utils

    monkeypatch.setattr("utils.paths.storage_roots.cache_root", lambda: tmp_path / "cache")
    elsewhere = tmp_path / "elsewhere"
    recent, old = elsewhere / "recent", elsewhere / "old"
    for folder in (recent, old):
        folder.mkdir(parents = True)
    (recent / "a.txt").write_bytes(b"shared")
    os.link(recent / "a.txt", recent / "b.txt")
    day_ago = time.time() - 25 * 60 * 60
    os.utime(old, (day_ago, day_ago))
    (tmp_path / "cache").mkdir()
    (tmp_path / "cache" / "open-staging").symlink_to(elsewhere, target_is_directory = True)
    path_utils.prune_open_staging()
    path_utils._prune_open_staging(tmp_path / "cache" / "open-staging")
    assert sorted(os.listdir(recent)) == ["a.txt", "b.txt"]
    assert old.exists()


def test_legacy_links_wait_when_the_staging_lock_cannot_be_taken(tmp_path, monkeypatch):
    from utils import cache_cleanup
    from utils.paths import path_utils

    monkeypatch.setattr("utils.paths.storage_roots.cache_root", lambda: tmp_path / "cache")
    sandbox = tmp_path / "sandbox" / "thread-1"
    sandbox.mkdir(parents = True)
    _stage_like_an_older_build(tmp_path, sandbox)

    def no_locks_here(fd):
        raise OSError(errno.ENOSYS, "locking not supported")

    monkeypatch.setattr(cache_cleanup, "_try_lock", no_locks_here)
    path_utils.prune_open_staging()
    assert os.stat(sandbox / "chart.png").st_nlink == 2


def test_a_failed_copy_leaves_nothing_staged(tmp_path, monkeypatch):
    from utils.paths import path_utils

    monkeypatch.setattr("utils.paths.storage_roots.cache_root", lambda: tmp_path / "cache")
    report = tmp_path / "sandbox" / "report.pdf"
    report.parent.mkdir()
    report.write_bytes(b"%PDF-1.4")

    class DiskFull(io.BytesIO):
        def read(self, *args):
            raise OSError(28, "No space left on device")

    def fdopen_then_fail(fd, *args, **kwargs):
        os.close(fd)
        return DiskFull()

    monkeypatch.setattr(os, "fdopen", fdopen_then_fail)
    with pytest.raises(OSError):
        path_utils._stage_for_open(report, report.parent)
    assert list((tmp_path / "cache" / "open-staging").iterdir()) == []


@pytest.mark.parametrize(
    "name, status",
    [
        ("run.sh", 415),
        ("run.command", 415),
        ("page.html", 415),
        ("Tool.app", 415),
        ("link.pdf", 403),
        ("../../secret.pdf", 404),
        ("missing.pdf", 404),
        ("outputs", 404),
    ],
)
def test_opening_refuses_scripts_links_and_escapes(tmp_path, monkeypatch, name, status):
    from fastapi import HTTPException

    inference, sandbox, launched = _sandbox_route_setup(tmp_path, monkeypatch)
    outside = tmp_path / "secret.pdf"
    outside.write_bytes(b"%PDF-1.4")
    (sandbox / "link.pdf").symlink_to(outside)
    for script in ("run.sh", "run.command", "page.html", "Tool.app"):
        (sandbox / script).write_text("x", encoding = "utf-8")
    with pytest.raises(HTTPException) as caught:
        _open(inference, name)
    assert caught.value.status_code == status
    assert launched == []


def test_only_the_installation_owner_opens_or_reveals_files_on_the_host(tmp_path, monkeypatch):
    from fastapi import HTTPException

    from hub.services.models import account_access

    inference, sandbox, launched = _sandbox_route_setup(tmp_path, monkeypatch)
    (sandbox / "report.pdf").write_bytes(b"%PDF-1.4")
    monkeypatch.setattr(account_access, "managed_account", lambda: True)
    with pytest.raises(HTTPException) as caught:
        _open(inference, "report.pdf")
    assert caught.value.status_code == 403
    with pytest.raises(HTTPException) as caught:
        asyncio.run(
            inference.reveal_sandbox_dir(
                "thread-1", request = None, token = None, session = None, file = "report.pdf"
            )
        )
    assert caught.value.status_code == 403
    assert launched == []


def test_a_file_swapped_for_a_link_after_the_check_opens_what_was_checked(tmp_path, monkeypatch):
    from utils.paths import path_utils

    monkeypatch.setattr("utils.paths.storage_roots.cache_root", lambda: tmp_path / "cache")
    report = tmp_path / "sandbox" / "report.pdf"
    report.parent.mkdir()
    report.write_bytes(b"%PDF-1.4 checked")
    app = tmp_path / "Evil.app"
    app.write_text("x", encoding = "utf-8")
    real_fdopen = os.fdopen

    def swap_then_open(fd, *args, **kwargs):
        os.unlink(report)
        os.symlink(app, report)
        return real_fdopen(fd, *args, **kwargs)

    monkeypatch.setattr(os, "fdopen", swap_then_open)
    staged = path_utils._stage_for_open(report, report.parent)
    assert not staged.is_symlink()
    assert staged.read_bytes() == b"%PDF-1.4 checked"


def test_a_file_still_being_written_is_staged_as_it_was_opened(tmp_path, monkeypatch):
    from utils.paths import path_utils

    monkeypatch.setattr("utils.paths.storage_roots.cache_root", lambda: tmp_path / "cache")
    log = tmp_path / "sandbox" / "run.log"
    log.parent.mkdir()
    log.write_bytes(b"line 1\n")
    real_fdopen = os.fdopen

    def append_then_open(fd, *args, **kwargs):
        with open(log, "ab") as handle:
            handle.write(b"line 2\n")
        return real_fdopen(fd, *args, **kwargs)

    monkeypatch.setattr(os, "fdopen", append_then_open)
    staged = path_utils._stage_for_open(log, log.parent)
    assert staged.read_bytes() == b"line 1\n"


def test_a_file_cut_short_while_staging_is_not_handed_over_half_copied(tmp_path, monkeypatch):
    from utils.paths import path_utils

    monkeypatch.setattr("utils.paths.storage_roots.cache_root", lambda: tmp_path / "cache")
    table = tmp_path / "sandbox" / "rows.csv"
    table.parent.mkdir()
    table.write_bytes(b"a,b\n1,2\n")
    real_fdopen = os.fdopen

    def truncate_then_open(fd, *args, **kwargs):
        os.truncate(table, 4)
        return real_fdopen(fd, *args, **kwargs)

    monkeypatch.setattr(os, "fdopen", truncate_then_open)
    with pytest.raises(OSError):
        path_utils._stage_for_open(table, table.parent)
    assert list((tmp_path / "cache" / "open-staging").iterdir()) == []


def test_opening_a_file_again_reuses_its_copy_until_either_changes(tmp_path, monkeypatch):
    from utils.paths import path_utils

    monkeypatch.setattr("utils.paths.storage_roots.cache_root", lambda: tmp_path / "cache")
    video = tmp_path / "sandbox" / "clip.mp4"
    video.parent.mkdir()
    video.write_bytes(b"frames v1")
    first = path_utils._stage_for_open(video, video.parent)
    assert path_utils._stage_for_open(video, video.parent) == first
    assert len(list((tmp_path / "cache" / "open-staging").iterdir())) == 1

    first.write_bytes(b"frames v0")
    edited = path_utils._stage_for_open(video, video.parent)
    assert edited != first and edited.read_bytes() == b"frames v1"

    # Same size and, as on a coarse-timestamp volume, the same mtime.
    before = video.stat()
    video.write_bytes(b"frames v2")
    os.utime(video, ns = (before.st_atime_ns, before.st_mtime_ns))
    changed = path_utils._stage_for_open(video, video.parent)
    assert changed not in (first, edited) and changed.read_bytes() == b"frames v2"


def test_reopening_after_many_edits_compares_one_earlier_copy(tmp_path, monkeypatch):
    from utils.paths import path_utils

    monkeypatch.setattr("utils.paths.storage_roots.cache_root", lambda: tmp_path / "cache")
    table = tmp_path / "sandbox" / "rows.csv"
    table.parent.mkdir()
    for version in range(5):
        table.write_bytes(b"a,b\n1,%d\n" % version)
        path_utils._stage_for_open(table, table.parent)
    compared = []
    real_same_bytes = path_utils._same_bytes

    def counting(*args):
        compared.append(args)
        return real_same_bytes(*args)

    monkeypatch.setattr(path_utils, "_same_bytes", counting)
    table.write_bytes(b"a,b\n1,9\n")
    path_utils._stage_for_open(table, table.parent)
    assert len(compared) == 1


@pytest.mark.skipif(os.name == "nt", reason = "symlinked directories need privileges on Windows")
def test_a_parent_swapped_for_a_link_is_refused(tmp_path, monkeypatch):
    from utils.paths import path_utils

    monkeypatch.setattr("utils.paths.storage_roots.cache_root", lambda: tmp_path / "cache")
    sandbox, outside = tmp_path / "sandbox", tmp_path / "outside"
    sandbox.mkdir()
    outside.mkdir()
    (outside / "secret.pdf").write_bytes(b"%PDF-1.4 secret")
    (sandbox / "outputs").symlink_to(outside, target_is_directory = True)
    with pytest.raises(FileNotFoundError):
        path_utils._stage_for_open(sandbox / "outputs" / "secret.pdf", sandbox)


@pytest.mark.skipif(sys.platform == "win32", reason = "symlinks need privileges on Windows")
def test_a_parent_swapped_for_a_link_before_a_reveal_is_refused(tmp_path, monkeypatch):
    import asyncio

    from fastapi import HTTPException

    from utils.paths import path_utils

    inference, sandbox, _launched = _sandbox_route_setup(tmp_path, monkeypatch)
    (sandbox / "outputs").mkdir(exist_ok = True)
    (sandbox / "outputs" / "report.csv").write_text("a,b", encoding = "utf-8")
    outside = tmp_path / "outside"
    outside.mkdir()
    (outside / "report.csv").write_text("secret", encoding = "utf-8")
    checked = inference._sandbox_regular_file

    def check_then_swap(*args):
        result = checked(*args)
        shutil.rmtree(sandbox / "outputs")
        (sandbox / "outputs").symlink_to(outside, target_is_directory = True)
        return result

    monkeypatch.setattr(inference, "_sandbox_regular_file", check_then_swap)
    revealed = []
    monkeypatch.setattr(
        path_utils, "reveal_in_file_manager", lambda path, **kw: revealed.append(path)
    )
    with pytest.raises(HTTPException) as caught:
        asyncio.run(
            inference.reveal_sandbox_dir(
                "thread-1", request = None, token = None, session = None, file = "outputs/report.csv"
            )
        )
    assert caught.value.status_code == 404
    assert revealed == []


def test_revealing_a_sandbox_file_selects_that_file(tmp_path, monkeypatch):
    import asyncio

    from utils.paths import path_utils

    inference, sandbox, _launched = _sandbox_route_setup(tmp_path, monkeypatch)
    target = sandbox / "outputs" / "report.csv"
    target.write_text("a,b", encoding = "utf-8")
    revealed = []
    monkeypatch.setattr(
        path_utils, "reveal_in_file_manager", lambda path, **kw: revealed.append(path)
    )
    result = asyncio.run(
        inference.reveal_sandbox_dir(
            "thread-1", request = None, token = None, session = None, file = "outputs/report.csv"
        )
    )
    assert result["path"] == os.path.realpath(target)
    assert revealed == [Path(os.path.realpath(target))]


if __name__ == "__main__":
    sys.exit(pytest.main([__file__, "-q", "-s"]))