Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
feat(chat): make agentic turns survive tab-close (#10365)
CelesteHeartsong0270575f5aaf
diff --git a/studio/backend/core/inference/chat_generation_runs.py b/studio/backend/core/inference/chat_generation_runs.pyindex 33272c6a919..81b0bfe0d53 100644--- a/studio/backend/core/inference/chat_generation_runs.py+++ b/studio/backend/core/inference/chat_generation_runs.py@@ -18,7 +18,7 @@ from starlette.requests import Requestfrom core.inference.llama_keepwarm import InferenceActivityReservationfrom core.training.account_jobs import sweepable_job_accounts-from utils.account_context import run_as+from utils.account_context import current_account_id, run_asfrom loggers import get_loggerfrom models.inference import ChatCompletionRequestfrom state import active_generations@@ -493,6 +493,35 @@ class ChatGenerationSupervisor:passreturn Truecancel_event = threading.Event()+ # Durable marker read by state.tool_approvals.wait_tool_decision: a confirm-mode ("ask") call+ # parked mid-run waits for the returning session (resolved by approval_id) instead of the+ # 3600s ceiling a browser-owned run uses. In-memory only, so a backend restart still loses+ # the slot. Two things end an abandoned park, whichever comes first:+ # 1. the park ceiling itself, UNSLOTH_STUDIO_TOOL_APPROVAL_TIMEOUT_S, default 300s. The gate+ # denies, the model is told the call timed out unanswered and adapts, and the run carries+ # on. A user who returns after that finds the call already refused, not still waiting.+ # The ceiling counts time with NOBODY WATCHING: durable means cancel_on_disconnect is+ # off, not that the tab is gone, so a user reading the card keeps the full+ # _DECISION_TIMEOUT. durable_run_id is how the gate asks (state/run_subscribers.py).+ # 2. the lease sweeper, for a producer wedged before it ever reaches the gate. Parking does+ # not renew the progress lease, so once progress has aged past the lease timeout+ # reconcile_runs settles the run as interrupted and supervisor.cancel() sets THIS event,+ # which wait_tool_decision polls at 500ms.+ # Either way the waiter returns deny and pops its own _pending slot. Note what the ceiling+ # does NOT bound: it ends one approval WAIT, not the run. The loop appends the denial as a+ # tool message and keeps generating, so the InferenceActivityReservation below is released by+ # the producer unwinding and by nothing else. A turn that parks on several calls in a row can+ # therefore hold it for several ceilings, and the progress between them renews the lease. The+ # sweeper is the only bound on a producer that stops making progress at all.+ cancel_event.durable = True+ cancel_event.durable_run_id = run_id+ # Same scope ActiveGeneration captures below: the id alone is not unique across accounts.+ cancel_event.durable_account_id = current_account_id() or ""+ # Re-arming the approval counter alone is not enough: parking makes no progress, so the+ # sweeper settles the run at the lease timeout (1200s) and cancels the wait, capping an+ # ATTENDED deliberation near 20 minutes. A watching user is not the wedged producer the+ # lease exists to reap.+ cancel_event.renew_lease = lambda: db.touch_progress(run_id)activity = InferenceActivityReservation()activity.reserve()registration = active_generations.ActiveGeneration(diff --git a/studio/backend/core/inference/llama_cpp.py b/studio/backend/core/inference/llama_cpp.pyindex 13bc4b52d46..cab56b097b8 100644--- a/studio/backend/core/inference/llama_cpp.py+++ b/studio/backend/core/inference/llama_cpp.py@@ -471,10 +471,13 @@ from core.inference.tool_loop_controller import (tool_call_limit_nudge,)from state.tool_approvals import (+ DECISION_EXPIRED,+ TOOL_APPROVAL_EXPIRED_MESSAGE,TOOL_REJECTED_MESSAGE,abort_tool_decision,begin_tool_decision,new_approval_id,+ decision_reason,wait_tool_decision,)from utils.paths.path_utils import _is_wsl, is_appledouble_metadata@@ -36979,6 +36982,9 @@ class LlamaCppBackend:if decision_slot is not Noneelse None)+ # The slot is where the waiter says WHY: the user's refusal, or an approval+ # nobody answered. Read before decision_slot is dropped in the deny branch.+ _decision_reason = decision_reason(decision_slot)if _decision is not None and _decision != "deny":# Approved: now it really is running.yield {"type": "status", "text": decision.status_text}@@ -36986,17 +36992,25 @@ class LlamaCppBackend:decision_slot = None_forced_choice_resolved = Trueresolved_provisional_tool_call_ids.add(decision.tool_call_id)+ # An approval nobody answered is not the user's decision, and this+ # string is the only account of the call both the model and the+ # reopened card get: the buttons are gone by the time it lands.+ _denied_text = (+ TOOL_APPROVAL_EXPIRED_MESSAGE+ if _decision_reason == DECISION_EXPIRED+ else TOOL_REJECTED_MESSAGE+ )yield {"type": "tool_end","tool_name": decision.tool_name,"tool_call_id": decision.tool_call_id,- "result": TOOL_REJECTED_MESSAGE,+ "result": _denied_text,"provenance": decision.provenance,}denied_message = {"role": "tool","name": decision.tool_name,- "content": TOOL_REJECTED_MESSAGE,+ "content": _denied_text,}if decision.tool_call_id:denied_message["tool_call_id"] = decision.tool_call_iddiff --git a/studio/backend/core/inference/safetensors_agentic.py b/studio/backend/core/inference/safetensors_agentic.pyindex c288054bcca..c20daa8274e 100644--- a/studio/backend/core/inference/safetensors_agentic.py+++ b/studio/backend/core/inference/safetensors_agentic.py@@ -76,10 +76,13 @@ from core.inference.chat_template_helpers import ()from core.inference.passthrough_healing import nudge_enabledfrom state.tool_approvals import (+ DECISION_EXPIRED,+ TOOL_APPROVAL_EXPIRED_MESSAGE,TOOL_REJECTED_MESSAGE,abort_tool_decision,begin_tool_decision,new_approval_id,+ decision_reason,wait_tool_decision,)@@ -1467,24 +1470,35 @@ def run_safetensors_tool_loop(if decision_slot is not Noneelse None)+ # The slot is where the waiter says WHY: the user's refusal, or an approval nobody+ # answered. Read before decision_slot is dropped in the deny branch below.+ _decision_reason = decision_reason(decision_slot)if _decision is not None and _decision != "deny":yield {"type": "status", "text": decision.status_text}if _decision == "deny":decision_slot = Noneif provisional_match:provisional_resolved = True+ # An approval nobody answered is not the user's decision, and this string is+ # the only account of the call both the model and the reopened card get: the+ # buttons are gone by the time it lands.+ _denied_text = (+ TOOL_APPROVAL_EXPIRED_MESSAGE+ if _decision_reason == DECISION_EXPIRED+ else TOOL_REJECTED_MESSAGE+ )yield {"type": "tool_end","tool_name": decision.tool_name,"tool_call_id": decision.tool_call_id,- "result": TOOL_REJECTED_MESSAGE,+ "result": _denied_text,"provenance": decision.provenance,}tool_denied = Truedenied_message = {"role": "tool","name": decision.tool_name,- "content": TOOL_REJECTED_MESSAGE,+ "content": _denied_text,}if decision.tool_call_id:denied_message["tool_call_id"] = decision.tool_call_iddiff --git a/studio/backend/core/inference/studio_tool_loop.py b/studio/backend/core/inference/studio_tool_loop.pyindex 3be1beca53a..fd6faf43304 100644--- a/studio/backend/core/inference/studio_tool_loop.py+++ b/studio/backend/core/inference/studio_tool_loop.py@@ -97,9 +97,12 @@ from core.inference.tool_stream_exec import ()from core.inference.tools import build_rag_autoinject, execute_tool, is_high_risk_tool_callfrom state.tool_approvals import (+ DECISION_EXPIRED,+ TOOL_APPROVAL_EXPIRED_MESSAGE,TOOL_REJECTED_MESSAGE,abort_tool_decision,begin_tool_decision,+ decision_reason,new_approval_id,wait_tool_decision,)@@ -1681,6 +1684,7 @@ async def stream_with_studio_tools()yield _sse(start_event)verdict = None+ denied_reason = Noneif decision_slot is not None:waiter = asyncio.ensure_future(asyncio.to_thread(@@ -1703,6 +1707,9 @@ async def stream_with_studio_tools(waiter.cancel()verdict = waiter.result() if waiter.done() else Noneif verdict == "deny":+ # Read before decision_slot is dropped below: the slot is where the waiter says+ # whether this was the user's refusal or an approval nobody answered.+ denied_reason = decision_reason(decision_slot)decision_slot = Nonedenied = Trueelif verdict is not None:@@ -1714,19 +1721,27 @@ async def stream_with_studio_tools(abort_tool_decision(decision_slot, approval_id)if denied:+ # An approval nobody answered is not the user's decision, and this string is the only+ # account of the call both the model and the reopened card get: the buttons are gone+ # by the time it lands. Saying "the user declined" there is simply false.+ denied_text = (+ TOOL_APPROVAL_EXPIRED_MESSAGE+ if denied_reason == DECISION_EXPIRED+ else TOOL_REJECTED_MESSAGE+ )yield _sse({"type": "tool_end","tool_name": name,"tool_call_id": card_id,- "result": TOOL_REJECTED_MESSAGE,+ "result": denied_text,"provenance": decision.provenance,})denied_message: dict[str, Any] = {"role": "tool","name": name,- "content": TOOL_REJECTED_MESSAGE,+ "content": denied_text,}if call_id:denied_message["tool_call_id"] = call_iddiff --git a/studio/backend/models/inference.py b/studio/backend/models/inference.pyindex 15ff0859fca..8b0bb9e9399 100644--- a/studio/backend/models/inference.py+++ b/studio/backend/models/inference.py@@ -2839,6 +2839,14 @@ class ToolConfirmRequest(BaseModel):decision: Literal["allow", "deny"] = "deny"+class ToolApprovalStatusRequest(BaseModel):+ """Ask whether one approval is still waiting. Takes the id rather than listing them, so a+ caller can only ask about an approval it already holds and cannot enumerate anyone else's."""++ session_id: Optional[str] = None+ approval_id: Optional[str] = None++class OpenAIContainerRequest(BaseModel):"""Shared body for the OpenAI container endpoints (list / create / delete).diff --git a/studio/backend/routes/chat_generation_runs.py b/studio/backend/routes/chat_generation_runs.pyindex 8a2bfc87ba0..d2bdd9a0661 100644--- a/studio/backend/routes/chat_generation_runs.py+++ b/studio/backend/routes/chat_generation_runs.py@@ -5,6 +5,8 @@from __future__ import annotations+import os+import asyncioimport jsonimport re@@ -19,7 +21,7 @@ from pydantic import BaseModel, ConfigDict, Field, ValidationErrorfrom auth.authentication import get_current_subjectfrom auth import policy-from state import active_generations+from state import active_generations, run_subscribersfrom utils.account_context import current_account, current_account_id, run_asfrom core.inference.llama_keepwarm import inference_lifecycle_gatefrom models.inference import ChatCompletionRequest@@ -154,6 +156,7 @@ def _sanitize_request(payload: CreateChatGenerationRun) -> dict[str, Any]:) from exc# Without this an unservable part is queued at 202 and fails where the caller cannot see it.from routes.inference import (+ _messages_have_embedded_image,_messages_have_input_audio,_reject_unsupported_content_parts,_request_has_video,@@ -186,24 +189,38 @@ def _sanitize_request(payload: CreateChatGenerationRun) -> dict[str, Any]:)# A media turn has no replayable transcript and its payload persists verbatim, so a base64 blob would live in# request_json for the life of the thread. _MEDIA_FIELDS is field-shaped, so a video_url part- # needs _request_has_video.+ # needs _request_has_video, and an inline image part needs _messages_have_embedded_image:+ # turn-scoping means a TEXT-only follow-up no longer sets top-level image_base64, yet the+ # thread's earlier screenshot still rides along inside messages[].content, so the field-shaped+ # check alone admits it and re-persists the blob on every follow-up.if (any(raw.get(field) not in (None, "") for field in _MEDIA_FIELDS)or _messages_have_input_audio(request.messages)+ or _messages_have_embedded_image(request.messages)or _request_has_video(request)):raise HTTPException(status_code = 400,detail = "Media chat runs use the legacy streaming path",)- # Recovery currently rebuilds text and reasoning deltas, not server-side tool events. Keep any request whose- # effective policy can enter the local tool loop on the legacy subscriber-owned stream until those events are- # replayable.+ # What UNSLOTH_STUDIO_DURABLE_TOOL_TURNS=0 hands back to the legacy stream beyond the raw `tools` key: the+ # launcher's effective tool policy, and any checkpoint recall that can switch tools on mid-thread.from routes.inference import _checkpoint_recall_may_enable_tools, _effective_enable_toolsrequest = request.model_copy(update = {"thread_id": payload.threadId})- if (+ # Shipped ON (default ON so a restart alone activates it - env scoping proved unreliable across launchers):+ # tool-enabled turns are durable because replay now re-tags persisted frames exactly as the live stream yields+ # them; set UNSLOTH_STUDIO_DURABLE_TOOL_TURNS=0 to restore the original refusal.+ _durable_tools = os.environ.get("UNSLOTH_STUDIO_DURABLE_TOOL_TURNS", "1").strip().lower() in (+ "1",+ "true",+ "yes",+ )+ # NOTE: the guard wraps the WHOLE or-chain. `A and B or C or D` parses as `(A and B) or C or D`, so a bare+ # prefix only guarded raw["tools"]; the Studio UI sends `enable_tools: true` (term 2), which still raised with+ # the flag ON - the flip was inert for exactly the turns it was added to unblock.+ if not _durable_tools and (raw.get("tools")or request.enable_tools is Trueor bool(request.mcp_enabled)@@ -359,6 +376,11 @@ async def chat_generation_events(# run_in_executor does not copy ContextVars, unlike asyncio.to_thread.wait_for_events = partial(run_as, current_account(), db.wait_for_events)+ # One token per stream, so a closing tab clears only its OWN stamp. Two tabs on a run, or a+ # reconnect overlapping the stream it replaces, otherwise delete each other's heartbeat.+ follower = run_subscribers.new_follower_token()+ follower_account = current_account_id() or ""+async def stream():nonlocal cursorloop = asyncio.get_running_loop()@@ -370,6 +392,10 @@ async def chat_generation_events(if opening["status"] in db.TERMINAL_STATUSES and cursor >= int(opening["lastEventSeq"]):returnwhile True:+ # A parked tool approval reads this before applying its ceiling, so the ceiling bounds+ # an ABANDONED decision rather than a user still reading the card. Stamped before the+ # wait so a follower attaching mid-park counts immediately. See state/run_subscribers.py.+ run_subscribers.mark_subscriber_seen(run_id, follower, follower_account)events = await loop.run_in_executor(_EVENT_WAIT_EXECUTOR,wait_for_events,@@ -405,8 +431,20 @@ async def chat_generation_events(# still drops it and no client parsing it as an event is affected.yield f": keep-alive {int(snapshot['updatedAt'])}\n\n"+ async def stream_while_attended():+ """``stream`` plus the bookend that says this follower has gone.++ The stamp ages out on its own, so this only makes a cleanly closed tab stop counting as+ attended now rather than ~45s from now.+ """+ try:+ async for frame in stream():+ yield frame+ finally:+ run_subscribers.subscriber_departed(run_id, follower, follower_account)+return StreamingResponse(- stream(),+ stream_while_attended(),media_type = "text/event-stream",headers = {"Cache-Control": "no-cache", "X-Accel-Buffering": "no"},)diff --git a/studio/backend/routes/inference.py b/studio/backend/routes/inference.pyindex dd8be7253a2..518332f9526 100644--- a/studio/backend/routes/inference.py+++ b/studio/backend/routes/inference.py@@ -3415,6 +3415,7 @@ from models.inference import (ChatCountTokensRequest,ChatCompletionChunk,ChatCompletion,+ ToolApprovalStatusRequest,ToolConfirmRequest,ChatMessage,ChunkChoice,@@ -3588,7 +3589,7 @@ def _request_used_api_key(request: Any) -> bool:return False-from state.tool_approvals import resolve_tool_decision+from state.tool_approvals import resolve_tool_decision, tool_decision_is_pendingfrom core.inference.model_ids import display_model_name, model_id_matches, public_model_idfrom core.inference.api_monitor import api_monitor@@ -18602,6 +18603,22 @@ async def confirm_tool_call(return {"resolved": True}+@studio_router.post("/tool-approval-status")+async def tool_approval_status(+ request: ToolApprovalStatusRequest, current_subject: str = Depends(get_current_subject)+):+ """Whether a parked approval is still waiting on a human.++ A reopened tab restores Approve/Deny from a saved card that has no result and an approval id.+ That shape is identical whether the call is still parked or the user already answered it and+ the tool is mid-flight, since the result only arrives with tool_end. Without this the tab+ re-arms the answered one and every press 404s.++ POST rather than GET so the id travels in the body instead of a URL that lands in logs.+ """+ return {"pending": tool_decision_is_pending(request.approval_id, request.session_id)}++@studio_router.get("/monitor")async def get_api_monitor(current_subject: str = Depends(get_current_subject)):"""Return recent OpenAI-compatible API activity for Unsloth."""@@ -21840,6 +21857,30 @@ def _messages_have_input_audio(messages) -> bool:)+def _messages_have_embedded_image(messages) -> bool:+ """True when any message carries an image as an inline base64 data URL.++ Deliberately narrower than "has an image". A remote image_url is a short string and costs+ nothing to persist, so it has no business forcing a turn off the durable path; an inline+ data URL is the whole blob, and durable runs persist their request verbatim, so admitting+ one writes it into request_json again on every follow-up turn for the life of the thread.+ That is the exact cost the media guard exists to prevent, and it is reachable without any+ top-level image field once the gate is turn-scoped: the history image still rides along+ inside messages[].content.+ """+ for msg in messages:+ content = getattr(msg, "content", None)+ if not isinstance(content, list):+ continue+ for part in content:+ if getattr(part, "type", None) != "image_url":+ continue+ url = getattr(getattr(part, "image_url", None), "url", None)+ if isinstance(url, str) and url.startswith("data:"):+ return True+ return False++def _normalise_chat_content_parts(payload) -> None:"""Lift the request's ``input_audio`` part onto ``audio_base64``, in place.diff --git a/studio/backend/state/run_subscribers.py b/studio/backend/state/run_subscribers.pynew file mode 100644index 00000000000..59b29269048--- /dev/null+++ b/studio/backend/state/run_subscribers.py@@ -0,0 +1,110 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""Which durable runs currently have a browser watching them.++"Durable" means cancel_on_disconnect is off, not that the tab is gone, so nothing downstream could+tell a watching user from an abandoned run. ``state.tool_approvals.wait_tool_decision`` needs that:+its park ceiling is only defensible for a run nobody is watching.++Keyed by (account, run) then by follower. Account, because studio.db is per-account+(``utils.paths.storage_roots.studio_db_path``) and the run id is the client's+(``CreateChatGenerationRun.runId``), so two accounts can hold the same id and one tenant's follower+would answer for another's run. Follower, because two tabs (or a reconnect overlapping the stream it+replaces) share a run, and a single stamp let either one's cleanup delete the other's heartbeat.++A heartbeat rather than a reference count: a count must be decremented by a ``finally`` an abandoned+generator may never reach, and a leaked increment would park an abandoned approval forever.+"""++from __future__ import annotations++import threading+import time+import uuid++# Three follower keep-alive periods. Errs towards present: a stale stamp costs an idle slot for 45s,+# reading a present user as absent costs them the decision.+_ATTENDED_FOR_S = 45.0++_LOCK = threading.Lock()+# (account_id, run_id) -> {follower token: monotonic seconds at that follower's last heartbeat}+_SEEN: dict[tuple[str, str], dict[str, float]] = {}+++def _key(account_id: str, run_id: str) -> tuple[str, str]:+ # A missing account is its own scope, never a wildcard matching accounts that name themselves.+ return (account_id or "", run_id)+++def new_follower_token() -> str:+ """Identify one follower for the life of its stream, so it can only clear its own stamp."""+ return uuid.uuid4().hex+++def mark_subscriber_seen(+ run_id: str,+ follower: str,+ account_id: str = "",+) -> None:+ """Record that ``follower`` is attached to ``account_id``'s ``run_id`` right now.++ Called once per iteration of the run's SSE loop, so at least every keep-alive period.+ """+ if not run_id or not follower:+ return+ with _LOCK:+ _SEEN.setdefault(_key(account_id, run_id), {})[follower] = time.monotonic()+++def subscriber_departed(+ run_id: str,+ follower: str,+ account_id: str = "",+) -> None:+ """Drop only ``follower``'s stamp when its loop exits, leaving any other follower's alone.++ Best effort: an abruptly closed generator never runs its cleanup, which is why ``is_attended``+ expires by age rather than trusting this. This only buys promptness on a clean close.+ """+ if not run_id or not follower:+ return+ key = _key(account_id, run_id)+ with _LOCK:+ followers = _SEEN.get(key)+ if followers is None:+ return+ followers.pop(follower, None)+ if not followers:+ _SEEN.pop(key, None)+++def is_attended(run_id: str, account_id: str = "") -> bool:+ """Whether ANY follower of ``account_id``'s ``run_id`` has been heard from recently."""+ if not run_id:+ return False+ key = _key(account_id, run_id)+ cutoff = time.monotonic() - _ATTENDED_FOR_S+ with _LOCK:+ followers = _SEEN.get(key)+ if not followers:+ return False+ # Expired lazily; a reaper would need a thread for what this loop already does.+ for token, seen in list(followers.items()):+ if seen <= cutoff:+ del followers[token]+ if not followers:+ _SEEN.pop(key, None)+ return False+ return True+++def attendance_for_tests(run_id: str, account_id: str = "") -> int:+ """How many followers are currently stamped for ``account_id``'s ``run_id``."""+ with _LOCK:+ return len(_SEEN.get(_key(account_id, run_id)) or {})+++def reset_for_tests() -> None:+ with _LOCK:+ _SEEN.clear()diff --git a/studio/backend/state/tool_approvals.py b/studio/backend/state/tool_approvals.pyindex d1ddef9b420..9e7f86c6b05 100644--- a/studio/backend/state/tool_approvals.py+++ b/studio/backend/state/tool_approvals.py@@ -10,16 +10,86 @@ Each gated call is identified by a unique ``approval_id`` (minted with ``new_appThe slot is registered with ``begin_tool_decision`` *before* the loop yields ``tool_start``, closing the race where a fast confirmation (or an auto "Always allow") could arrive before the waiter exists. ``wait_tool_decision`` then blocks and cleans up its own slot."""+import osimport secretsimport threadingfrom typing import Optional+from state import run_subscribers+# Generous ceiling so a user can deliberate; the stop button or a disconnect still breaks the wait early via cancel_event._DECISION_TIMEOUT = 3600.0+# How long a durable (parked) approval waits for a human before denying and letting the agent continue.+# Default is generous enough for "grab your phone" but short enough that an unattended agentic loop+# doesn't stall for the full lease window. 0 denies at the first poll, so a decision that lands inside+# that first 500ms still wins; it is "autonomous", not "instant".+_PARK_TIMEOUT_DEFAULT_S = 300.0+++def _park_timeout_from_env() -> float:+ """Read the park ceiling once, at import, tolerating a value nobody can parse.++ Read here rather than per wait so one run cannot change ceiling mid-flight. A typo must not be+ fatal: this module is imported on the chat path, so raising would take the backend down at+ startup over an environment variable, and a stuck approval is the lesser failure. A negative or+ non-finite value would disable the ceiling silently, so both fall back too.+ """+ raw = os.environ.get("UNSLOTH_STUDIO_TOOL_APPROVAL_TIMEOUT_S")+ if raw is None or not raw.strip():+ return _PARK_TIMEOUT_DEFAULT_S+ try:+ value = float(raw)+ except (TypeError, ValueError):+ print(+ f"[unsloth] UNSLOTH_STUDIO_TOOL_APPROVAL_TIMEOUT_S={raw!r} is not a number; "+ f"using {_PARK_TIMEOUT_DEFAULT_S:g}s",+ )+ return _PARK_TIMEOUT_DEFAULT_S+ if value != value or value in (float("inf"), float("-inf")) or value < 0:+ print(+ f"[unsloth] UNSLOTH_STUDIO_TOOL_APPROVAL_TIMEOUT_S={raw!r} is out of range; "+ f"using {_PARK_TIMEOUT_DEFAULT_S:g}s",+ )+ return _PARK_TIMEOUT_DEFAULT_S+ return value+++_PARK_TIMEOUT_S = _park_timeout_from_env()++# Far under the lease timeout (1200s), far over the 0.5s poll: one small write every half minute.+_LEASE_RENEW_EVERY_S = 30.0+# Fed to the model as the tool result when the user denies a call, so it can adapt instead of the turn ending abruptly.TOOL_REJECTED_MESSAGE = "The user declined to run this tool call."+# The same, for a call that hit its park ceiling unanswered. Distinct from TOOL_REJECTED_MESSAGE+# because the model and the card read the result verbatim, and by then it is the only account of the+# call the returning user gets: telling them they declined something they never saw is false.+TOOL_APPROVAL_EXPIRED_MESSAGE = (+ "This tool call was not run: nobody answered the approval request in time."+)++# Why wait_tool_decision returned, on the slot rather than in the return value, so the verdict stays+# a bare "allow"/"deny": the three tool loops patch it by name in tests+# (core.inference.llama_cpp.wait_tool_decision and friends), and a fake that knows nothing about+# reasons leaves slot["reason"] untouched, which reads as the old behaviour.+DECISION_ANSWERED = "answered"+DECISION_CANCELLED = "cancelled"+DECISION_EXPIRED = "expired"+++def decision_reason(slot) -> Optional[str]:+ """Why ``wait_tool_decision`` returned for ``slot``, or None if it did not say.++ None on purpose for a slot a stubbed waiter never touched: the caller then falls back to+ TOOL_REJECTED_MESSAGE, which is exactly what it did before reasons existed.+ """+ if not isinstance(slot, dict):+ return None+ return slot.get("reason")++_lock = threading.Lock()# approval_id -> {"event": threading.Event, "decision": str|None, "session": str}_pending: dict[str, dict] = {}@@ -36,6 +106,8 @@ def begin_tool_decision(session_id, approval_id) -> dict:"event": threading.Event(),"decision": None,"session": session_id or "",+ # Filled in by wait_tool_decision; read back with decision_reason().+ "reason": None,}with _lock:_pending[approval_id] = slot@@ -48,22 +120,89 @@ def wait_tool_decision(cancel_event = None,timeout = _DECISION_TIMEOUT,):- """Block on a slot from ``begin_tool_decision`` until the user decides. Returns ``"allow"`` or ``"deny"``, falling back to ``"deny"`` if the wait times out or generation is cancelled first. Always removes its own slot on exit."""+ """Block on a slot from ``begin_tool_decision`` until the user decides. Returns ``"allow"`` or ``"deny"``, falling back to ``"deny"`` if the wait times out or generation is cancelled first. Records WHY in ``slot["reason"]`` (see ``decision_reason``), because a bare ``"deny"`` cannot tell a user who refused from an approval nobody answered, and the loops report one of those to the user as their own decision. Always removes its own slot on exit.++ The park ceiling measures time with NOBODY WATCHING, not time since the call parked. A durable run outlives its tab by design, so its cancel_event says nothing about whether a human is there; ``run_subscribers`` is what knows. While a follower is attached the deadline keeps re-arming and the wait is bounded by ``_DECISION_TIMEOUT`` exactly as a browser-owned run always was, so a user still reading what the tool wants to do does not lose the decision out from under them. Reaching that bound takes renewing the RUN's lease as well (``cancel_event.renew_lease``), since parking makes no progress and the sweeper would otherwise settle the run at its lease timeout, around 20 minutes, and cancel the wait. Once the followers go the ceiling runs (default 300s, ``UNSLOTH_STUDIO_TOOL_APPROVAL_TIMEOUT_S``) and an unattended agent adapts and continues. An explicit Stop still denies immediately.+ """+ park = bool(getattr(cancel_event, "durable", False))+ run_id = getattr(cancel_event, "durable_run_id", "") or ""+ # Run ids are account-local (per-account studio.db, client-chosen id), so attendance has to be+ # asked for under the same account or one tenant's follower answers for another's run.+ account_id = getattr(cancel_event, "durable_account_id", "") or ""+ renew_lease = getattr(cancel_event, "renew_lease", None)++ def _settle(verdict, reason):+ if isinstance(slot, dict):+ slot["reason"] = reason+ return verdict+try:+ # `waited` is time with nobody watching and resets when a follower is seen; `total` is the+ # whole wait and never resets. A park is bounded by both.waited = 0.0+ total = 0.0+ last_renew: Optional[float] = Nonewhile not slot["event"].wait(timeout = 0.5):if cancel_event is not None and cancel_event.is_set():- return "deny"+ return _settle("deny", DECISION_CANCELLED)waited += 0.5- if waited >= timeout:- return "deny"- return slot["decision"] or "deny"+ total += 0.5+ if not park:+ # Unchanged: the caller's ceiling is the only one a browser-owned run has ever had.+ if total >= timeout:+ return _settle("deny", DECISION_EXPIRED)+ continue+ # A durable park ignores the caller's timeout by design, so the attended backstop is this+ # module's own ceiling, the same hour a browser-owned run gets.+ if total >= _DECISION_TIMEOUT:+ return _settle("deny", DECISION_EXPIRED)+ if run_subscribers.is_attended(run_id, account_id):+ # Someone is watching, so this is deliberation, not abandonment.+ waited = 0.0+ # The RUN's lease too, not just this counter: parking makes no progress, so the+ # sweeper would otherwise settle the run at its lease timeout (1200s) and cancel the+ # wait, capping an attended deliberation near 20 minutes rather than the ceiling.+ if renew_lease is not None and (+ last_renew is None or total - last_renew >= _LEASE_RENEW_EVERY_S+ ):+ last_renew = total+ try:+ renew_lease()+ except Exception:+ # A lease we could not renew is the sweeper's problem, not a reason to drop+ # the decision this wait exists to collect.+ pass+ elif waited >= _PARK_TIMEOUT_S:+ return _settle("deny", DECISION_EXPIRED)+ return _settle(slot["decision"] or "deny", DECISION_ANSWERED)finally:with _lock:if _pending.get(approval_id) is slot:_pending.pop(approval_id, None)+def tool_decision_is_pending(approval_id, session_id = None) -> bool:+ """True while `approval_id` is still waiting on a human.++ A reopened tab cannot otherwise tell a parked call from one the user already answered: both are+ saved as a card with no result and an approval id, because the result only lands with tool_end.+ Re-arming the answered one puts Approve/Deny over a call that is already executing, and every+ press 404s. So the state is asked for rather than inferred.++ Scoped like resolve_tool_decision, and a set event counts as decided rather than pending, so the+ window between the decision and the waiter popping its own slot does not read as still-parked.+ """+ if not approval_id:+ return False+ with _lock:+ slot = _pending.get(approval_id)+ if not slot:+ return False+ if session_id is not None and slot["session"] != (session_id or ""):+ return False+ return not slot["event"].is_set()++def abort_tool_decision(slot, approval_id) -> None:"""Remove a slot that was announced but never entered ``wait_tool_decision``. Streaming wrappers may stop after ``tool_start`` is yielded and before the loop resumes into ``wait_tool_decision``, leaving no waiter to run the normal cleanup path, so the generator close path calls this explicitly."""with _lock:diff --git a/studio/backend/tests/test_chat_generation_runs.py b/studio/backend/tests/test_chat_generation_runs.pyindex b61b3c6eff0..de5e22313ab 100644--- a/studio/backend/tests/test_chat_generation_runs.py+++ b/studio/backend/tests/test_chat_generation_runs.py@@ -575,8 +575,6 @@ def test_an_explicit_delete_still_removes_a_detached_generated_assistant(chat_ho"override,detail",[({"provider_id": "external"}, "only for local"),- ({"tools": [{"type": "function"}]}, "legacy streaming"),- ({"enable_tools": True}, "legacy streaming"),({"rag_scope": {"access_token": "secret"}}, "Credentials"),({"rag_scope": {"signing_key": "secret"}}, "Credentials"),({"rag_scope": {"ssh_key": "secret"}}, "Credentials"),@@ -666,24 +664,54 @@ def test_request_sanitization_accepts_empty_optional_routing(overrides):assert _sanitize_request(_model(**overrides))["stream"] is True-def test_request_sanitization_rejects_launcher_default_tools():+# A tool-enabled turn is durable now: the run persists every decoded frame, so a call that parks on an approval+# survives the tab closing instead of being cancelled by it. The refusal these tests used to pin still exists - it is+# what UNSLOTH_STUDIO_DURABLE_TOOL_TURNS=0 restores - so both halves are pinned here: admitted by default, refused+# under the toggle, and nothing in between.+++def test_request_sanitization_admits_a_tool_enabled_turn_by_default():+ assert _sanitize_request(_model(tools = [{"type": "function"}]))["stream"] is True+ assert _sanitize_request(_model(enable_tools = True))["stream"] is True+++def test_request_sanitization_refuses_a_tool_enabled_turn_when_the_toggle_is_off(monkeypatch):+ monkeypatch.setenv("UNSLOTH_STUDIO_DURABLE_TOOL_TURNS", "0")+ with pytest.raises(Exception, match = "Tool-enabled chat runs use the legacy streaming path"):+ _sanitize_request(_model(tools = [{"type": "function"}]))+ with pytest.raises(Exception, match = "Tool-enabled chat runs use the legacy streaming path"):+ _sanitize_request(_model(enable_tools = True))+++def test_a_launcher_default_tool_policy_is_admitted_by_default_and_refused_under_the_toggle(+ monkeypatch,+):set_tool_policy_default(True)- with pytest.raises(Exception, match = "legacy streaming path"):+ assert _sanitize_request(_model())["stream"] is True+ monkeypatch.setenv("UNSLOTH_STUDIO_DURABLE_TOOL_TURNS", "0")+ with pytest.raises(Exception, match = "Tool-enabled chat runs use the legacy streaming path"):_sanitize_request(_model())-def test_request_sanitization_rejects_cli_tools_override_even_when_request_disables_tools():+def test_a_cli_tools_override_is_admitted_even_when_the_request_disables_tools(monkeypatch):set_tool_policy(True)- with pytest.raises(Exception, match = "legacy streaming path"):+ assert _sanitize_request(_model(enable_tools = False))["stream"] is True+ monkeypatch.setenv("UNSLOTH_STUDIO_DURABLE_TOOL_TURNS", "0")+ with pytest.raises(Exception, match = "Tool-enabled chat runs use the legacy streaming path"):_sanitize_request(_model(enable_tools = False))-def test_request_sanitization_rejects_checkpoint_recall_tool_loop(monkeypatch):+def test_a_checkpoint_recall_tool_loop_is_admitted_by_default_and_refused_under_the_toggle(+ monkeypatch,+):import routes.inference as inference_routes+monkeypatch.setattr(inference_routes, "_checkpoint_recall_may_enable_tools", lambda request: True, raising = False)- with pytest.raises(Exception, match = "legacy streaming path"):+ assert _sanitize_request(_model(enable_tools = False))["stream"] is True+ monkeypatch.setenv("UNSLOTH_STUDIO_DURABLE_TOOL_TURNS", "0")+ with pytest.raises(Exception, match = "Tool-enabled chat runs use the legacy streaming path"):_sanitize_request(_model(enable_tools = False))@@ -708,3 +736,110 @@ def test_request_sanitization_rejects_inline_media(field):"""with pytest.raises(Exception, match = "legacy streaming path"):_sanitize_request(_model(**{field: "iVBORw0KGgo="}))+++class TestEmbeddedImagesStayOffTheDurablePath:+ """A durable run persists its request verbatim, so an inline base64 image written into+ request_json is written again on every follow-up turn for the life of the thread.++ The media guard was field-shaped plus nested audio and video. It had no nested IMAGE check,+ and turn-scoping removed the thing that used to catch this case by accident: a text-only+ follow-up no longer sets top-level image_base64, yet the thread's earlier screenshot still+ rides along inside messages[].content. So the follow-up was admitted and the blob re-persisted.+ """++ _PNG = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg=="++ def _msg(self, parts):+ from models.inference import ChatMessage+ return ChatMessage(role = "user", content = parts)++ def test_an_inline_data_url_image_in_history_is_detected(self):+ from routes.inference import _messages_have_embedded_image+ messages = [+ self._msg(+ [+ {"type": "text", "text": "what is this?"},+ {+ "type": "image_url",+ "image_url": {"url": f"data:image/png;base64,{self._PNG}"},+ },+ ]+ ),+ self._msg([{"type": "text", "text": "and what colour was it?"}]),+ ]+ assert _messages_have_embedded_image(messages) is True++ def test_a_remote_image_url_is_not_treated_as_embedded(self):+ """Deliberately narrower than 'has an image'. A remote URL is a short string and costs+ nothing to persist, so it has no business forcing a turn off the durable path."""+ from routes.inference import _messages_have_embedded_image++ messages = [+ self._msg(+ [+ {"type": "text", "text": "what is this?"},+ {"type": "image_url", "image_url": {"url": "https://example.com/cat.png"}},+ ]+ )+ ]+ assert _messages_have_embedded_image(messages) is False++ def test_a_plain_text_thread_is_not_treated_as_media(self):+ """Negative control: if this ever returns True the guard would push every text turn onto+ the legacy path and the feature would silently stop working."""+ from routes.inference import _messages_have_embedded_image++ assert (+ _messages_have_embedded_image([self._msg([{"type": "text", "text": "hello"}])]) is False+ )+ from models.inference import ChatMessage++ assert _messages_have_embedded_image([ChatMessage(role = "user", content = "hello")]) is False+++# The admission half of TestEmbeddedImagesStayOffTheDurablePath: the helper returning True is not+# the claim, the route refusing the run is.++_INLINE_PNG_PART = {+ "type": "image_url",+ "image_url": {+ "url": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAA"+ "DUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==",+ },+}+++def test_a_text_only_follow_up_carrying_an_earlier_inline_image_stays_legacy():+ """The case turn-scoping opened. No top-level image_base64 is set on a text-only follow-up, so+ the field-shaped guard admitted it, and a durable run persists its request verbatim: the+ thread's earlier screenshot would be written into request_json again on every follow-up."""+ messages = [+ {"role": "user", "content": [{"type": "text", "text": "what is this?"}, _INLINE_PNG_PART]},+ {"role": "assistant", "content": "A red square."},+ {"role": "user", "content": [{"type": "text", "text": "and what colour was it again?"}]},+ ]+ with pytest.raises(Exception, match = "Media chat runs use the legacy streaming path"):+ _sanitize_request(_model(messages = messages))+++def test_a_remote_image_url_does_not_force_a_turn_off_the_durable_path():+ """Narrower than 'has an image' on purpose: a remote URL is a short string, so persisting it+ costs nothing and there is no reason to give up durability for it."""+ messages = [+ {+ "role": "user",+ "content": [+ {"type": "text", "text": "what is this?"},+ {"type": "image_url", "image_url": {"url": "https://example.com/cat.png"}},+ ],+ },+ ]+ assert _sanitize_request(_model(messages = messages))["stream"] is True+++def test_an_ordinary_text_thread_is_still_admitted():+ """Negative control. If the new guard over-matched, every text turn would fall back to the+ legacy stream and the whole feature would quietly stop working while these tests stayed green."""+ messages = [{"role": "user", "content": [{"type": "text", "text": "hello"}]}]+ assert _sanitize_request(_model(messages = messages))["stream"] is Truediff --git a/studio/backend/tests/test_chat_generation_supervisor.py b/studio/backend/tests/test_chat_generation_supervisor.pyindex 7f9671b4579..a01d2afb88b 100644--- a/studio/backend/tests/test_chat_generation_supervisor.py+++ b/studio/backend/tests/test_chat_generation_supervisor.py@@ -653,3 +653,34 @@ async def test_shutdown_returns_even_when_a_producer_will_not_unwind(durable_runfinally:release.set()await asyncio.sleep(0)+++# ── The durable marker is production state, so a test must read it off the producer ──+# Every approval test constructs `cancel.durable = True` by hand, which means the line in+# _ensure_reservation that actually sets it was pinned by nothing: deleting it left the whole+# backend suite green while silently returning every parked approval to the 3600s auto-deny.+# Same for durable_run_id, which is how the gate asks whether anyone is still watching.+++def test_the_producers_cancel_event_carries_the_durable_marker_and_its_run_id():+ supervisor = ChatGenerationSupervisor(SimpleNamespace(state = SimpleNamespace()))+ try:+ assert supervisor._ensure_reservation("run-durable", thread_id = "thread-1") is True+ cancel_event = supervisor._cancel_events["run-durable"]+ assert getattr(cancel_event, "durable", False) is True, (+ "state.tool_approvals.wait_tool_decision reads this to park an approval instead of "+ "auto-denying it, so without it a tool turn that outlives its tab still loses the call"+ )+ assert getattr(cancel_event, "durable_run_id", None) == "run-durable", (+ "the gate resolves attendance by run id (state/run_subscribers.py); without it an "+ "attended approval cannot be told from an abandoned one and expires at the park ceiling"+ )+ finally:+ supervisor.cancel("run-durable")+ registration = supervisor._active_registrations.pop("run-durable", None)+ if registration is not None:+ registration.__exit__(None, None, None)+ supervisor._cancel_events.pop("run-durable", None)+ activity = supervisor._activities.pop("run-durable", None)+ if activity is not None:+ activity.finish()diff --git a/studio/backend/tests/test_tool_approvals.py b/studio/backend/tests/test_tool_approvals.pyindex af792e652c0..a16b7cb27e6 100644--- a/studio/backend/tests/test_tool_approvals.py+++ b/studio/backend/tests/test_tool_approvals.py@@ -16,8 +16,9 @@ import timeimport pytest-from state import tool_approvals+from state import run_subscribers, tool_approvalsfrom state.tool_approvals import (+ TOOL_APPROVAL_EXPIRED_MESSAGE,TOOL_REJECTED_MESSAGE,abort_tool_decision,begin_tool_decision,@@ -143,6 +144,9 @@ def test_resolve_before_wait_is_not_lost():assert resolve_tool_decision(aid, "allow", session_id = "sess") is True# wait() is only entered now, after the decision already landed.assert wait_tool_decision(slot, aid) == "allow"+ # A stubbed waiter says nothing about why, and that reads as the pre-reason behaviour.+ assert tool_approvals.decision_reason({"event": None}) is None+ assert tool_approvals.decision_reason(None) is Noneassert not _has_pending(aid)@@ -259,3 +263,518 @@ def test_concurrent_distinct_calls_route_their_own_decisions():def test_rejected_message_is_user_facing_text():assert isinstance(TOOL_REJECTED_MESSAGE, str)assert TOOL_REJECTED_MESSAGE.strip()+++# ── Durable runs park the gate instead of auto-denying on timeout ───────+# A durable run's cancel_event is never set on a browser disconnect, so a pending+# approval must keep waiting for the returning session (resolved by id) rather than+# silently denying at the 3600s ceiling. An explicit Stop still denies: setting the+# event wins over parking.+++def test_durable_approval_parks_past_timeout():+ """A durable gate waits past its timeout for the session to return, not deny."""+ cancel = threading.Event()+ cancel.durable = True+ aid = new_approval_id()+ w = _Waiter("sess", aid, cancel_event = cancel, timeout = 0.2).start()+ # The short ceiling has long passed; the gate must still be parked, not auto-denied.+ time.sleep(0.6)+ assert _has_pending(aid), "durable gate must park past its timeout, not auto-deny"+ assert resolve_tool_decision(aid, "allow", session_id = "sess") is True+ assert w.join(timeout = 3.0) == "allow"+++def test_durable_cancel_still_denies():+ """An explicit Stop (cancel_event set) denies even when the run is durable."""+ cancel = threading.Event()+ cancel.durable = True+ aid = new_approval_id()+ w = _Waiter("sess", aid, cancel_event = cancel).start()+ cancel.set()+ assert w.join(timeout = 3.0) == "deny"+ assert _wait_until(lambda: not _has_pending(aid))+++def test_an_unanswered_park_is_released_by_the_settles_cancel_not_a_ceiling():+ """The sweeper's settle-cancel releases a park that the park timeout has not yet reached.++ At 0.6s the default park timeout (300s) has not elapsed, so the only thing that can release+ the gate is an external cancel — exactly what ``supervisor.cancel()`` does after+ ``reconcile_runs`` settles a lease-expired run. The parked gate must deny and pop its own+ slot, so the reservation unwinds.+ """+ cancel = threading.Event()+ cancel.durable = True+ aid = new_approval_id()+ w = _Waiter("sess", aid, cancel_event = cancel, timeout = 0.2).start()+ # Well within the park timeout (300s default); only an external cancel can release now.+ time.sleep(0.6)+ assert _has_pending(aid), "the park has not timed out yet; only cancel releases it"+ cancel.set() # what reconcile_runs' settle does to a lease-expired run+ assert w.join(timeout = 3.0) == "deny", "the settle's cancel must read as deny"+ assert _wait_until(lambda: not _has_pending(aid)), "the slot must be popped on release"+++def test_durable_park_denies_at_park_timeout(monkeypatch):+ """A durable park denies at the park timeout so an unattended agent adapts and continues.++ This is the release path for agentic work where the user has left: the approval times out,+ the model receives TOOL_REJECTED_MESSAGE, and the loop proceeds to the next step. The sweeper+ remains a backstop for producers wedged before they reach wait_tool_decision.+ """+ monkeypatch.setattr(tool_approvals, "_PARK_TIMEOUT_S", 0.2)+ cancel = threading.Event()+ cancel.durable = True+ aid = new_approval_id()+ w = _Waiter("sess", aid, cancel_event = cancel).start()+ # Park timeout (0.2s) has elapsed; the gate must deny on its own, no cancel needed.+ time.sleep(0.6)+ assert w.join(timeout = 3.0) == "deny", "the park timeout must release an unanswered approval"+ assert _wait_until(lambda: not _has_pending(aid)), "the slot must be popped on timeout"+++# ── The park ceiling is configuration, and configuration must not be able to kill the backend ──+++ "value,expected",+ [+ ("30", 30.0),+ ("0", 0.0),+ ("0.5", 0.5),+ (" 45 ", 45.0),+ # Anything unparseable, out of range, or nonsensical falls back rather than raising: this+ # module is imported on the chat path, so a typo here would otherwise take the backend down+ # at startup, and a stuck approval is the lesser failure.+ ("5m", 300.0),+ ("", 300.0),+ (" ", 300.0),+ ("none", 300.0),+ ("-1", 300.0),+ ("nan", 300.0),+ ("inf", 300.0),+ ],+)+def test_the_park_ceiling_reads_the_env_without_ever_raising(monkeypatch, value, expected):+ monkeypatch.setenv("UNSLOTH_STUDIO_TOOL_APPROVAL_TIMEOUT_S", value)+ assert tool_approvals._park_timeout_from_env() == expected+++def test_an_unset_park_ceiling_is_the_documented_default(monkeypatch):+ monkeypatch.delenv("UNSLOTH_STUDIO_TOOL_APPROVAL_TIMEOUT_S", raising = False)+ assert tool_approvals._park_timeout_from_env() == 300.0+ assert tool_approvals._PARK_TIMEOUT_DEFAULT_S == 300.0+++def test_a_zero_ceiling_denies_at_the_first_poll_not_before_it(monkeypatch):+ """The loop waits before it checks, so 0 is "autonomous", not "instant": a decision landing+ inside the first 500ms still wins. The comment on _PARK_TIMEOUT_DEFAULT_S says so; this pins it,+ because a reader who takes "denies immediately" literally would call the opposite a bug."""+ monkeypatch.setattr(tool_approvals, "_PARK_TIMEOUT_S", 0.0)+ cancel = threading.Event()+ cancel.durable = True+ aid = new_approval_id()+ w = _Waiter("sess", aid, cancel_event = cancel).start()+ time.sleep(0.05)+ assert resolve_tool_decision(aid, "allow", session_id = "sess") is True+ assert w.join(timeout = 3.0) == "allow"+++def test_a_returning_session_past_the_ceiling_cannot_resolve_its_own_approval(monkeypatch):+ """What the user actually experiences at the ceiling: the call is already refused, and the+ Approve they press on return has nothing left to resolve (the route turns this into a 404)."""+ monkeypatch.setattr(tool_approvals, "_PARK_TIMEOUT_S", 0.2)+ cancel = threading.Event()+ cancel.durable = True+ aid = new_approval_id()+ w = _Waiter("sess", aid, cancel_event = cancel).start()+ assert w.join(timeout = 3.0) == "deny"+ assert resolve_tool_decision(aid, "allow", session_id = "sess") is False+++def test_a_pending_approval_reports_pending_and_a_decided_one_does_not():+ """A reopened tab cannot tell a parked call from one the user already answered: both are saved+ as a card with no result and an approval id, because the result only lands with tool_end. This+ is the durable signal that separates them."""+ approval_id = tool_approvals.new_approval_id()+ slot = tool_approvals.begin_tool_decision("sess-a", approval_id)+ assert tool_approvals.tool_decision_is_pending(approval_id, "sess-a") is True++ assert tool_approvals.resolve_tool_decision(approval_id, "allow", session_id = "sess-a") is True+ # Decided but not yet collected by the waiter: the window that would otherwise read as parked.+ assert tool_approvals.tool_decision_is_pending(approval_id, "sess-a") is False++ assert tool_approvals.wait_tool_decision(slot, approval_id, timeout = 30) == "allow"+ # And gone once the waiter has popped its own slot.+ assert tool_approvals.tool_decision_is_pending(approval_id, "sess-a") is False+++def test_the_pending_check_is_session_scoped_and_unguessable():+ approval_id = tool_approvals.new_approval_id()+ tool_approvals.begin_tool_decision("sess-a", approval_id)+ assert tool_approvals.tool_decision_is_pending(approval_id, "sess-b") is False+ assert tool_approvals.tool_decision_is_pending(approval_id, "sess-a") is True+ # No id, no answer: this must never be a way to ask "is anything pending".+ assert tool_approvals.tool_decision_is_pending("", "sess-a") is False+ assert tool_approvals.tool_decision_is_pending(None, "sess-a") is False+ assert tool_approvals.tool_decision_is_pending("not-a-real-id", "sess-a") is False+++# ── The park ceiling counts time with NOBODY WATCHING, not time since the call parked ──+# "Durable" means cancel_on_disconnect is off, not that the tab is gone. Since every+# tool-enabled turn is durable by default, keying the 300s ceiling off the durable marker+# alone put a five-minute deadline on a user who is sitting there reading what the tool+# wants to do, where a browser-owned run gave them the full hour. The gate asks+# state.run_subscribers whether a follower is attached, and re-arms while one is.++[email protected](autouse = True)+def _clear_subscribers():+ run_subscribers.reset_for_tests()+ yield+ run_subscribers.reset_for_tests()+++def test_an_attended_park_does_not_expire_at_the_park_ceiling(monkeypatch):+ """A user watching the run keeps their decision past the park ceiling.++ The regression this pins: with the ceiling keyed on the durable marker alone, an Approve/Deny+ card the user was reading auto-denied at 300s and the model was told they had declined it.+ """+ monkeypatch.setattr(tool_approvals, "_PARK_TIMEOUT_S", 0.2)+ cancel = threading.Event()+ cancel.durable = True+ cancel.durable_run_id = "run-attended"+ aid = new_approval_id()+ w = _Waiter("sess", aid, cancel_event = cancel).start()++ # A follower heartbeat, exactly as the SSE loop in routes/chat_generation_runs.py stamps it.+ for _ in range(6):+ run_subscribers.mark_subscriber_seen("run-attended", "tab-1")+ time.sleep(0.1)+ assert _has_pending(aid), "an attended park must not expire at the park ceiling"++ assert resolve_tool_decision(aid, "allow", session_id = "sess") is True+ assert w.join(timeout = 3.0) == "allow"+++def test_a_park_expires_once_its_followers_go(monkeypatch):+ """The ceiling still releases an abandoned approval: presence is what re-arms it, and it ages out."""+ monkeypatch.setattr(tool_approvals, "_PARK_TIMEOUT_S", 0.2)+ monkeypatch.setattr(run_subscribers, "_ATTENDED_FOR_S", 0.1)+ cancel = threading.Event()+ cancel.durable = True+ cancel.durable_run_id = "run-leaving"+ aid = new_approval_id()+ run_subscribers.mark_subscriber_seen("run-leaving", "tab-1")+ w = _Waiter("sess", aid, cancel_event = cancel).start()+ # Nobody stamps again: the entry ages out and the ceiling runs.+ assert w.join(timeout = 5.0) == "deny"+ assert _wait_until(lambda: not _has_pending(aid))+++def test_an_unattended_park_is_unchanged_without_a_run_id(monkeypatch):+ """A durable event carrying no run id keeps the plain park behaviour, so nothing regresses+ for a producer that never registered one."""+ monkeypatch.setattr(tool_approvals, "_PARK_TIMEOUT_S", 0.2)+ cancel = threading.Event()+ cancel.durable = True+ aid = new_approval_id()+ w = _Waiter("sess", aid, cancel_event = cancel).start()+ assert w.join(timeout = 3.0) == "deny"+++def test_attendance_cannot_hold_an_approval_past_the_absolute_ceiling(monkeypatch):+ """Presence re-arms the park ceiling, never the hour a browser-owned run has always had."""+ monkeypatch.setattr(tool_approvals, "_PARK_TIMEOUT_S", 10.0)+ monkeypatch.setattr(tool_approvals, "_DECISION_TIMEOUT", 0.2)+ cancel = threading.Event()+ cancel.durable = True+ cancel.durable_run_id = "run-forever"+ aid = new_approval_id()+ slot = begin_tool_decision("sess", aid)++ stop = threading.Event()++ def _stamp():+ while not stop.is_set():+ run_subscribers.mark_subscriber_seen("run-forever", "tab-1")+ time.sleep(0.02)++ stamper = threading.Thread(target = _stamp, daemon = True)+ stamper.start()+ try:+ verdict = tool_approvals.wait_tool_decision(slot, aid, cancel_event = cancel)+ reason = tool_approvals.decision_reason(slot)+ finally:+ stop.set()+ stamper.join(timeout = 2.0)+ assert (verdict, reason) == ("deny", tool_approvals.DECISION_EXPIRED)+++# ── An expiry is not the user's decision, and must not be reported as one ──+++def test_the_reason_separates_an_expiry_from_a_deny_from_a_cancel(monkeypatch):+ """The three outcomes used to arrive as one bare "deny", so the loops had no way to tell a+ returning user that nobody answered rather than that they refused."""+ monkeypatch.setattr(tool_approvals, "_PARK_TIMEOUT_S", 0.2)++ # Expired: nobody watching, nobody answering.+ cancel = threading.Event()+ cancel.durable = True+ aid = new_approval_id()+ slot = begin_tool_decision("sess", aid)+ assert tool_approvals.wait_tool_decision(slot, aid, cancel_event = cancel) == "deny"+ assert tool_approvals.decision_reason(slot) == tool_approvals.DECISION_EXPIRED++ # Cancelled: an explicit Stop, or the sweeper settling a lease-expired run.+ cancel2 = threading.Event()+ cancel2.durable = True+ aid2 = new_approval_id()+ slot2 = begin_tool_decision("sess", aid2)+ cancel2.set()+ assert tool_approvals.wait_tool_decision(slot2, aid2, cancel_event = cancel2) == "deny"+ assert tool_approvals.decision_reason(slot2) == tool_approvals.DECISION_CANCELLED++ # Answered: the user actually pressed Deny.+ aid3 = new_approval_id()+ slot3 = begin_tool_decision("sess", aid3)+ resolve_tool_decision(aid3, "deny", session_id = "sess")+ assert tool_approvals.wait_tool_decision(slot3, aid3) == "deny"+ assert tool_approvals.decision_reason(slot3) == tool_approvals.DECISION_ANSWERED+++def test_the_expiry_message_does_not_claim_the_user_decided():+ assert TOOL_APPROVAL_EXPIRED_MESSAGE != TOOL_REJECTED_MESSAGE+ assert TOOL_APPROVAL_EXPIRED_MESSAGE.strip()+ lowered = TOOL_APPROVAL_EXPIRED_MESSAGE.lower()+ assert "declin" not in lowered, "an expiry must not be reported as the user declining"+ assert "the user" not in lowered, "an expiry is not a statement about the user"+++def test_the_waiter_keeps_its_name_signature_and_bare_verdict(monkeypatch):+ """The reason rides on the slot precisely so this stays true: the three tool loops are+ monkeypatched by name (core.inference.llama_cpp.wait_tool_decision and friends), and a fake+ that returns a bare string and never touches slot["reason"] must keep working."""+ monkeypatch.setattr(tool_approvals, "_PARK_TIMEOUT_S", 0.2)+ aid = new_approval_id()+ slot = begin_tool_decision("sess", aid)+ resolve_tool_decision(aid, "allow", session_id = "sess")+ assert wait_tool_decision(slot, aid) == "allow"+++# ── The presence registry itself ──+++def test_presence_expires_by_age_so_a_leaked_stamp_cannot_park_forever(monkeypatch):+ monkeypatch.setattr(run_subscribers, "_ATTENDED_FOR_S", 0.1)+ run_subscribers.mark_subscriber_seen("run-x", "tab-1")+ assert run_subscribers.is_attended("run-x") is True+ time.sleep(0.2)+ assert run_subscribers.is_attended("run-x") is False+++def test_presence_is_per_run_and_an_empty_id_is_never_attended():+ run_subscribers.mark_subscriber_seen("run-a", "tab-1")+ assert run_subscribers.is_attended("run-a") is True+ assert run_subscribers.is_attended("run-b") is False+ run_subscribers.mark_subscriber_seen("", "tab-1")+ assert run_subscribers.is_attended("") is False+++def test_a_departing_follower_drops_its_stamp_promptly():+ run_subscribers.mark_subscriber_seen("run-c", "tab-1")+ assert run_subscribers.is_attended("run-c") is True+ run_subscribers.subscriber_departed("run-c", "tab-1")+ assert run_subscribers.is_attended("run-c") is False+++# ── One run, several followers: a tab closing must not clear another tab's stamp ──+# Two tabs on the same thread, or a reconnect whose replacement stream attaches before the old+# one finishes unwinding, both put two followers on one run. With a single stamp per run, either+# one's cleanup deleted the other's heartbeat, and the survivor does not stamp again until its+# event wait turns over (15s). Under a park ceiling shorter than that -- 0 is supported and+# tested above -- a call a second tab was watching could be denied.+++def test_one_tab_closing_leaves_another_tabs_attendance_intact():+ run_subscribers.mark_subscriber_seen("run-two-tabs", "tab-a")+ run_subscribers.mark_subscriber_seen("run-two-tabs", "tab-b")+ assert run_subscribers.attendance_for_tests("run-two-tabs") == 2++ run_subscribers.subscriber_departed("run-two-tabs", "tab-a")+ assert (+ run_subscribers.is_attended("run-two-tabs") is True+ ), "tab-b is still watching; its heartbeat must survive tab-a's cleanup"+ assert run_subscribers.attendance_for_tests("run-two-tabs") == 1++ run_subscribers.subscriber_departed("run-two-tabs", "tab-b")+ assert run_subscribers.is_attended("run-two-tabs") is False+ assert run_subscribers.attendance_for_tests("run-two-tabs") == 0+++def test_an_attended_park_survives_a_second_tab_closing(monkeypatch):+ """The reachable consequence, end to end: a short ceiling plus two tabs.++ At 0.2s the ceiling is well under the 15s a surviving follower may take to stamp again, which+ is exactly the window the single-stamp version left open.+ """+ monkeypatch.setattr(tool_approvals, "_PARK_TIMEOUT_S", 0.2)+ cancel = threading.Event()+ cancel.durable = True+ cancel.durable_run_id = "run-two-tabs-park"+ aid = new_approval_id()++ run_subscribers.mark_subscriber_seen("run-two-tabs-park", "tab-a")+ run_subscribers.mark_subscriber_seen("run-two-tabs-park", "tab-b")+ w = _Waiter("sess", aid, cancel_event = cancel).start()++ # tab-a goes away; tab-b keeps watching but does not stamp again for a while.+ run_subscribers.subscriber_departed("run-two-tabs-park", "tab-a")+ time.sleep(0.8)+ assert _has_pending(aid), "the park expired while a second tab was still attended"++ assert resolve_tool_decision(aid, "allow", session_id = "sess") is True+ assert w.join(timeout = 3.0) == "allow"+++def test_a_follower_only_clears_its_own_stamp_not_the_run():+ run_subscribers.mark_subscriber_seen("run-solo", "tab-a")+ # A departure naming a follower that was never stamped must not wipe the run.+ run_subscribers.subscriber_departed("run-solo", "tab-ghost")+ assert run_subscribers.is_attended("run-solo") is True+ run_subscribers.subscriber_departed("run-solo", "tab-a")+ assert run_subscribers.is_attended("run-solo") is False+++# ── An attended park must renew the RUN's lease, not only its own counter ──+# Parking makes no progress, so the sweeper settles the run at its lease timeout+# (core.inference.chat_generation_runs._LEASE_TIMEOUT_SECONDS, 1200s by default) and cancels the+# wait. Re-arming `waited` alone therefore capped an attended deliberation at ~20 minutes and+# ended it as a cancel, while the docstring promised the full _DECISION_TIMEOUT.+++def test_an_attended_park_renews_the_runs_lease(monkeypatch):+ monkeypatch.setattr(tool_approvals, "_PARK_TIMEOUT_S", 5.0)+ monkeypatch.setattr(tool_approvals, "_LEASE_RENEW_EVERY_S", 0.0)+ renewals = []+ cancel = threading.Event()+ cancel.durable = True+ cancel.durable_run_id = "run-lease"+ cancel.renew_lease = lambda: renewals.append(time.monotonic())+ aid = new_approval_id()++ run_subscribers.mark_subscriber_seen("run-lease", "tab-1")+ w = _Waiter("sess", aid, cancel_event = cancel).start()+ for _ in range(6):+ run_subscribers.mark_subscriber_seen("run-lease", "tab-1")+ time.sleep(0.1)++ assert renewals, (+ "an attended park renewed its own counter but never the run's lease, so the sweeper "+ "settles the run at the lease timeout and cancels the decision"+ )+ assert resolve_tool_decision(aid, "allow", session_id = "sess") is True+ assert w.join(timeout = 3.0) == "allow"+++def test_an_unattended_park_does_not_renew_the_lease(monkeypatch):+ """The sweeper is the backstop for an abandoned run: holding its lease open would remove it."""+ monkeypatch.setattr(tool_approvals, "_PARK_TIMEOUT_S", 0.2)+ monkeypatch.setattr(tool_approvals, "_LEASE_RENEW_EVERY_S", 0.0)+ renewals = []+ cancel = threading.Event()+ cancel.durable = True+ cancel.durable_run_id = "run-abandoned"+ cancel.renew_lease = lambda: renewals.append(1)+ aid = new_approval_id()++ w = _Waiter("sess", aid, cancel_event = cancel).start()+ assert w.join(timeout = 3.0) == "deny"+ assert renewals == [], "nobody was watching; the lease must be allowed to lapse"+++def test_lease_renewal_is_throttled_not_once_per_poll(monkeypatch):+ """The poll is twice a second and this is a database write, so it is rate limited."""+ monkeypatch.setattr(tool_approvals, "_PARK_TIMEOUT_S", 5.0)+ monkeypatch.setattr(tool_approvals, "_LEASE_RENEW_EVERY_S", 30.0)+ renewals = []+ cancel = threading.Event()+ cancel.durable = True+ cancel.durable_run_id = "run-throttle"+ cancel.renew_lease = lambda: renewals.append(1)+ aid = new_approval_id()++ run_subscribers.mark_subscriber_seen("run-throttle", "tab-1")+ w = _Waiter("sess", aid, cancel_event = cancel).start()+ for _ in range(8):+ run_subscribers.mark_subscriber_seen("run-throttle", "tab-1")+ time.sleep(0.1)+ # Well inside one 30s window: the first poll renews, the rest must not.+ assert len(renewals) == 1, f"expected a single renewal in the first window, got {len(renewals)}"+ assert resolve_tool_decision(aid, "allow", session_id = "sess") is True+ assert w.join(timeout = 3.0) == "allow"+++def test_a_waiter_with_no_renew_hook_still_works(monkeypatch):+ """Backwards compatible: a cancel_event from any other producer carries no renew_lease."""+ monkeypatch.setattr(tool_approvals, "_PARK_TIMEOUT_S", 0.2)+ cancel = threading.Event()+ cancel.durable = True+ cancel.durable_run_id = "run-no-hook"+ aid = new_approval_id()+ run_subscribers.mark_subscriber_seen("run-no-hook", "tab-1")+ w = _Waiter("sess", aid, cancel_event = cancel).start()+ time.sleep(0.6)+ assert _has_pending(aid), "attendance must still hold the park open without a renew hook"+ assert resolve_tool_decision(aid, "allow", session_id = "sess") is True+ assert w.join(timeout = 3.0) == "allow"+++# ── Attendance is per ACCOUNT as well as per run ──+# studio.db is per-account (utils.paths.storage_roots.studio_db_path -> account_path) and the run id+# comes from the client (CreateChatGenerationRun.runId), so two accounts on one managed install can+# hold the same id. Keyed on the bare id, one tenant's follower answered for another's run: it would+# hold a stranger's approval open past its ceiling and keep renewing that run's lease.+++def test_attendance_does_not_cross_accounts():+ run_subscribers.mark_subscriber_seen("shared-id", "tab-a", "account-a")+ assert run_subscribers.is_attended("shared-id", "account-a") is True+ assert (+ run_subscribers.is_attended("shared-id", "account-b") is False+ ), "another account's follower must not report this run as attended"+ # And the no-account scope is its own, not a wildcard that matches everyone.+ assert run_subscribers.is_attended("shared-id") is False+++def test_a_departure_only_clears_its_own_accounts_stamp():+ run_subscribers.mark_subscriber_seen("shared-id", "tab-a", "account-a")+ run_subscribers.mark_subscriber_seen("shared-id", "tab-b", "account-b")+ run_subscribers.subscriber_departed("shared-id", "tab-a", "account-a")+ assert run_subscribers.is_attended("shared-id", "account-a") is False+ assert run_subscribers.is_attended("shared-id", "account-b") is True+++def test_another_accounts_follower_cannot_hold_this_park_open(monkeypatch):+ """The reachable consequence: account B watching its own run must not keep account A's+ approval alive, nor keep renewing A's lease."""+ monkeypatch.setattr(tool_approvals, "_PARK_TIMEOUT_S", 0.2)+ monkeypatch.setattr(tool_approvals, "_LEASE_RENEW_EVERY_S", 0.0)+ renewals = []+ cancel = threading.Event()+ cancel.durable = True+ cancel.durable_run_id = "shared-id"+ cancel.durable_account_id = "account-a"+ cancel.renew_lease = lambda: renewals.append(1)+ aid = new_approval_id()++ # Only account B is watching, on ITS run that happens to share the id.+ run_subscribers.mark_subscriber_seen("shared-id", "tab-b", "account-b")+ w = _Waiter("sess", aid, cancel_event = cancel).start()+ assert (+ w.join(timeout = 3.0) == "deny"+ ), "account A's approval was held open by account B's follower"+ assert renewals == [], "account A's lease was renewed on the strength of account B's follower"diff --git a/studio/frontend/src/components/assistant-ui/tool-confirmation-controls.tsx b/studio/frontend/src/components/assistant-ui/tool-confirmation-controls.tsxindex 30a5053cf28..23748d8e195 100644--- a/studio/frontend/src/components/assistant-ui/tool-confirmation-controls.tsx+++ b/studio/frontend/src/components/assistant-ui/tool-confirmation-controls.tsx@@ -4,7 +4,10 @@"use client";import { Button } from "@/components/ui/button";-import { resolveToolConfirmation } from "@/features/chat/api/chat-api";+import {+ resolveToolConfirmation,+ ToolApprovalGoneError,+} from "@/features/chat/api/chat-api";import { useChatRuntimeStore } from "@/features/chat/stores/chat-runtime-store";import {COMPOSER_INPUT_SELECTOR,@@ -58,7 +61,11 @@ export function ToolConfirmationControls({const [decided, setDecided] = useState(false);const [pending, setPending] = useState<"allow" | "deny" | null>(null);- const [failed, setFailed] = useState(false);+ // "retry" is a post that failed on the way out and is worth pressing again; "gone" is an approval+ // the backend is no longer holding, where pressing again can only fail the same way. They were one+ // flag, and the shared copy told the user to retry something that could never succeed.+ const [failure, setFailure] = useState<"retry" | "gone" | null>(null);+ const failed = failure !== null;// Still awaiting our decision: a gated pending entry exists, the tool has// not produced a result, and the card is in its running state.@@ -69,10 +76,10 @@ export function ToolConfirmationControls({const showControls = awaiting && !decided;const resolve = useCallback(- async (decision: "allow" | "deny") => {+ async (decision: "allow" | "deny", alsoAlways = false) => {if (!toolCallId || !confirmation) return;setPending(decision);- setFailed(false);+ setFailure(null);try {const ok = await resolveToolConfirmation(confirmation.sessionId,@@ -80,20 +87,31 @@ export function ToolConfirmationControls({decision,);if (ok) {+ // The session-wide grant is recorded only once the backend has actually taken the+ // decision. Granting it up front on the click meant a press that visibly failed still+ // silently auto-approved this tool for every later call in the session.+ if (alsoAlways && autoAllowKey) allowToolAlways(autoAllowKey, toolName);// Only hide the controls once the backend confirms it matched the pending call --// otherwise the generation would stay blocked with no way to retry.setDecided(true);clearToolConfirmation(toolCallId);} else {- setFailed(true);+ setFailure("retry");}- } catch {- setFailed(true);+ } catch (err) {+ setFailure(err instanceof ToolApprovalGoneError ? "gone" : "retry");} finally {setPending(null);}},- [toolCallId, confirmation, clearToolConfirmation],+ [+ toolCallId,+ confirmation,+ clearToolConfirmation,+ allowToolAlways,+ autoAllowKey,+ toolName,+ ],);// Tools the user marked "Always allow" (this session) approve themselves.@@ -166,7 +184,7 @@ export function ToolConfirmationControls({<div className="flex flex-wrap items-center gap-2 pt-1"><Buttonsize="xs"- disabled={pending !== null}+ disabled={pending !== null || failure === "gone"}onClick={() => void resolve("allow")}>Allow@@ -174,25 +192,24 @@ export function ToolConfirmationControls({<Buttonsize="xs"variant="outline"- disabled={pending !== null}- onClick={() => {- if (autoAllowKey) allowToolAlways(autoAllowKey, toolName);- void resolve("allow");- }}+ disabled={pending !== null || failure === "gone"}+ onClick={() => void resolve("allow", true)}>Always allow</Button><Buttonsize="xs"variant="destructive"- disabled={pending !== null}+ disabled={pending !== null || failure === "gone"}onClick={() => void resolve("deny")}>Deny</Button>- {failed ? (+ {failure !== null ? (<span className="text-xs text-destructive">- Could not send your decision. Try again.+ {failure === "gone"+ ? "This request is no longer waiting for an answer."+ : "Could not send your decision. Try again."}</span>) : null}</div>diff --git a/studio/frontend/src/features/chat/api/chat-adapter.ts b/studio/frontend/src/features/chat/api/chat-adapter.tsindex d008a60abcd..46c8cb2fb9e 100644--- a/studio/frontend/src/features/chat/api/chat-adapter.ts+++ b/studio/frontend/src/features/chat/api/chat-adapter.ts@@ -360,6 +360,7 @@ import {followChatGenerationRun,supportsChatGenerationRuns,} from "./chat-generation-api";+import { isDurableRunCandidate, turnRequiresLegacyStream } from "./durable-gate";// Small models (<=9B) answer from memory, so "auto" forces retrieval for them.const AUTOINJECT_AUTO_MAX_SIZE_B = 9;@@ -5200,21 +5201,33 @@ export function createOpenAIStreamAdapter(const generationUserMessage = [...survivingMessages].reverse().find((message) => message.role === "user");- const generationCandidate = Boolean(- !isExternalRequest &&- !activeModel?.isAudio &&- !runtime.loadedIsDiffusion &&- !imageBase64 &&- !audioBase64 &&- !videoBase64 &&- // Continue yields the seeded partial before the request starts so the autosave lands before- // admission, which 409s a substantive placeholder. Continuations keep the legacy stream.- !continuation &&- resolvedThreadId &&- !isThreadIncognito(resolvedThreadId) &&- unstable_assistantMessageId &&- generationUserMessage,++ // Durability gate keys on THIS turn's attachments only. The scans above walk post-prune history so an old+ // refused turn cannot mis-attribute media onto the next one - correct for building the request payload, but it+ // also meant one screenshot anywhere in a thread excluded every later text-only turn from the durable path.+ // A turn that itself carries media still stays on the subscriber-owned stream; a text follow-up does not.+ const currentTurnMessages = [generationUserMessage] as unknown as Parameters<+ typeof findLatestUserImageBase64+ >[0];+ const currentTurnCarriesMedia = Boolean(+ findLatestUserImageBase64(currentTurnMessages) ||+ findLatestUserAudioBase64(currentTurnMessages, !queuedRunSettings && !continuation) ||+ findLatestUserVideoBase64(currentTurnMessages),);+ const generationCandidate = isDurableRunCandidate({+ externalProvider: isExternalRequest,+ modelIsAudio: activeModel?.isAudio,+ loadedIsDiffusion: runtime.loadedIsDiffusion,+ // Turn-scoped, not thread-scoped: see currentTurnCarriesMedia above.+ turnCarriesMedia: currentTurnCarriesMedia,+ // Continue yields the seeded partial before the request starts so the autosave lands before+ // admission, which 409s a substantive placeholder. Continuations keep the legacy stream.+ continuation,+ threadId: resolvedThreadId,+ incognito: resolvedThreadId ? isThreadIncognito(resolvedThreadId) : false,+ assistantMessageId: unstable_assistantMessageId,+ hasUserMessage: Boolean(generationUserMessage),+ });let generationDecision: "pending" | "durable" | "legacy" =generationCandidate ? "pending" : "legacy";let generationRun: ChatGenerationRun | null = null;@@ -5416,8 +5429,20 @@ export function createOpenAIStreamAdapter(...reasoningDurationTracker.metadata(),openaiCodexReasoning: codexReasoningLedger,contextTruncation,+ // A legacy (browser-tool / attachment / incognito) run that ends because you closed the tab has no+ // server-side run to resume from, so its last streamed yield is what persists. Mark it an interruption+ // — partial kept + Resume — instead of a silent blank/ambiguous state. Durable runs keep "cancelled":+ // their reconnect path marks state via recovery, and an explicit Stop still reads as cancelled below.+ // A window the provider reported as full outranks either guess (utils/continuation.ts).incomplete: {- reason: resolveIncompleteReason("cancelled" as const, contextWindowExceeded),+ reason: resolveIncompleteReason(+ // Once an explicit Stop has latched its reason, streamed yields that+ // still go out carry it -- a stopped legacy turn must persist as+ // cancelled, not read back as a walk-away interruption.+ incompleteReason ??+ (generationDecision === "durable" ? "cancelled" : "interrupted"),+ contextWindowExceeded,+ ),},...generationCustom(),});@@ -5915,6 +5940,9 @@ export function createOpenAIStreamAdapter(return;}generationStopRequested = true;+ // An explicit Stop is a cancel, not a walk-away interruption: override the provisional reason so a+ // deliberate Stop still reads as "cancelled" even though streamed yields carried the legacy default.+ incompleteReason = "cancelled";const stopPlan = chatGenerationStopPlan(generationDecision,generationRunId,@@ -6386,9 +6414,16 @@ export function createOpenAIStreamAdapter(...(params.seed == null || !modelReadsSamplingSeed(activeModel)? {}: { seed: params.seed }),- image_base64: imageBase64,- audio_base64: audioBase64,- video_base64: videoBase64,+ // Turn-scoped, not thread-scoped. These are the CURRENT turn's attachment channel; history media rides+ // along inside messages[].content. Sending a stale screenshot from an earlier turn made the backend see a+ // non-empty media field on every later text-only turn and refuse the durable run with 400 "Media chat+ // runs use the legacy streaming path" - which is what kept these turns on the cancel-on-disconnect stream.+ image_base64: findLatestUserImageBase64(currentTurnMessages),+ audio_base64: findLatestUserAudioBase64(+ currentTurnMessages,+ !queuedRunSettings && !continuation,+ ),+ video_base64: findLatestUserVideoBase64(currentTurnMessages),cancel_id: cancelId,...(sandboxSessionId ? { session_id: sandboxSessionId } : {}),...(resolvedThreadId ? { thread_id: resolvedThreadId } : {}),@@ -6498,14 +6533,14 @@ export function createOpenAIStreamAdapter(requestedMaxTokens = requestPayload.max_tokens;await ThreadAutosaveHandle.awaitFirstSave(resolvedThreadId);if (generationDecision === "pending") {- const clientTools = (- requestPayload as unknown as { tools?: unknown }- ).tools;- if (- requestPayload.enable_tools === true ||- (Array.isArray(clientTools) && clientTools.length > 0)- ) {- // Confirmation and browser-executed tool chains still use the subscriber-owned stream.+ // Keyed on `enabled_tools`, never on `requestPayload.tools`: see durable-gate.ts. Keying this on+ // `tools` read as "no tools" on the local path and as "browser tools" for every passthrough turn that+ // carried a schema catalog, silently forcing those turns back onto the cancel-on-disconnect path.+ if (turnRequiresLegacyStream(requestPayload)) {+ // Only a tool chain the BROWSER must execute still needs the live tab: there is no server-side+ // executor to run it while you're away. Everything else is durable like plain text - an auto/bypass+ // loop runs to completion while you're away, and a confirm ("ask") call parks on its approval_id+ // (see tool_approvals.wait_tool_decision) and is resolved by id on return.generationDecision = "legacy";} else {const admission = explicitStopSignal(runSignal);@@ -8171,7 +8206,11 @@ export function createOpenAIStreamAdapter(});}}- if (!abortSignal.aborted) {+ // An explicit Stop is an abort too, but it must persist its reason instead of+ // leaving the last streamed yield's label standing: the replacement yield+ // below carries the latched "cancelled" (the durable path reads the latch at+ // 5098-101 style already; legacy only got it via this gate staying shut).+ if (!abortSignal.aborted || generationStopRequested) {closeReasoningContent();const partialText = mergeContinuation(cumulativeText, { final: true });const partialContent = buildAssistantContent(partialText);@@ -8194,12 +8233,15 @@ export function createOpenAIStreamAdapter(// said why the model stopped.incomplete: {reason: resolveIncompleteReason(- err instanceof GenerationLengthError- ? ("length" as const)- : err instanceof ChatGenerationTerminalError &&- err.generationStatus === "cancelled"- ? ("cancelled" as const)- : ("interrupted" as const),+ // An explicit Stop latched incompleteReason = "cancelled" at the abort+ // handler; that outranks the error-derived guess below.+ incompleteReason ??+ (err instanceof GenerationLengthError+ ? ("length" as const)+ : err instanceof ChatGenerationTerminalError &&+ err.generationStatus === "cancelled"+ ? ("cancelled" as const)+ : ("interrupted" as const)),contextWindowExceeded,),},diff --git a/studio/frontend/src/features/chat/api/chat-api.ts b/studio/frontend/src/features/chat/api/chat-api.tsindex eaf52b0ae80..e1500ec1a5a 100644--- a/studio/frontend/src/features/chat/api/chat-api.ts+++ b/studio/frontend/src/features/chat/api/chat-api.ts@@ -474,9 +474,22 @@ export async function unloadModel(payload: UnloadModelRequest): Promise<void> {dismissCarveoutAdviceForModel(payload.model_path);}+/** The approval this decision was for is no longer waiting: it expired unanswered, the run was+ * cancelled, or the backend restarted and took its in-memory slot with it.+ *+ * Distinct from a transport failure because the advice is the opposite: a failed post is worth+ * retrying, this can never succeed. */+export class ToolApprovalGoneError extends Error {+ constructor(message = "No pending tool call confirmation") {+ super(message);+ this.name = "ToolApprovalGoneError";+ }+}+/** Allow or deny a tool call paused awaiting user confirmation, identified by the backend* `approvalId` echoed in the tool_start event, with `sessionId` as a scope check. Resolves to- * true only when the backend matched a pending call. */+ * true only when the backend matched a pending call, and throws `ToolApprovalGoneError` when the+ * slot has already gone. */export async function resolveToolConfirmation(sessionId: string,approvalId: string,@@ -491,6 +504,8 @@ export async function resolveToolConfirmation(decision,}),});+ // Ahead of parseJsonOrThrow, which folds every non-ok status into one bare Error.+ if (response.status === 404) throw new ToolApprovalGoneError();const parsed = await parseJsonOrThrow<{ resolved?: boolean }>(response);return parsed.resolved === true;}diff --git a/studio/frontend/src/features/chat/api/chat-generation-api.ts b/studio/frontend/src/features/chat/api/chat-generation-api.tsindex a729f09be80..fd88c25d7ec 100644--- a/studio/frontend/src/features/chat/api/chat-generation-api.ts+++ b/studio/frontend/src/features/chat/api/chat-generation-api.ts@@ -60,15 +60,37 @@ export interface ChatGenerationRunUpdate {export function normalizeChatGenerationChunkPayload(payload: OpenAIChatChunk | Record<string, unknown>,): OpenAIChatChunk | Record<string, unknown> {- if (- payload !== null &&- typeof payload === "object" &&- "type" in payload &&- payload.type === "reasoning_summary"- ) {- return {- _reasoningDurationMs: (payload as { duration_ms?: unknown }).duration_ms,- } as unknown as OpenAIChatChunk;+ if (payload !== null && typeof payload === "object" && "type" in payload) {+ const frameType = (payload as { type?: unknown }).type;+ // Relay server-side reasoning duration.+ if (frameType === "reasoning_summary") {+ return {+ _reasoningDurationMs: (payload as { duration_ms?: unknown }).duration_ms,+ } as unknown as OpenAIChatChunk;+ }+ // The producer persists every decoded SSE data frame as a `chunk` event and _SSEDecoder drops the+ // SSE event field, so persisted tool/media control frames arrive here indistinguishable from OpenAI+ // chunks. Re-tag them exactly as the legacy stream (chat-api.ts) does before yielding, or a resumed+ // run replays raw {type} payloads into consumers that only read `_tool*`/`_diffusionFrame` and tool+ // activity silently vanishes.+ if (frameType === "tool_status") {+ return {+ _toolStatus: (payload as { content?: unknown }).content ?? "",+ } as unknown as OpenAIChatChunk;+ }+ if (+ frameType === "tool_start" ||+ frameType === "tool_end" ||+ frameType === "tool_output" ||+ frameType === "tool_args"+ ) {+ // tool_start/end carry full input/output; tool_output streams incremental stdout and+ // tool_args streams the call arguments live. The consumer accumulates by tag, so keep the frame whole.+ return { _toolEvent: payload } as unknown as OpenAIChatChunk;+ }+ if (frameType === "diffusion_frame") {+ return { _diffusionFrame: payload } as unknown as OpenAIChatChunk;+ }}return payload;}@@ -116,6 +138,11 @@ export function isLegacyFallbackChatGenerationAdmissionError((error instanceof ChatGenerationApiError &&error.status === 400 &&error.message === "Credentials cannot be persisted") ||+ // A media-bearing payload is a POLICY rejection meaning "use the legacy stream", the same class as the tool-enabled+ // case below - not an error to surface. Without this it rethrows and the turn dies with "An error occurred".+ (error instanceof ChatGenerationApiError &&+ error.status === 400 &&+ error.message === "Media chat runs use the legacy streaming path") ||(error instanceof ChatGenerationApiError &&error.status === 404 &&error.message === "Thread not found") ||@@ -239,6 +266,30 @@ export async function getActiveChatGenerationRuns(return (await json<{ runs: ChatGenerationRun[] }>(response)).runs ?? [];}+/** Whether a parked approval is still waiting on a human.+ *+ * A reopened tab cannot tell this from the saved card: a call the user already answered looks+ * exactly like one still parked, since the result only arrives with tool_end. Answering from the+ * server is the only way to avoid restoring Approve/Deny over a call that is already executing.+ *+ * An older backend has no such route, so a 404 or 405 means "cannot tell" rather than "answered",+ * and the caller keeps its previous arm-everything behaviour instead of silently dropping buttons.+ */+export async function toolApprovalIsPending(+ approvalId: string,+ sessionId: string,+ signal?: AbortSignal,+): Promise<boolean> {+ const response = await authFetch("/api/inference/tool-approval-status", {+ method: "POST",+ headers: { "Content-Type": "application/json" },+ body: JSON.stringify({ approval_id: approvalId, session_id: sessionId }),+ signal,+ });+ if (response.status === 404 || response.status === 405) return true;+ return (await json<{ pending?: boolean }>(response)).pending !== false;+}+export async function createChatGenerationRun(input: CreateChatGenerationRunInput,): Promise<ChatGenerationRun> {diff --git a/studio/frontend/src/features/chat/api/durable-gate.ts b/studio/frontend/src/features/chat/api/durable-gate.tsnew file mode 100644index 00000000000..89f61aabde3--- /dev/null+++ b/studio/frontend/src/features/chat/api/durable-gate.ts@@ -0,0 +1,81 @@+// SPDX-License-Identifier: AGPL-3.0-only+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++/**+ * Tools the BROWSER has to execute itself, so a turn that uses one genuinely cannot proceed once the tab is gone+ * and must stay on the subscriber-owned (cancel-on-disconnect) stream.+ *+ * Intentionally empty: every tool Studio runs for a local model - web_search, web_fetch, python, terminal,+ * edit_file, render_html, image_generation, MCP - is executed by the SERVER and streams as chunk events, so it is+ * durable like plain text. The set exists to name the exception the durable gate keys on, not to re-list the+ * server's own tools. Add a name here only if a tool truly needs a live tab.+ */+export const BROWSER_EXECUTED_TOOLS: ReadonlySet<string> = new Set<string>([]);++/**+ * Whether this turn must fall back to the legacy stream that cancels when the browser disconnects.+ *+ * The discriminating field is `enabled_tools` - the resolved list of server-executed tools for the turn.+ * `requestPayload.tools` does NOT carry this information: on the local path the key is absent entirely, and on the+ * passthrough/external-provider path it is the caller's own OpenAI/Anthropic schema catalog (see+ * backend/routes/inference.py:_passthrough_client_tools) - a server-executed catalog in both cases. Keying the gate+ * off `tools` therefore read as "no tools" for local turns and as "browser tools!" for every passthrough turn that+ * carried a catalog, which silently forced those turns back onto the cancel-on-disconnect path.+ */+export function turnRequiresLegacyStream(requestPayload: unknown): boolean {+ const enabled = (requestPayload as { enabled_tools?: unknown } | undefined)?.enabled_tools;+ return Array.isArray(enabled) && enabled.some((name) => BROWSER_EXECUTED_TOOLS.has(String(name)));+}++/**+ * Everything the durability gate reads, as plain values.+ *+ * Each field is exactly what the adapter already had in hand at the gate: a resolved model flag, a resolved thread+ * id, THIS turn's attachment scan. Nothing here resolves anything itself - that is what lets the whole gate be a+ * truth table instead of a grep over the adapter's source.+ */+export type DurableRunCandidate = {+ /** A passthrough/external-provider turn: its own client owns the stream, so there is nothing to make durable. */+ externalProvider?: boolean;+ /** The resolved model is an audio model - it runs on its own legacy path regardless of the gate. */+ modelIsAudio?: boolean;+ /** A diffusion model is loaded: that path has no durable run to join. */+ loadedIsDiffusion?: boolean;+ /** THIS turn carries an attachment (image/audio/video), scanned out of the current turn's message alone.+ * A media turn stays on the subscriber-owned stream; a text follow-up to an earlier screenshot does not. */+ turnCarriesMedia?: boolean;+ /** Continue: the seeded partial is autosaved before the request starts, and admission 409s a placeholder that+ * already has content - which is not one of the errors that falls back, so the turn would just fail. The adapter+ * holds a request object here, not a flag; what the gate reads is only whether one is present. */+ continuation?: unknown;+ /** The thread the run belongs to. No thread id means nowhere to reattach to, so nothing to make durable. */+ threadId?: string | null;+ /** That thread is incognito: its runs are not persisted, so a run has no stored history to resume from. */+ incognito?: boolean;+ /** The assistant message the run writes into. Without one there is no row for a follower to reattach to. */+ assistantMessageId?: string | null;+ /** The turn's user message exists - the run has something to answer. */+ hasUserMessage?: boolean;+};++/**+ * Whether THIS turn is a candidate for a durable (server-owned, reconnectable) generation run.+ *+ * Extracted from the adapter unchanged: every term below is one `&&` of the conjunction it was written as. It lives+ * here so the gate can be tested as what it is - a truth table over plain values - instead of by regexing the+ * adapter's source and hoping a token keeps its spelling.+ */+export function isDurableRunCandidate(input: DurableRunCandidate): boolean {+ return Boolean(+ !input.externalProvider &&+ input.modelIsAudio !== true &&+ input.loadedIsDiffusion !== true &&+ // Turn-scoped, not thread-scoped: a stale blob from an earlier turn must not refuse this one.+ input.turnCarriesMedia !== true &&+ !input.continuation &&+ input.threadId &&+ !input.incognito &&+ input.assistantMessageId &&+ input.hasUserMessage,+ );+}diff --git a/studio/frontend/src/features/chat/runtime-provider.tsx b/studio/frontend/src/features/chat/runtime-provider.tsxindex 18f2a6d81dd..54632af7eab 100644--- a/studio/frontend/src/features/chat/runtime-provider.tsx+++ b/studio/frontend/src/features/chat/runtime-provider.tsx@@ -54,6 +54,7 @@ import {ChatGenerationStalledError,followChatGenerationRun,isTerminalChatGenerationRun,+ toolApprovalIsPending,} from "./api/chat-generation-api";import {TEXT_ATTACHMENT_ACCEPT,@@ -869,7 +870,25 @@ function scheduleGenerationRecovery(if (!Number.isSafeInteger(cursor) || cursor < 0) cursor = 0;const stored = generationRawContent(storedMessage.content);const carried = stored.carried;- const toolRecovery = createGenerationToolRecovery(carried, runId, cursor);+ // A durable tool turn can be parked on an approval when the tab closes; the backend keeps waiting+ // for the returning session, so this tab has to be able to answer. Keyed and scoped exactly as the+ // live stream keys it (chat-adapter's toolConfirmationScopeId), so a card armed here and a card+ // armed there are the same card to the store and to "Always allow".+ const toolRecovery = createGenerationToolRecovery(carried, runId, cursor, {+ register: (partId, approvalId, sessionId) =>+ useChatRuntimeStore+ .getState()+ .setToolConfirmation(+ partId,+ approvalId,+ sessionId,+ `${sessionId || "_default"}:${threadId}`,+ ),+ resolve: (partId) =>+ useChatRuntimeStore.getState().clearToolConfirmation(partId),+ });+ // Settled before disarmAll so an arm cannot land after the run is over.+ let seededApprovals: Promise<void> | null = null;let { raw, reasoningOpen } = stored;let parseThink = metadata.parseThinkTags !== false;let completionTokens: number | undefined;@@ -1046,6 +1065,34 @@ function scheduleGenerationRecovery(raw = lastRequestMessage.content;}}+ // The run's session is known now, and the decision is resolved against it. A call that+ // parked before the tab closed has no frame left to re-fold, so this is the only thing+ // that puts its Approve/Deny back in front of the user.+ //+ // Only while the run can still be answered. A run that settled while parked (a backend+ // restart terminalises surviving rows) has no _pending slot left, and no tool_end is+ // coming to disarm the card, so arming from the seed would leave permanent Approve/Deny+ // buttons whose confirm can only 404.+ if (!isTerminalChatGenerationRun(update.run)) {+ // Deliberately NOT awaited: this generator is suspended at the snapshot yield, so+ // awaiting holds /events closed, and /events is the only thing that marks the run+ // attended server-side (state/run_subscribers.py). A tab returning near the park+ // ceiling would expire its approval during the very request asking whether it is+ // still pending. Settled at the end of the loop instead.+ seededApprovals = toolRecovery.armSeededApprovals(+ update.run.requestPayload?.session_id,+ (approvalId) =>+ toolApprovalIsPending(+ approvalId,+ typeof update.run.requestPayload?.session_id === "string"+ ? update.run.requestPayload.session_id+ : "",+ ),+ );+ // armSeededApprovals already treats an unanswerable check as "still parked"; this+ // only stops an unhandled rejection before the join below reaches it.+ seededApprovals.catch(() => {});+ }if (typeof metadata.parseThinkTags !== "boolean") {parseThink = requestParsesThinkTags(update.run.requestPayload);currentMetadata = {@@ -1151,6 +1198,8 @@ function scheduleGenerationRecovery(await publish(update.run);}if (isTerminalChatGenerationRun(update.run)) {+ // The disarm itself now lives in the finally, so it also covers the exits this branch+ // never sees (a permanent follower error, a thread deleted in another tab).// Only another successful active-list sync would otherwise drop it, so the thread would keep// reading as durable and a later subscriber-owned stream would be capped, losing the// checkpoints that are its only persistence.@@ -1183,6 +1232,14 @@ function scheduleGenerationRecovery();}} finally {+ // EVERY exit, not just the terminal one: a permanent follower error (another tab deletes the+ // thread, the run row cascades, the request 404s) throws past the terminal branch into the+ // outer catch, leaving a seeded card in the global store for the session. `soleRequest` counts+ // entries, so a later real approval reads as non-sole and loses its Enter/Escape chords.+ // Joined first because the arming is no longer awaited at its call site, so without this a+ // late arm lands after the disarm and leaves the card up on a finished run.+ if (seededApprovals) await seededApprovals.catch(() => {});+ toolRecovery.disarmAll();const store = useChatRuntimeStore.getState();store.setThreadRunning(threadId, false, { owner: serverCancel });store.clearThreadServerCancel(threadId, serverCancel);diff --git a/studio/frontend/src/features/chat/utils/generation-tool-recovery.ts b/studio/frontend/src/features/chat/utils/generation-tool-recovery.tsindex 09a19a82fcf..0d255eb8e76 100644--- a/studio/frontend/src/features/chat/utils/generation-tool-recovery.ts+++ b/studio/frontend/src/features/chat/utils/generation-tool-recovery.ts@@ -103,12 +103,63 @@ function recoveredToolResult(return text;}+/** How a reopened tab re-arms a tool call that is still waiting on a human.+ *+ * The live stream registers a parked call with the confirmation store, and `ToolConfirmationControls`+ * renders Approve/Deny only for a card that has an entry there. Recovery rebuilds the card but has no+ * store of its own, so without this the reopened tab shows the call spinning with no way to answer it+ * while the backend sits parked -- see `state/tool_approvals.wait_tool_decision`, which waits for the+ * returning session precisely so that tab can answer.+ *+ * Passed in rather than imported so this module stays a pure util: the caller owns the store. Both+ * calls must be idempotent -- a card can be re-raised from the seed AND re-folded from its frame. */+export type RecoveredToolConfirmations = {+ /** This card is waiting on a decision. `partId` is the card's own `toolCallId`, which is what the+ * controls look themselves up by, whichever path minted it. `sessionId` is the run's sandbox+ * session, which the decision is resolved against and which scopes "Always allow". */+ register: (partId: string, approvalId: string, sessionId: string) => void;+ /** It is no longer waiting: answered, denied, timed out, or finished. */+ resolve: (partId: string) => void;+};+export function createGenerationToolRecovery(carried: CarriedPart[],runId: string,snapshotSeq = 0,+ toolConfirmations?: RecoveredToolConfirmations,) {const pending = new Map<string, CarriedPart>();+ /** Arm the card a frame or a seed says is parked. Reads the id off the part rather than taking one,+ * so the seed path and the fold path cannot disagree about which card is being armed. */+ /** Cards this recovery armed, so it only ever resolves its own. The live adapter clears+ * unconditionally because it owns every card in its stream; a recovery shares the store with+ * whatever else is on screen, so reaching for a card it never raised is not its business. */+ const armed = new Set<string>();+ const armApproval = (entry: CarriedPart, approvalId: unknown, sessionId: string) => {+ if (!toolConfirmations || typeof approvalId !== "string" || !approvalId) return;+ const partId = record(entry.part)?.toolCallId;+ if (typeof partId === "string" && partId) {+ armed.add(partId);+ toolConfirmations.register(partId, approvalId, sessionId);+ }+ };+ const disarmApproval = (entry: CarriedPart) => {+ const partId = record(entry.part)?.toolCallId;+ if (!toolConfirmations || typeof partId !== "string" || !armed.has(partId)) return;+ armed.delete(partId);+ toolConfirmations.resolve(partId);+ };+ /** Drop every card this recovery armed, for the end of the run rather than the end of a call.+ * A run that terminates WITHOUT a tool_end (the backend failed or restarted while the call was+ * parked) never reaches disarmApproval, so the card would outlive its own run: buttons still on+ * screen, tool group still open, and a decision that can only 404 because the backend's pending+ * slot went with the restart. Still only this recovery's own cards, for the same reason+ * disarmApproval is scoped that way. */+ const disarmAll = () => {+ if (!toolConfirmations) return;+ for (const partId of armed) toolConfirmations.resolve(partId);+ armed.clear();+ };const researchHandoff = newDeepResearchHandoff();/** The sources a finished search card yields, parsed once per card rather than per publish.* Every rebuild used to re-run the parse over every finished search result in the turn,@@ -176,6 +227,46 @@ export function createGenerationToolRecovery(pending.set(id || `#idless:saved:${savedIdless++}`, entry);}}+ /** Re-arm every call that parked BEFORE the tab closed.+ *+ * Such a call was saved as an unresolved card and its `tool_start` sits at or below the cursor, so+ * no frame re-folds it and the fold's own registration never fires for it: the seed is the only+ * place it still exists. Called by the follower once the run's session is known rather than at+ * construction, because the decision is resolved against that session. A call whose `tool_start`+ * lands above the cursor arms itself as the frame folds; `register` is idempotent, so both paths+ * can name the same pair without raising two cards. */+ /* `stillPending` is what keeps this from arming a call the user ALREADY answered. A saved card+ * looks the same either way: no result, approval id intact, because the result only lands with+ * tool_end. So a tab closed between the click and the tool finishing would otherwise reopen+ * with Approve/Deny over a call that is already executing, and every press 404s. Optional, and+ * when it is absent the old arm-everything behaviour stands, so a caller with no way to ask is+ * no worse off than before. */+ const armSeededApprovals = async (+ sessionId: unknown,+ stillPending?: (approvalId: string) => Promise<boolean>,+ ) => {+ if (!toolConfirmations) return;+ const session = typeof sessionId === "string" ? sessionId : "";+ for (const entry of savedPending) {+ const approvalId = record(entry.part)?.toolApprovalId;+ if (stillPending && typeof approvalId === "string" && approvalId) {+ // A check that throws (offline, server gone) must not cost the user their buttons on a+ // call that really is parked, so an unanswerable question falls back to arming.+ let pending = true;+ try {+ pending = await stillPending(approvalId);+ } catch {+ pending = true;+ }+ if (!pending) continue;+ }+ // Re-read AFTER the await: the replay runs concurrently now, so `tool_end` can fold this card+ // mid-request. Arming a finished call leaves an entry until disarmAll, long enough to make a+ // LATER approval non-sole and silently drop its Enter/Escape chord.+ if (record(entry.part)?.result !== undefined) continue;+ armApproval(entry, approvalId, session);+ }+ };const replayFrom = savedPending.some((entry) => typeof record(entry.part)?.backendToolCallId !== "string",)@@ -371,6 +462,11 @@ export function createGenerationToolRecovery(pending.set(backendId || `#idless:${runId}:${seq}`, entry);if (backendId) completed.delete(backendId);else lastIdless = undefined;+ // The frame says this call is waiting on a human, so the card must offer the decision here too:+ // the backend parks for the returning session, and this tab IS the returning session.+ if (event.awaiting_confirmation === true) {+ armApproval(entry, event.approval_id, sessionId);+ }return;}if (!entry) {@@ -408,6 +504,10 @@ export function createGenerationToolRecovery(pending.delete(id);}}+ // The call is answered, denied, or timed out: the card now has a result, so the decision is gone.+ // Unconditional, as the live path's clearToolConfirmation is: a card that was never armed resolves+ // to a no-op, and a card left armed would offer buttons over a finished result.+ disarmApproval(entry);if (backendId) completed.set(backendId, entry);else lastIdless = entry;};@@ -431,5 +531,5 @@ export function createGenerationToolRecovery(}return out;};- return { replayFrom, apply, withSources };+ return { replayFrom, apply, withSources, armSeededApprovals, disarmAll };}diff --git a/studio/frontend/tests/auto-compaction-settings.test.ts b/studio/frontend/tests/auto-compaction-settings.test.tsindex c5963326a2c..33169cc0877 100644--- a/studio/frontend/tests/auto-compaction-settings.test.ts+++ b/studio/frontend/tests/auto-compaction-settings.test.ts@@ -60,8 +60,9 @@ test("the chat adapter sends compaction fields through the shared helper", () =>assert.match(adapter, /isGguf: isGgufForCompaction/);assert.match(adapter, /loadedIsGguf: runtime\.loadedIsGguf/);assert.match(adapter, /isServedByLlamaCpp\(/);- // One request object for both streams, so a media turn cannot lose these fields.- assert.match(adapter, /image_base64: imageBase64/);+// One request object for both streams, so a media turn cannot lose these fields; the value is+ // derived from THIS turn's messages only (durable-gate.ts), not a variable hoisted earlier.+ assert.match(adapter, /image_base64: findLatestUserImageBase64\(currentTurnMessages\)/);});test("a queued run keeps its own model's llama.cpp verdict after the picker moves on", async () => {diff --git a/studio/frontend/tests/chat-continuation.test.ts b/studio/frontend/tests/chat-continuation.test.tsindex b1a779c488c..eb50cb78418 100644--- a/studio/frontend/tests/chat-continuation.test.ts+++ b/studio/frontend/tests/chat-continuation.test.ts@@ -343,10 +343,13 @@ test("the adapter latches the backend window-exhaustion event", () => {/reason: resolveIncompleteReason\([\s\S]{0,400}contextWindowExceeded,\s*\)/,"the error path decides a reason without asking what the provider reported",);+ // The provisional reason on every streamed yield is the durability gate's call - a run with a server-side+ // run to resume from reads as cancelled, a walk-away reads as interrupted - and either guess still goes+ // through the resolver, so a window the provider reported outranks what the client inferred.assert.match(adapter,- /incomplete: \{\s*reason: resolveIncompleteReason\("cancelled" as const, contextWindowExceeded\),\s*\}/,- "an abort saves a bare cancelled again, losing what the provider reported",+ /incomplete: \{\s*reason: resolveIncompleteReason\(\s*(?:\/\/[^\n]*\n\s*)*incompleteReason \?\?\s*\(\s*generationDecision === "durable" \?\s*"cancelled"\s*:\s*"interrupted"\),\s*contextWindowExceeded,\s*\)/,+ "an abort saves a bare cancelled again, losing the gate that names a walk-away and what the provider reported",);// The finish chunk carries no delta, so nothing between here and `[DONE]` need yield.const handler = adapter.slice(diff --git a/studio/frontend/tests/chat-generation-reconnect.test.ts b/studio/frontend/tests/chat-generation-reconnect.test.tsindex ecb0ad50e3f..ef6ab73b069 100644--- a/studio/frontend/tests/chat-generation-reconnect.test.ts+++ b/studio/frontend/tests/chat-generation-reconnect.test.ts@@ -179,6 +179,42 @@ test("durable replay normalizes reasoning summary control frames", async () => {assert.deepEqual(chunks, [{ _reasoningDurationMs: 3200 }]);});+test("durable replay re-tags persisted tool control frames for the shared consumer", async () => {+ const toolStart = { type: "tool_start", name: "web_search", input: { q: "x" } };+ globalThis.fetch = (async (input: RequestInfo | URL) => {+ if (String(input).includes("/events")) {+ return sse([+ frame(1, "chunk", { type: "tool_status", content: "Searching…" }),+ frame(2, "chunk", toolStart),+ frame(+ 3,+ "chunk",+ { choices: [{ delta: { tool_calls: [{ index: 0, id: "c1" }] } }] },+ ),+ frame(4, "run.completed", { status: "completed" }, run("completed", 4)),+ ]);+ }+ return new Response(JSON.stringify(run("completed", 4)), {+ status: 200,+ headers: { "content-type": "application/json" },+ });+ }) as typeof fetch;+ const chunks: object[] = [];+ for await (const update of followChatGenerationRun("run-1", {+ initialRun: run("running", 0),+ replayFrom: 0,+ })) {+ if (update.event?.type === "chunk") chunks.push(update.event.payload);+ }+ // Tagged exactly like the legacy stream yields them, and structured delta.tool_calls chunks+ // pass through untouched for the consumer's index-keyed accumulation.+ assert.deepEqual(chunks, [+ { _toolStatus: "Searching…" },+ { _toolEvent: toolStart },+ { choices: [{ delta: { tool_calls: [{ index: 0, id: "c1" }] } }] },+ ]);+});+test("a backend without chat runs selects the legacy path", async () => {globalThis.fetch = (async () =>new Response(null, { status: 404 })) as typeof fetch;diff --git a/studio/frontend/tests/durable-gate.test.ts b/studio/frontend/tests/durable-gate.test.tsnew file mode 100644index 00000000000..3c13863489e--- /dev/null+++ b/studio/frontend/tests/durable-gate.test.ts@@ -0,0 +1,138 @@+// SPDX-License-Identifier: AGPL-3.0-only+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++import assert from "node:assert/strict";+import test from "node:test";++import { registerBundlerResolver } from "./helpers/kit.ts";++registerBundlerResolver();++const {+ BROWSER_EXECUTED_TOOLS,+ isDurableRunCandidate,+ turnRequiresLegacyStream,+} = await import("../src/features/chat/api/durable-gate.ts");++// A turn stays durable (returns false) unless a tool the BROWSER must execute is enabled. The regression these+// tests pin: the gate used to read `requestPayload.tools`, which is absent on the local path and is the caller's+// schema catalog on the passthrough path - so a catalog-bearing turn was forced onto the cancel-on-disconnect+// stream and a closed browser halted generation mid-turn.++test("a local agentic turn with server-executed tools stays durable", () => {+ assert.equal(+ turnRequiresLegacyStream({+ enable_tools: true,+ enabled_tools: ["web_search", "python", "terminal", "edit_file"],+ }),+ false,+ );+});++test("a passthrough turn carrying the caller's schema catalog stays durable", () => {+ assert.equal(+ turnRequiresLegacyStream({+ enable_tools: true,+ enabled_tools: ["web_search"],+ tools: [{ type: "function", function: { name: "get_weather" } }],+ }),+ false,+ );+});++test("a plain text turn stays durable", () => {+ assert.equal(turnRequiresLegacyStream({ enable_tools: false }), false);+});++test("only a browser-executed tool forces the legacy stream", () => {+ assert.equal(BROWSER_EXECUTED_TOOLS.size, 0, "nothing is browser-executed today");+});++// ── The gate itself: every term of the conjunction, as a truth table ─────────────────────+// These replace grepping the adapter's source for a token that happens to spell the rule. A turn is a durable-run+// candidate unless exactly one of these is true of it; each case below flips ONE term and says out loud which path+// that sends the turn to and why.++/** A plain text turn on a persisted thread, with its message in hand: everything the gate wants.+ * Field names are the values the adapter resolves and passes; the gate itself resolves nothing. */+const durableTurn = {+ externalProvider: false,+ modelIsAudio: false,+ loadedIsDiffusion: false,+ turnCarriesMedia: false,+ continuation: false,+ threadId: "thread-1",+ incognito: false,+ assistantMessageId: "assistant-1",+ hasUserMessage: true,+};++test("a plain text turn on a persisted thread is a durable-run candidate", () => {+ assert.equal(isDurableRunCandidate(durableTurn), true);+});++const legacyCases: [string, Record<string, unknown>, string][] = [+ [+ "an external-provider turn",+ { externalProvider: true },+ "its own client owns that stream, so there is no server run to reattach to",+ ],+ [+ "an audio model",+ { modelIsAudio: true },+ "audio runs on its own path regardless of the gate",+ ],+ [+ "a loaded diffusion model",+ { loadedIsDiffusion: true },+ "the diffusion path has no durable run to join",+ ],+ [+ "THIS turn carrying an attachment",+ { turnCarriesMedia: true },+ "a media turn stays subscriber-owned; a text follow-up to an earlier screenshot must NOT (see the next case)",+ ],+ [+ "a continuation",+ { continuation: true },+ "the seeded partial is autosaved before the request starts, and admission 409s a placeholder that already has content",+ ],+ [+ "an incognito thread",+ { incognito: true },+ "an incognito run has no stored history to resume from",+ ],+ [+ "no thread to reattach to",+ { threadId: undefined },+ "a durable run is found again by its thread; with none, nothing reattaches",+ ],+ [+ "no assistant message to write into",+ { assistantMessageId: null },+ "a follower needs a row to resume, not invent one",+ ],+ [+ "no user message for the run to answer",+ { hasUserMessage: false },+ "there is no turn yet, so nothing survives the tab closing",+ ],+];++for (const [name, override, why] of legacyCases) {+ test(`${name} stays on the subscriber-owned stream`, () => {+ assert.equal(+ isDurableRunCandidate({ ...durableTurn, ...override }),+ false,+ `${name}: ${why}`,+ );+ });+}++test("the gate reads the turn's own media, never the thread's history", () => {+ // The regression this pins: the scan that fills `turnCarriesMedia` walked post-prune HISTORY, so one screenshot+ // from an earlier turn refused every later text-only turn AND sent it to the legacy stream. A turn whose OWN+ // message carries no media is a candidate even when the thread above it is full of it - which is what the adapter+ // passes here (currentTurnMessages), pinned by tests/studio/test_multi_chat_prompt_queue_contract.py.+ assert.equal(isDurableRunCandidate({ ...durableTurn, turnCarriesMedia: false }), true);+});diff --git a/studio/frontend/tests/generation-tool-recovery-approvals.test.ts b/studio/frontend/tests/generation-tool-recovery-approvals.test.tsnew file mode 100644index 00000000000..417fca29f5b--- /dev/null+++ b/studio/frontend/tests/generation-tool-recovery-approvals.test.ts@@ -0,0 +1,360 @@+// SPDX-License-Identifier: AGPL-3.0-only+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++import assert from "node:assert/strict";+import test from "node:test";++import { registerBundlerResolver } from "./helpers/kit.ts";++registerBundlerResolver();++const { createGenerationToolRecovery } = await import(+ "../src/features/chat/utils/generation-tool-recovery.ts"+);++// A durable tool turn parks the backend on a human decision and keeps waiting for the returning+// session (state/tool_approvals.wait_tool_decision). The reopened tab IS that session, so the card it+// rebuilds has to offer Approve/Deny -- ToolConfirmationControls renders them only for a card the+// confirmation store knows about. These pin both ways a parked call can reach a reopened tab: as a+// frame above the cursor, and as a seed whose frame will never be re-folded.++type Registered = { partId: string; approvalId: string; sessionId: string };++function spyConfirmations() {+ const registered: Registered[] = [];+ const resolved: string[] = [];+ const live = new Map<string, Registered>();+ return {+ registered,+ resolved,+ /** What the store actually holds, after registers and resolves have both been applied. */+ live,+ hooks: {+ register: (partId: string, approvalId: string, sessionId: string) => {+ registered.push({ partId, approvalId, sessionId });+ live.set(partId, { partId, approvalId, sessionId });+ },+ resolve: (partId: string) => {+ resolved.push(partId);+ live.delete(partId);+ },+ },+ };+}++const parkedStart = (approvalId: string) => ({+ _toolEvent: {+ type: "tool_start",+ tool_name: "terminal",+ tool_call_id: "call-1",+ approval_id: approvalId,+ awaiting_confirmation: true,+ arguments: { command: "echo hi" },+ },+});++test("a call that parks after the tab reattaches arms its own card", async () => {+ const spy = spyConfirmations();+ const carried: { at: number; part: unknown }[] = [];+ const recovery = createGenerationToolRecovery(carried, "run-1", 0, spy.hooks);++ recovery.apply(parkedStart("appr-1"), 0, 1, "sess-1");++ const part = carried[0].part as Record<string, unknown>;+ assert.equal(spy.registered.length, 1);+ assert.deepEqual(spy.registered[0], {+ partId: part.toolCallId as string,+ approvalId: "appr-1",+ sessionId: "sess-1",+ });+});++test("a call that parked BEFORE the tab closed is re-raised from the seed", async () => {+ // The saved card is all that is left of it: its tool_start sits at or below the cursor, so no+ // frame re-folds it. This is the case that made a reopened tab show a spinner with no buttons.+ const spy = spyConfirmations();+ const carried = [+ {+ at: 12,+ part: {+ type: "tool-call",+ toolCallId: "sess-1:thread-1:appr-7",+ toolName: "terminal",+ backendToolCallId: "call-7",+ toolApprovalId: "appr-7",+ args: { command: "echo hi" },+ },+ },+ ];+ const recovery = createGenerationToolRecovery(carried, "run-1", 40, spy.hooks);++ assert.deepEqual(spy.registered, [], "nothing is armed before the session is known");+ await recovery.armSeededApprovals("sess-1");+ assert.deepEqual(spy.registered, [+ { partId: "sess-1:thread-1:appr-7", approvalId: "appr-7", sessionId: "sess-1" },+ ]);+});++test("a finished card is never re-armed from the seed", async () => {+ const spy = spyConfirmations();+ const carried = [+ {+ at: 0,+ part: {+ type: "tool-call",+ toolCallId: "sess-1:thread-1:appr-8",+ toolName: "terminal",+ toolApprovalId: "appr-8",+ result: "hi",+ },+ },+ ];+ await createGenerationToolRecovery(carried, "run-1", 40, spy.hooks).armSeededApprovals("sess-1");+ assert.deepEqual(spy.registered, [], "an answered call has a result; it is not waiting on anyone");+});++test("the seed and the frame can name the same call without raising two cards", async () => {+ const spy = spyConfirmations();+ const carried = [+ {+ at: 5,+ part: {+ type: "tool-call",+ toolCallId: "sess-1:thread-1:appr-1",+ toolName: "terminal",+ backendToolCallId: "call-1",+ toolApprovalId: "appr-1",+ },+ },+ ];+ const recovery = createGenerationToolRecovery(carried, "run-1", 0, spy.hooks);+ await recovery.armSeededApprovals("sess-1");+ recovery.apply(parkedStart("appr-1"), 5, 9, "sess-1");++ assert.equal(spy.live.size, 1, "one card, however many times it was named");+ assert.equal(carried.length, 1, "and the frame folded into the saved card, not a second one");+ assert.equal(spy.live.get("sess-1:thread-1:appr-1")?.approvalId, "appr-1");+});++test("the decision goes away when the call gets a result", async () => {+ const spy = spyConfirmations();+ const carried: { at: number; part: unknown }[] = [];+ const recovery = createGenerationToolRecovery(carried, "run-1", 0, spy.hooks);++ recovery.apply(parkedStart("appr-1"), 0, 1, "sess-1");+ const partId = (carried[0].part as Record<string, unknown>).toolCallId as string;+ assert.equal(spy.live.size, 1);++ recovery.apply(+ {+ _toolEvent: {+ type: "tool_end",+ tool_name: "terminal",+ tool_call_id: "call-1",+ result: "hi\n",+ },+ },+ 0,+ 2,+ "sess-1",+ );+ assert.deepEqual(spy.resolved, [partId]);+ assert.equal(spy.live.size, 0, "no Approve/Deny over a finished result");+});++test("a call that never needed a decision arms nothing", async () => {+ const spy = spyConfirmations();+ const carried: { at: number; part: unknown }[] = [];+ createGenerationToolRecovery(carried, "run-1", 0, spy.hooks).apply(+ {+ _toolEvent: {+ type: "tool_start",+ tool_name: "web_search",+ tool_call_id: "call-2",+ arguments: { q: "x" },+ },+ },+ 0,+ 1,+ "sess-1",+ );+ assert.deepEqual(spy.registered, []);+});++test("recovery still works for a caller that passes no confirmation hooks", async () => {+ // The hooks are optional: every other caller of this module keeps its old shape.+ const carried: { at: number; part: unknown }[] = [];+ const recovery = createGenerationToolRecovery(carried, "run-1", 0);+ recovery.apply(parkedStart("appr-1"), 0, 1, "sess-1");+ await recovery.armSeededApprovals("sess-1");+ assert.equal(carried.length, 1);+ assert.equal(+ (carried[0].part as Record<string, unknown>).toolApprovalId,+ "appr-1",+ "the approval id still lands on the part",+ );+});++const seededParkedCard = () => [+ {+ at: 12,+ part: {+ type: "tool-call",+ toolCallId: "sess-1:thread-1:appr-9",+ toolName: "terminal",+ backendToolCallId: "call-9",+ toolApprovalId: "appr-9",+ args: { command: "echo hi" },+ },+ },+];++test("a run that ends without a tool_end still takes its cards down", async () => {+ // The backend failed or restarted while the call was parked, so no tool_end is ever emitted and+ // the per-call disarm never runs. Without a run-level sweep the card outlives its own run:+ // buttons on screen over a dead run, and a decision that can only 404 because the pending slot+ // went with the restart. The terminal guard at the call site covers a run that was ALREADY+ // terminal when recovery attached; this covers one that gets there afterwards.+ const spy = spyConfirmations();+ const recovery = createGenerationToolRecovery(seededParkedCard(), "run-1", 40, spy.hooks);++ await recovery.armSeededApprovals("sess-1");+ assert.equal(spy.registered.length, 1);+ assert.equal(spy.live.size, 1, "the card is live before the run ends");++ recovery.disarmAll();+ assert.deepEqual(spy.resolved, ["sess-1:thread-1:appr-9"]);+ assert.equal(spy.live.size, 0, "no card outlives its run");++ // Idempotent: the follow loop can see more than one terminal snapshot, and a second sweep must+ // not reach for a card this recovery no longer owns.+ recovery.disarmAll();+ assert.deepEqual(spy.resolved, ["sess-1:thread-1:appr-9"]);+});++test("the run-level sweep only takes down cards this recovery armed", async () => {+ // Same scoping rule as the per-call disarm: a recovery shares the store with whatever else is on+ // screen, so a card it never raised is not its business to resolve.+ const spy = spyConfirmations();+ const recovery = createGenerationToolRecovery(seededParkedCard(), "run-1", 40, spy.hooks);++ recovery.disarmAll(); // armed nothing, so it resolves nothing+ assert.deepEqual(spy.resolved, []);+});++test("a call the user already answered is not re-armed on reopen", async () => {+ // The third way a reopened tab ended up with buttons that cannot work. Approve a slow tool, close+ // the tab before tool_end, reopen: the saved card still has no result and still carries its+ // approval id, because the result only arrives with tool_end. Arming it puts Approve/Deny over a+ // call that is already executing, and every press 404s. The card's shape cannot tell the two+ // apart, so the server is asked.+ const spy = spyConfirmations();+ const recovery = createGenerationToolRecovery(seededParkedCard(), "run-1", 40, spy.hooks);++ await recovery.armSeededApprovals("sess-1", async () => false);+ assert.deepEqual(spy.registered, [], "an answered call must not get its buttons back");+ assert.equal(spy.live.size, 0);+});++test("a call that really is still parked is armed as before", async () => {+ // The control for the test above. If the check were wired the wrong way round, the feature would+ // stop working entirely and the negative test alone would still pass.+ const spy = spyConfirmations();+ const recovery = createGenerationToolRecovery(seededParkedCard(), "run-1", 40, spy.hooks);++ await recovery.armSeededApprovals("sess-1", async () => true);+ assert.equal(spy.registered.length, 1);+ assert.equal(spy.live.size, 1);+});++test("a check that cannot be answered still arms the card", async () => {+ // Offline, or a backend too old to have the route. Losing the buttons on a call that really is+ // parked is the worse failure, so an unanswerable question falls back to the old behaviour.+ const spy = spyConfirmations();+ const recovery = createGenerationToolRecovery(seededParkedCard(), "run-1", 40, spy.hooks);++ await recovery.armSeededApprovals("sess-1", async () => {+ throw new Error("network down");+ });+ assert.equal(spy.registered.length, 1, "a failed check must not cost a parked call its buttons");+});++// ── The check and the replay now run concurrently ───────────────────────────+// runtime-provider no longer awaits armSeededApprovals at its call site: awaiting held the+// /events stream closed, and that stream is the only thing marking the run attended server-side,+// so a tab returning near the park ceiling lost its approval during the very request asking+// whether it was still pending. The cost of un-blocking it is this window: `tool_end` can fold a+// card while its status request is in flight. disarmApproval is a no-op then, because nothing is+// armed yet, so arming afterwards registers an already-finished call. Nothing renders buttons over+// it (a part with a result is not `awaiting`), but the store entry survives until disarmAll, which+// is long enough to make a LATER approval non-sole and silently drop its Enter/Escape chord.++const finishedEnd = (approvalId: string) => ({+ _toolEvent: {+ type: "tool_end",+ tool_name: "terminal",+ tool_call_id: "call-9",+ approval_id: approvalId,+ result: "4.0G\t/home/u/models",+ },+});++test("a card finished while its status request was in flight is not armed afterwards", async () => {+ const spy = spyConfirmations();+ const carried = [+ {+ at: 12,+ part: {+ type: "tool-call",+ toolCallId: "sess-1:thread-1:appr-9",+ toolName: "terminal",+ backendToolCallId: "call-9",+ toolApprovalId: "appr-9",+ args: { command: "du -sh ~/models" },+ },+ },+ ];+ const recovery = createGenerationToolRecovery(carried, "run-1", 40, spy.hooks);++ // Held open so the replay lands strictly between the answer and the resume, which is the race.+ let release: (value: boolean) => void = () => {};+ const gate = new Promise<boolean>((resolve) => {+ release = resolve;+ });+ const arming = recovery.armSeededApprovals("sess-1", () => gate);++ // Another tab answered it, or it expired: the run streams tool_end while we are still waiting.+ recovery.apply(finishedEnd("appr-9"), 0, 41, "sess-1");+ release(true); // the backend still said "pending", because it was, when it was asked+ await arming;++ assert.deepEqual(+ spy.registered,+ [],+ "the card gained a result while the status request was in flight, so arming it leaves a " ++ "store entry nothing will ever clear until the run ends",+ );+});++test("a card still unresolved when the status request returns is armed as before", async () => {+ // The guard above must not cost the case the feature exists for.+ const spy = spyConfirmations();+ const carried = [+ {+ at: 12,+ part: {+ type: "tool-call",+ toolCallId: "sess-1:thread-1:appr-10",+ toolName: "terminal",+ backendToolCallId: "call-10",+ toolApprovalId: "appr-10",+ args: { command: "du -sh ~/models" },+ },+ },+ ];+ const recovery = createGenerationToolRecovery(carried, "run-1", 40, spy.hooks);+ await recovery.armSeededApprovals("sess-1", async () => true);+ assert.deepEqual(spy.registered, [+ { partId: "sess-1:thread-1:appr-10", approvalId: "appr-10", sessionId: "sess-1" },+ ]);+});diff --git a/studio/frontend/tests/generation-tool-recovery.test.ts b/studio/frontend/tests/generation-tool-recovery.test.tsindex 2b08652a849..62653f0b378 100644--- a/studio/frontend/tests/generation-tool-recovery.test.ts+++ b/studio/frontend/tests/generation-tool-recovery.test.ts@@ -339,6 +339,11 @@ async function recoverRun(registerThreadServerCancel() {},setThreadRunning() {},clearThreadServerCancel() {},+ // The real store carries these, and recovery now uses them to put Approve/Deny back in front of+ // a call that parked while the tab was gone. A stand-in missing them would make a recovery that+ // touches an approval look broken here and work in the app.+ setToolConfirmation() {},+ clearToolConfirmation() {},};const run = {id: "run",diff --git a/studio/frontend/tests/seeded-approval-terminal-run.test.ts b/studio/frontend/tests/seeded-approval-terminal-run.test.tsnew file mode 100644index 00000000000..150ab8af8ca--- /dev/null+++ b/studio/frontend/tests/seeded-approval-terminal-run.test.ts@@ -0,0 +1,189 @@+// SPDX-License-Identifier: AGPL-3.0-only+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++// generation-tool-recovery-approvals.test.ts holds the rule that a call parked before the tab+// closed gets its Approve/Deny back. This holds the BOUND on that rule, at the one place it can be+// expressed: the run has to still be answerable.+//+// A run that settled while it was parked (a backend restart terminalises surviving rows as+// interrupted) has no in-memory _pending slot left. The saved assistant message still carries the+// unresolved card, and no tool_end is ever coming to disarm it, so arming from the seed puts+// Approve/Deny in front of the user whose confirm can only ever return 404. Buttons that cannot+// work are worse than the missing buttons this feature exists to restore.+//+// The guard lives in the follow loop, which no behavioural test reaches without a live EventSource,+// so the call site is pinned here rather than trusted: deleting the guard leaves every other test+// green.++import assert from "node:assert/strict";+import { readFileSync } from "node:fs";+import test from "node:test";+import { fileURLToPath } from "node:url";++import ts from "typescript";++const SOURCE = fileURLToPath(+ new URL("../src/features/chat/runtime-provider.tsx", import.meta.url),+);++const parsed = ts.createSourceFile(+ "runtime-provider.tsx",+ readFileSync(SOURCE, "utf8"),+ ts.ScriptTarget.Latest,+ true,+ ts.ScriptKind.TSX,+);++/** Every call to `<something>.armSeededApprovals(...)` in the file. */+const seededApprovalCalls = (): ts.CallExpression[] => {+ const found: ts.CallExpression[] = [];+ const walk = (node: ts.Node): void => {+ if (+ ts.isCallExpression(node) &&+ ts.isPropertyAccessExpression(node.expression) &&+ node.expression.name.text === "armSeededApprovals"+ ) {+ found.push(node);+ }+ ts.forEachChild(node, walk);+ };+ walk(parsed);+ return found;+};++/** Walk up from a node collecting the text of every enclosing `if` condition. */+const enclosingConditions = (node: ts.Node): string[] => {+ const conditions: string[] = [];+ let current: ts.Node | undefined = node.parent;+ while (current) {+ if (ts.isIfStatement(current)) {+ conditions.push(current.expression.getText(parsed));+ }+ current = current.parent;+ }+ return conditions;+};++test("the seeded approvals call exists at all", () => {+ // If this fails the test below would pass vacuously, which is the failure mode that makes a+ // call-site test worthless.+ assert.ok(+ seededApprovalCalls().length > 0,+ "no armSeededApprovals call site found in runtime-provider.tsx",+ );+});++test("seeded approvals are armed only while the run is non-terminal", () => {+ for (const call of seededApprovalCalls()) {+ const conditions = enclosingConditions(call);+ const guarded = conditions.some(+ (text) =>+ text.includes("isTerminalChatGenerationRun") && text.trimStart().startsWith("!"),+ );+ assert.ok(+ guarded,+ "armSeededApprovals must sit under `if (!isTerminalChatGenerationRun(...))`. " ++ "A run that settled while parked has no pending slot left, so the card it arms " ++ "offers buttons that can only 404. Enclosing conditions were: " ++ JSON.stringify(conditions),+ );+ }+});++test("the terminal check is imported, not invented locally", () => {+ const text = readFileSync(SOURCE, "utf8");+ assert.match(+ text,+ /import\s*\{[^}]*isTerminalChatGenerationRun[^}]*\}\s*from/s,+ "isTerminalChatGenerationRun must come from chat-generation-api, so the terminal set " ++ "stays defined in exactly one place",+ );+});++// ── Arming must not hold the event stream closed ────────────────────────────+// followChatGenerationRun yields its snapshot BEFORE it opens /events+// (chat-generation-api.ts: `yield { run, source: "snapshot" }` precedes the+// streamChatGenerationEvents loop), so the consumer is what decides when that stream opens.+// /events is also the only thing that marks the run attended server-side+// (state/run_subscribers.py), and attendance is what stops the park ceiling denying an+// approval. Awaiting the seeded-approval check at the call site therefore serialises the+// "is it still pending?" request AHEAD of the stream that says someone is watching, so a tab+// returning near the ceiling can have its approval expire during that very request and be+// handed buttons that can only 404.+//+// Pinned on the call site for the same reason as the guard above: the follow loop needs a live+// EventSource, so no behavioural test in this suite reaches it.++/** Walk up from a node looking for an `await` that directly wraps it. */+const isDirectlyAwaited = (node: ts.Node): boolean => {+ let current: ts.Node | undefined = node.parent;+ while (current) {+ if (ts.isAwaitExpression(current)) return true;+ // Stop at the first construct that is not a transparent wrapper: anything further up+ // awaits a different expression, not this call.+ if (+ ts.isExpressionStatement(current) ||+ ts.isVariableDeclaration(current) ||+ ts.isBinaryExpression(current) ||+ ts.isBlock(current)+ ) {+ return false;+ }+ current = current.parent;+ }+ return false;+};++test("the seeded-approval check does not block the event stream from opening", () => {+ for (const call of seededApprovalCalls()) {+ assert.ok(+ !isDirectlyAwaited(call),+ "armSeededApprovals must NOT be awaited at its call site. The follower is suspended " ++ "at followChatGenerationRun's snapshot yield, so awaiting here delays " ++ "streamChatGenerationEvents, which is the only thing that marks the run attended " ++ "(state/run_subscribers.py). A tab returning near UNSLOTH_STUDIO_TOOL_APPROVAL_TIMEOUT_S " ++ "would then lose the approval during the request that checks whether it is still pending. " ++ "Assign the promise and join it before disarmAll instead.",+ );+ }+});+++// ── The disarm has to cover every exit, not just the terminal one ───────────+// A permanent follower error -- another tab deletes the thread, the run row cascades, the next+// request 404s -- throws straight past the terminal branch and is swallowed by the outer catch. A+// card armed from the seed would then sit in the global toolConfirmations store for the rest of the+// session. Nothing renders buttons over a finished part, but `soleRequest` counts entries, so a+// later real approval reads as non-sole and silently loses its Enter and Escape chords.++test("disarmAll runs in the recovery's finally, not only on the terminal path", () => {+ const text = readFileSync(SOURCE, "utf8");+ const disarms = [...text.matchAll(/toolRecovery\.disarmAll\(\)/g)];+ assert.equal(+ disarms.length,+ 1,+ "expected exactly one disarmAll call site; more than one means the terminal-path copy came " ++ "back and the two can disagree about ordering against the seed join",+ );+ // The call must sit inside a `finally {`, which is the only block every exit reaches.+ const before = text.slice(0, disarms[0]!.index!);+ const lastFinally = before.lastIndexOf("} finally {");+ const lastTerminalIf = before.lastIndexOf("isTerminalChatGenerationRun(update.run)");+ assert.ok(+ lastFinally > lastTerminalIf,+ "disarmAll must sit in the recovery's finally block. Under the terminal `if` it is skipped " ++ "whenever the follower throws, which leaks the armed card into the global store.",+ );+});++test("the seed is joined before the disarm, wherever the disarm lives", () => {+ const text = readFileSync(SOURCE, "utf8");+ const disarmIndex = text.indexOf("toolRecovery.disarmAll()");+ assert.ok(disarmIndex > 0, "disarmAll call site not found");+ assert.match(+ text.slice(0, disarmIndex),+ /await seededApprovals/,+ "the seeded-approval promise must be joined before disarmAll, or an arm that resolves " ++ "late re-raises buttons on a run that is already over",+ );+});diff --git a/studio/frontend/tests/tool-approval-gone.test.ts b/studio/frontend/tests/tool-approval-gone.test.tsnew file mode 100644index 00000000000..bc79817e087--- /dev/null+++ b/studio/frontend/tests/tool-approval-gone.test.ts@@ -0,0 +1,161 @@+// SPDX-License-Identifier: AGPL-3.0-only+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++// An approval the backend is no longer holding is not a failed post. Both used to arrive at the+// card as one bare throw out of parseJsonOrThrow, so an expired request rendered "Could not send+// your decision. Try again." next to re-enabled buttons: advice that can never work, since the+// slot is gone and every retry 404s until tool_end clears the card. The 404 now carries a type.++import assert from "node:assert/strict";+import test from "node:test";++import { loadWithStubs } from "./helpers/module-stubs.ts";++type Module = {+ resolveToolConfirmation: (+ sessionId: string,+ approvalId: string,+ decision: "allow" | "deny",+ ) => Promise<boolean>;+ ToolApprovalGoneError: new (message?: string) => Error;+};++function jsonResponse(status: number, body: unknown) {+ return {+ status,+ ok: status >= 200 && status < 300,+ headers: { get: () => null },+ async json() {+ return body;+ },+ };+}++function harness(response: ReturnType<typeof jsonResponse>) {+ const requests: { url: string; init?: RequestInit }[] = [];+ const module = loadWithStubs<Module>(+ new URL("../src/features/chat/api/chat-api.ts", import.meta.url),+ {+ "@/features/auth": {+ authFetch: async (url: string, init?: RequestInit) => {+ requests.push({ url, init });+ return response;+ },+ },+ "@/lib/format-fastapi-error": {+ formatApiErrorBody: (body: unknown) =>+ (body as { detail?: string } | null)?.detail ?? null,+ },+ "../types": {},+ "../types/api": {},+ "../utils/chat-history-revision": {+ notifyChatHistoryUpdated: () => {},+ isCoalescedHistoryEvent: () => false,+ },+ "../utils/load-warning-toast": { showLoadWarning: () => {} },+ "./generation-length.ts": {},+ "./gguf-variants-request": {},+ "./padded-response": { assertCompletedPaddedBody: () => {} },+ "@/features/hf-auth": { prepareHfTokenForUse: async () => undefined },+ "@/features/igpu-carveout": {+ dismissCarveoutAdviceForModel: () => {},+ showCarveoutAdvice: () => {},+ },+ "@/features/hub/lib/abort-signals": {},+ "@/features/hub/lib/hub-token-header": { hubTokenHeader: () => ({}) },+ "@/features/hub/lib/network": { isHuggingFaceOffline: () => false },+ "@/features/native-intents/api": { consumeNativePathToken: () => undefined },+ "@/lib/model-lifecycle-events": {},+ },+ );+ return { module, requests };+}++test("an approval that is no longer pending throws the typed error, not a bare one", async () => {+ const { module } = harness(+ jsonResponse(404, { detail: "No pending tool call confirmation" }),+ );+ const error = await module+ .resolveToolConfirmation("sess", "approval-1", "allow")+ .then(+ () => null,+ (err: unknown) => err,+ );+ assert.ok(+ error instanceof module.ToolApprovalGoneError,+ "a 404 must be distinguishable from a transport failure, or the card tells the user to retry something that can never succeed",+ );+});++test("a transport failure stays an ordinary error, so retrying is still offered", async () => {+ const { module } = harness(jsonResponse(500, { detail: "upstream exploded" }));+ const error = await module+ .resolveToolConfirmation("sess", "approval-1", "allow")+ .then(+ () => null,+ (err: unknown) => err,+ );+ assert.ok(error instanceof Error);+ assert.ok(+ !(error instanceof module.ToolApprovalGoneError),+ "only a 404 means the slot is gone",+ );+});++test("a matched decision still resolves true", async () => {+ const { module, requests } = harness(jsonResponse(200, { resolved: true }));+ assert.equal(await module.resolveToolConfirmation("sess", "a1", "deny"), true);+ assert.equal(requests.length, 1);+ assert.match(requests[0]!.url, /tool-confirm/);+});++// ── The card's own wiring, pinned at the source ──────────────────────────────+// The component is not mounted here (no DOM in this suite), so these read the source. Both are+// one-token regressions that a type check cannot catch and that would silently restore the bug.++const CONTROLS = await import("node:fs").then((fs) =>+ fs.readFileSync(+ new URL(+ "../src/components/assistant-ui/tool-confirmation-controls.tsx",+ import.meta.url,+ ),+ "utf-8",+ ),+);++test("the gone case gets its own copy and does not tell the user to retry", () => {+ assert.match(CONTROLS, /This request is no longer waiting for an answer\./);+ assert.match(+ CONTROLS,+ /failure === "gone"/,+ "the two failure kinds must stay distinguishable at the card",+ );+});++test("a decision that is gone disables the buttons instead of inviting another 404", () => {+ const disabled = CONTROLS.match(/disabled=\{pending !== null[^}]*\}/g) ?? [];+ assert.equal(disabled.length, 3, "all three buttons carry a disabled expression");+ for (const expr of disabled) {+ assert.match(+ expr,+ /failure === "gone"/,+ `a button stays pressable after the approval is gone: ${expr}`,+ );+ }+});++test("Always allow records its session grant only after the backend takes the decision", () => {+ // The regression: allowToolAlways ran in the onClick, before the post. A press that visibly+ // failed still auto-approved this tool for every later call in the session.+ assert.doesNotMatch(+ CONTROLS,+ /onClick=\{\(\) => \{\s*if \(autoAllowKey\) allowToolAlways/,+ "the session grant is being recorded on the click again, ahead of the decision landing",+ );+ assert.match(CONTROLS, /if \(alsoAlways && autoAllowKey\) allowToolAlways\(/);+ assert.match(+ CONTROLS,+ /onClick=\{\(\) => void resolve\("allow", true\)\}/,+ "Always allow must route through resolve so the grant follows the backend's answer",+ );+});diff --git a/tests/studio/test_multi_chat_prompt_queue_contract.py b/tests/studio/test_multi_chat_prompt_queue_contract.pyindex 58031b760cc..2573e621a3b 100644--- a/tests/studio/test_multi_chat_prompt_queue_contract.py+++ b/tests/studio/test_multi_chat_prompt_queue_contract.py@@ -14,6 +14,10 @@ FRONTEND = REPO / "studio/frontend/src"THREAD = (FRONTEND / "components/assistant-ui/thread.tsx").read_text(encoding = "utf-8")APP_SIDEBAR = (FRONTEND / "components/app-sidebar.tsx").read_text(encoding = "utf-8")CHAT_ADAPTER = (FRONTEND / "features/chat/api/chat-adapter.ts").read_text(encoding = "utf-8")+DURABLE_GATE = (FRONTEND / "features/chat/api/durable-gate.ts").read_text(encoding = "utf-8")+CHAT_GENERATION_API = (FRONTEND / "features/chat/api/chat-generation-api.ts").read_text(+ encoding = "utf-8"+)MODEL_RUNTIME = (FRONTEND / "features/chat/hooks/use-chat-model-runtime.ts").read_text(encoding = "utf-8")@@ -842,15 +846,58 @@ def test_queued_settings_are_thread_scoped_without_cross_chat_fallback():)-def test_base64_media_turns_stay_on_the_legacy_stream():+def test_a_media_turn_stays_legacy_and_its_attachments_stay_turn_scoped():+ """A turn that carries media is still legacy; what changed is that it stops refusing its FOLLOW-UPS.++ The gate used to read the raw base64 fields off post-prune HISTORY, so one screenshot anywhere in a thread+ pushed every later text-only turn onto the cancel-on-disconnect stream - and that stale blob went out as a+ top-level payload field too, so the backend 400'd "Media chat runs use the legacy streaming path" on turns that+ never carried media at all. Both halves are pinned here: the gate is now `isDurableRunCandidate({...})` over+ resolved values (its own truth table lives in tests/durable-gate.test.ts), and the payload's attachment channel+ scans THIS turn's message alone.++ Naming, because the two are easy to confuse: admitting a media turn ITSELF to a durable run is a separate+ change living in #10406, and this branch does not make it. `turnCarriesMedia` still sends such a turn to the+ legacy stream here, and routes/chat_generation_runs.py still refuses a populated `_MEDIA_FIELDS` payload.+ """+ # The adapter delegates; it does not spell the rule out here anymore.+ assert "const generationCandidate = isDurableRunCandidate({" in CHAT_ADAPTERcandidate = _between(CHAT_ADAPTER,- "const generationCandidate = Boolean(",- ");",+ "const generationCandidate = isDurableRunCandidate({",+ "});",)- assert "!imageBase64" in candidate- assert "!audioBase64" in candidate- assert "!videoBase64" in candidate+ # What reaches the gate is the turn-scoped scan, never a raw blob pulled off history: if a base64 field shows up+ # in the gate again, every text-only turn after one screenshot is back on the subscriber-owned stream and+ # closing the tab kills it mid-turn.+ assert "turnCarriesMedia: currentTurnCarriesMedia," in candidate+ # The rule itself lives in the pure module, where a truth table can pin it case by case.+ assert "input.turnCarriesMedia !== true" in DURABLE_GATE+ for token in ("imageBase64", "audioBase64", "videoBase64"):+ assert token not in candidate, (+ f"the durability gate reads the thread-scoped {token} again, so every text-only turn following one "+ "screenshot is back on the subscriber-owned stream and closing the tab kills it mid-generation. If "+ "those follow-ups must be refused again, do it behind a backend toggle in "+ "studio/backend/routes/chat_generation_runs.py, not in the frontend gate"+ )+ # The channel that made a text-only follow-up look like a media turn: scanned out of THIS turn's message.+ assert "const currentTurnMessages = [generationUserMessage]" in CHAT_ADAPTER+ for field, argument in (+ ("image_base64", "findLatestUserImageBase64(currentTurnMessages)"),+ (+ "audio_base64",+ "findLatestUserAudioBase64(\n currentTurnMessages,",+ ),+ ("video_base64", "findLatestUserVideoBase64(currentTurnMessages)"),+ ):+ assert f"{field}: {argument}" in CHAT_ADAPTER, (+ f"{field} must be scanned out of currentTurnMessages, not thread history: a stale blob from an "+ "earlier turn makes the backend refuse every later turn"+ )+ # A media-policy rejection is one of the policy rejections that degrade silently to the legacy stream, rather+ # than surfacing as "An error occurred" - which is what made the refusal survivable in the first place.+ assert '"Media chat runs use the legacy streaming path"' in CHAT_GENERATION_API+ assert "isLegacyFallbackChatGenerationAdmissionError" in CHAT_ADAPTERdef test_continuations_stay_on_the_legacy_stream():@@ -862,10 +909,12 @@ def test_continuations_stay_on_the_legacy_stream():"""candidate = _between(CHAT_ADAPTER,- "const generationCandidate = Boolean(",- ");",+ "const generationCandidate = isDurableRunCandidate({",+ "});",)- assert "!continuation" in candidate+ # The gate itself is a truth table in api/durable-gate.ts; what is pinned here is that the adapter still hands+ # it this turn's continuation flag, so a Continue keeps the stream it needs.+ assert "continuation," in candidatedef test_compare_prompt_list_resets_when_preflight_never_starts_a_run():