Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Studio: stop writing the date into user messages (#12699)
Nilay3f56cd15acb0
diff --git a/studio/backend/core/inference/context_window.py b/studio/backend/core/inference/context_window.pyindex cf68ba67add..c7b77a93cc1 100644--- a/studio/backend/core/inference/context_window.py+++ b/studio/backend/core/inference/context_window.py@@ -11,12 +11,6 @@ import refrom collections.abc import Callablefrom typing import Any, Optional-from utils.current_date_prompt_settings import (- CURRENT_DATE_PROMPT_LINE_RE,- CURRENT_DATE_UPDATE_NOTE_RE,- CURRENT_DATE_UPDATE_PREFIX,-)-_OMITTED_TOOL_EXCHANGE = "[Earlier tool exchange omitted from the rolling context window.]"_UNPRICED_MEDIA_TYPES = frozenset(("image_url", "input_audio", "audio", "input_image", "input_video", "video_url")@@ -141,95 +135,6 @@ def evicted_messages(before: list[dict], after: list[dict]) -> list[dict]:return [message for message in before if id(message) not in kept]-_DATE_NOTE_SEARCH_RE = re.compile(- rf"{re.escape(CURRENT_DATE_UPDATE_PREFIX)}[0-9]{{4}}-[0-9]{{2}}-[0-9]{{2}}\]"-)---def _first_text_index(content: Any) -> Optional[int]:- if isinstance(content, str):- return -1 if content.strip() else None- if isinstance(content, list):- return next(- (- index- for index, part in enumerate(content)- if isinstance(part, dict)- and isinstance(part.get("text"), str)- and part["text"].strip()- ),- None,- )- return None---def _texts(message: dict) -> list[str]:- content = message.get("content")- if isinstance(content, str):- return [content]- if isinstance(content, list):- return [- p["text"] for p in content if isinstance(p, dict) and isinstance(p.get("text"), str)- ]- return []---def _is_folded_tool_text(text: str) -> bool:- if not text.lstrip().startswith("{"):- return False- try:- parsed = json.loads(text)- except ValueError:- return False- return isinstance(parsed, dict) and "tool_response" in parsed---def keep_date_note(messages: list[dict], fitted: list[dict]) -> Optional[tuple[dict, Any]]:- """Move the evicted first turn's date note onto the oldest kept user turn."""- first = next(- (- message- for message in messages- if isinstance(message, dict)- and message.get("role") == "user"- and _first_text_index(message.get("content")) is not None- ),- None,- )- if first is None or any(message is first for message in fitted):- return None- index = _first_text_index(first["content"])- note = CURRENT_DATE_UPDATE_NOTE_RE.match(- first["content"] if index == -1 else first["content"][index]["text"]- )- if note is None or any(- CURRENT_DATE_PROMPT_LINE_RE.search(text) or _DATE_NOTE_SEARCH_RE.search(text)- for message in fitted- for text in _texts(message)- ):- return None- for message in fitted:- index = _first_text_index(message.get("content")) if message.get("role") == "user" else None- if index is None:- continue- content = message["content"]- text = content if index == -1 else content[index]["text"]- if _is_folded_tool_text(text):- continue- noted = f"{note.group(0).strip()}\n\n{text}"- # in place, so identity-based eviction bookkeeping still sees the same turn.- if index == -1:- message["content"] = noted- else:- message["content"] = [- *content[:index],- {**content[index], "text": noted},- *content[index + 1 :],- ]- return message, content- return None--def truncate_oldest_messages(messages: list[dict],keep_ratio: float,diff --git a/studio/backend/core/inference/llama_cpp.py b/studio/backend/core/inference/llama_cpp.pyindex fb8bdefcb14..5755366e2fa 100644--- a/studio/backend/core/inference/llama_cpp.py+++ b/studio/backend/core/inference/llama_cpp.py@@ -399,8 +399,6 @@ def _fit_with_instruction_pins()except Exception: # noqa: BLE001 -- a protection heuristic must never break a chatpins = set()- from core.inference.context_window import keep_date_note-fitted, truncation = _fit_context(messages, protected_message_ids = (anchors | pins) or None, **kwargs)@@ -414,19 +412,7 @@ def _fit_with_instruction_pins(and not truncation.get("fits")and not int(truncation.get("dropped_messages") or 0)):- fitted, truncation = _fit_context(messages, protected_message_ids = anchors or None, **kwargs)- moved = keep_date_note(messages, fitted)- if moved is not None and truncation:- message, before = moved- try:- tokens = kwargs["count_tokens"](fitted)- except Exception: # noqa: BLE001 -- an unpriced note is dropped, never a failed chat- tokens = None- target = prompt_budget(kwargs.get("context_length") or 0, kwargs.get("max_tokens") or 0)- if tokens is None or tokens > target:- message["content"] = before- else:- truncation = {**truncation, "prompt_tokens_after": tokens}+ return _fit_context(messages, protected_message_ids = anchors or None, **kwargs)return fitted, truncationdiff --git a/studio/backend/core/inference/worker.py b/studio/backend/core/inference/worker.pyindex 38e56ead37b..44617d0c1ce 100644--- a/studio/backend/core/inference/worker.py+++ b/studio/backend/core/inference/worker.py@@ -830,6 +830,12 @@ def _handle_load(backend, config: dict, resp_queue: Any) -> None:# Forward chat_template_info so the parent can classify capabilities.try:_tpl_info = _entry.get("chat_template_info")+ _mapped_tpl = None+ if isinstance(_tpl_info, dict) and not model_info["is_mlx"]:+ from core.inference.chat_template_helpers import mapped_chat_template+ _mapped_tpl = mapped_chat_template(+ _entry, getattr(backend, "active_model_name", None) or mc.identifier+ )if isinstance(_tpl_info, dict):model_info["chat_template_info"] = {"has_template": bool(_tpl_info.get("has_template", False)),@@ -841,6 +847,8 @@ def _handle_load(backend, config: dict, resp_queue: Any) -> None:"processor_template": _tpl_info.get("processor_template"),"renders_image": _tpl_info.get("renders_image"),"accepts_multiple_images": _tpl_info.get("accepts_multiple_images"),+ # The body a text render installs at generate time, when the model is mapped.+ "mapped_template": _mapped_tpl,}except Exception as _tpl_exc:logger.warning("chat_template_info forward failed: %s", _tpl_exc)diff --git a/studio/backend/routes/inference.py b/studio/backend/routes/inference.pyindex 71c87107125..1c622c38bd0 100644--- a/studio/backend/routes/inference.py+++ b/studio/backend/routes/inference.py@@ -3960,7 +3960,6 @@ from core.inference.providers import (from core.inference.external_provider import (ExternalProviderClient,_is_openai_family_cloud,- caches_at_the_last_block,)from core.inference.external_tool_transport import OAICompatTransportfrom core.inference.sse_control_frames import (@@ -3982,13 +3981,11 @@ import base64import zlibfrom utils.current_date_prompt_settings import (- CURRENT_DATE_PROMPT_LINE_RE,CURRENT_DATE_PROMPT_PREFIX,- CURRENT_DATE_UPDATE_NOTE_RE,- CURRENT_DATE_UPDATE_PREFIX,+ PROBE_TOOLS,contains_current_date_prompt_line,- conversation_start_date,current_date_prompt_line,+ template_system_turn,replace_current_date_prompt_lines,)@@ -6508,123 +6505,98 @@ def _wants_current_date(request: Any) -> bool:return not _request_has_api_key(request)-def _current_date_parts(request: Any, thread_id: Any) -> tuple[str, str]:- """(system line, user-turn note); a thread keeps its start date so its cached prefix survives."""- date_line = current_date_prompt_line(request = request)- if not date_line or not thread_id or request is None or not _wants_current_date(request):- return date_line, ""- today = CURRENT_DATE_PROMPT_LINE_RE.fullmatch(date_line)- started = conversation_start_date(thread_id, request) if today else None- if started is None:- return date_line, ""- today_iso = date_line[len(CURRENT_DATE_PROMPT_PREFIX) : -1]- # a browser clock running ahead can stamp a future creation day; never state a later "start".- if started.isoformat() >= today_iso:- return date_line, ""- return (- f"{CURRENT_DATE_PROMPT_PREFIX}{started.isoformat()}.",- f"{CURRENT_DATE_UPDATE_PREFIX}{today_iso}]",- )--def _date_gate_blocks(request: Any, include_api_key: bool) -> bool:if request is not None and not _wants_current_date(request):return not include_api_key or _request_is_internal_workflow(request)return False-def _is_folded_tool_json(text: Any) -> bool:- if not isinstance(text, str) or not text.lstrip().startswith("{"):+def _local_chat_template(image: bool = False, tools: bool = False) -> Optional[str]:+ """The template the loaded local model renders this chat with, if any."""+ try:+ llama = get_llama_cpp_backend()+ if llama.is_loaded:+ template = llama.chat_template_override or llama.chat_template+ else:+ from core.inference.orchestrator import peek_inference_backend++ backend = peek_inference_backend()+ info = (backend.models.get(backend.active_model_name) or {}) if backend else {}+ template_info = info.get("chat_template_info") or {}+ # an image or clip renders through the processor's template, as generation picks it.+ template = template_info.get("processor_template") if image else None+ if template is None:+ template = info.get("chat_template_override_requested")+ if (+ not isinstance(template, str)+ or not template.strip()+ or info.get("chat_template_override_reason")+ ):+ # a text render installs the mapped template at generate time, when there is one.+ mapped = None if image else template_info.get("mapped_template")+ template = mapped or template_info.get("template")+ from core.inference.chat_template_helpers import _selected_template_strings_from_value++ # a tool request renders a named template's tool_use body; a processor never picks it.+ selected = _selected_template_strings_from_value(+ template, PROBE_TOOLS if tools else None, prefer_tool_use = not image+ )+ except Exception:+ return None+ return selected[0] if selected else None+++def _local_managed_engine() -> bool:+ try:+ if get_llama_cpp_backend().is_loaded:+ return False+ from core.inference.orchestrator import peek_inference_backend++ backend = peek_inference_backend()+ info = (backend.models.get(backend.active_model_name) or {}) if backend else {}+ except Exception:return False+ return info.get("engine") in ("vllm", "sglang")+++# llama-server flags in the user's pass-through args that choose the template it renders, or how.+_TEMPLATE_EXTRA_FLAGS = frozenset(+ ("--chat-template", "--chat-template-file", "--chat-template-kwargs", "--no-jinja")+)+++def _local_extra_args_pick_the_template() -> bool:try:- parsed = json.loads(text)- except ValueError:+ llama = get_llama_cpp_backend()+ args = (getattr(llama, "extra_args", None) or []) if llama.is_loaded else []+ except Exception:return False- return isinstance(parsed, dict) and "tool_response" in parsed+ return any(str(arg).split("=", 1)[0] in _TEMPLATE_EXTRA_FLAGS for arg in args)-def _is_folded_tool_result(content: Any) -> bool:- # only a turn that is ALL folded result: a follow-up coalesced onto one is the user's text.- if isinstance(content, list):- texts = [- p.get("text")- for p in content- if isinstance(p, dict) and isinstance(p.get("text"), str) and p["text"].strip()- ]- return bool(texts) and all(_is_folded_tool_json(t) for t in texts)- return _is_folded_tool_json(content)+def _local_template_system_turn(+ today: Any,+ image: bool = False,+ tools: bool = False,+ controls: tuple = (),+) -> tuple[bool, Optional[str]]:+ if _local_managed_engine() or _local_extra_args_pick_the_template():+ # the server renders a template Studio never sees, so nothing says a system turn is safe.+ return False, None+ return template_system_turn(_local_chat_template(image, tools), today, tools, controls)-def _append_current_date_note(- messages: list[dict],- request: Any = None,- *,- include_api_key: bool = False,- thread_id: Any = None,- note: str | None = None,- system_prompt: str | None = None,- oldest: bool = False,-) -> list[dict]:- """Lead the newest user turn with the date-change note; earlier turns keep their own bytes."""- if note is None:- if _date_gate_blocks(request, include_api_key):- return messages- date_line, note = _current_date_parts(request, thread_id)- if system_prompt == "" and CURRENT_DATE_PROMPT_LINE_RE.fullmatch(date_line):- # the first turn stands in for the system line, so the cached prefix survives.- messages = _append_current_date_note(messages, note = note)- note = f"{CURRENT_DATE_UPDATE_PREFIX}{date_line[len(CURRENT_DATE_PROMPT_PREFIX) : -1]}]"- oldest = True- if not note:- return messages- for index in range(len(messages)) if oldest else range(len(messages) - 1, -1, -1):- msg = messages[index]- if not isinstance(msg, dict) or msg.get("role") != "user":- continue- content = msg.get("content")- if _is_folded_tool_result(content):- continue- if isinstance(content, str):- if content.startswith(note) or (oldest and CURRENT_DATE_UPDATE_NOTE_RE.match(content)):- return messages- new_content: Any = f"{note}\n\n{content}"- has_text = bool(content.strip())- elif isinstance(content, list):- if content and all(- isinstance(p, dict) and p.get("type") == "tool_result" for p in content- ):- continue- first = next(- (- i- for i, p in enumerate(content)- if isinstance(p, dict) and isinstance(p.get("text"), str) and p["text"].strip()- ),- None,- )- has_text = first is not None- if has_text and (- content[first]["text"].startswith(note)- or (oldest and CURRENT_DATE_UPDATE_NOTE_RE.match(content[first]["text"]))- ):- return messages- new_content = list(content)- if has_text:- new_content[first] = {- **content[first],- "text": f"{note}\n\n{content[first]['text']}",- }- else:- continue- # a media-only turn falls back to "transcribe" / "describe" defaults the note would replace.- if not has_text:- if oldest:- continue- return messages- copied = list(messages)- copied[index] = {**msg, "content": new_content}- return copied- return messages+def _template_controls(payload: Any) -> tuple:+ """The reasoning controls this request's chat template is rendered with."""+ names = ("enable_thinking", "reasoning_effort", "preserve_thinking")+ values = [getattr(payload, name, None) for name in names]+ try:+ llama = get_llama_cpp_backend()+ if llama.is_loaded:+ return tuple(sorted((llama._request_reasoning_kwargs(*values) or {}).items()))+ except Exception:+ pass+ return tuple((name, value) for name, value in zip(names, values) if value is not None)def _apply_current_date_prompt(@@ -6632,27 +6604,49 @@ def _apply_current_date_prompt(request: Any = None,*,include_api_key: bool = False,- thread_id: Any = None,+ template_default: bool = True,+ image: bool = False,+ tools: bool = False,+ controls: tuple = (),) -> str:"""Prefix the user's system prompt with the date when the setting is on.Kept ahead of the user's own text so a system prompt that ends in an instruction still readsas the last word to the model."""- # a system turn would displace the chat template's default one; the user-turn note carries it.- if not system_prompt and (request is None or _wants_current_date(request)):- return system_promptif _date_gate_blocks(request, include_api_key):return system_prompt- date_line = _current_date_parts(request, thread_id)[0]+ date_line = current_date_prompt_line(request = request)if not date_line:return system_promptrefreshed_prompt, stated, _ = _refresh_stated_date(system_prompt, date_line)if stated:return refreshed_prompt+ if not system_prompt:+ from datetime import date++ today = date.fromisoformat(date_line[len(CURRENT_DATE_PROMPT_PREFIX) : -1])+ takes_turn, default = _local_template_system_turn(today, image, tools, controls)+ if not takes_turn or (template_default and default is None):+ # no system turn renders here as the template's own would, and the date never goes into+ # the user's own turns.+ return ""+ if template_default:+ # a system turn displaces the chat template's default one, so carry that default along,+ # rendered for the user's day rather than the server's.+ system_prompt = defaultreturn f"{date_line}\n\n{system_prompt.lstrip()}" if system_prompt else date_line+# The backends' own instruction for an audio turn that brings no system prompt, stated here so the+# date goes ahead of it rather than replacing it.+_AUDIO_INPUT_SYSTEM_PROMPT = "You are an assistant that transcribes speech accurately."+++def _audio_input_system_prompt(system_prompt: str, request: Any) -> str:+ return _apply_current_date_prompt(system_prompt or _AUDIO_INPUT_SYSTEM_PROMPT, request)++# Ollama applies the Modelfile SYSTEM only when `req.Messages[0].Role != "system"` (its# server/routes.go ChatHandler, which /v1/chat/completions also routes into), so only a turn at# index 0 displaces it.@@ -6665,7 +6659,6 @@ def _prepend_current_date_to_messages(*,include_api_key: bool = False,provider_type: str | None = None,- thread_id: Any = None,) -> list[dict]:"""Apply the date to an already-built message list for a provider Studio proxies to.@@ -6676,10 +6669,9 @@ def _prepend_current_date_to_messages("""if _date_gate_blocks(request, include_api_key):return messages- date_line, note = _current_date_parts(request, thread_id)+ date_line = current_date_prompt_line(request = request)if not date_line:return messages- messages = _append_current_date_note(messages, note = note)copied = [dict(msg) for msg in messages]stated = Falserefreshed = False@@ -27423,7 +27415,6 @@ async def _proxy_to_external_provider(chat_messages,request,include_api_key = bool(studio_tool_payloads),- thread_id = getattr(payload, "thread_id", None),)cancel_event = threading.Event()cancel_keys = tuple(@@ -27796,10 +27787,6 @@ async def _proxy_to_external_provider(request,include_api_key = run_studio_tool_loop,provider_type = None if _external_nudge else provider_type,- # a thread's date note would sit on the cache breakpoint and move off it next turn.- thread_id = None- if caches_at_the_last_block(provider_type, model, payload.enable_prompt_caching)- else getattr(payload, "thread_id", None),)if _external_nudge:chat_messages = _append_to_system_message(chat_messages, _external_nudge)@@ -29513,15 +29500,7 @@ async def produce_openai_chat_completions(system_prompt, chat_messages, _ = await _extract_content_parts_async(payload.messages)- system_prompt = _apply_current_date_prompt(- system_prompt, request, thread_id = getattr(payload, "thread_id", None)- )- chat_messages = _append_current_date_note(- chat_messages,- request,- thread_id = getattr(payload, "thread_id", None),- system_prompt = system_prompt,- )+ system_prompt = _audio_input_system_prompt(system_prompt, request)except _DecodedAudioTooLongError as e:# A limit the caller can act on, not a server fault.api_monitor.fail(monitor_id, str(e))@@ -29984,14 +29963,20 @@ async def produce_openai_chat_completions(payload.messages, keep_tool_images = True)# applied once so both backends inherit it, with or without tools, and never state it twice.- system_prompt = _apply_current_date_prompt(- system_prompt, request, thread_id = getattr(payload, "thread_id", None)+ _user_system_prompt = system_prompt+ # what makes generation render through the processor's template: an attachment, a clip, or an+ # MCP picture it really promotes, checked off the loop as the envelope can be 12 MB.+ _renders_media = (+ _request_has_attached_image(payload)+ or _request_has_video(payload)+ or (+ _request_has_promotable_mcp_images(payload, exact = False)+ and await asyncio.to_thread(_request_has_promotable_mcp_images, payload)+ ))- chat_messages = _append_current_date_note(- chat_messages,- request,- thread_id = getattr(payload, "thread_id", None),- system_prompt = system_prompt,+ _date_controls = _template_controls(payload)+ system_prompt = _apply_current_date_prompt(+ system_prompt, request, image = _renders_media, controls = _date_controls)if not chat_messages:@@ -30063,12 +30048,6 @@ async def produce_openai_chat_completions(_gguf_replayed_image_parts,)gguf_messages = _set_or_prepend_system_message(gguf_messages, system_prompt)- gguf_messages = _append_current_date_note(- gguf_messages,- request,- thread_id = getattr(payload, "thread_id", None),- system_prompt = system_prompt,- )image_b64 = Nonefor audio_b64, audio_format in prepared_audio:_inject_audio_part(gguf_messages, audio_b64, audio_format)@@ -30219,11 +30198,14 @@ async def produce_openai_chat_completions()if _wants_multiple_choices(payload):raise _reject_unsupported_n("GGUF tool chat completions")+ # main's tool turn: the date and the caller's prompt, not the template's chat default.system_prompt = _apply_current_date_prompt(- system_prompt,+ _user_system_prompt,request,include_api_key = True,- thread_id = getattr(payload, "thread_id", None),+ template_default = False,+ tools = True,+ controls = _date_controls,)gguf_messages = _set_or_prepend_system_message(gguf_messages, system_prompt)# ── Tool-use system prompt nudge ──────────────────────@@ -31913,12 +31895,6 @@ async def produce_openai_chat_completions(payload.messages, _legacy_distinct):chat_messages, served_images = _msgs, _payloads- chat_messages = _append_current_date_note(- chat_messages,- request,- thread_id = getattr(payload, "thread_id", None),- system_prompt = system_prompt,- )# Decode image (from content parts OR legacy field)image_b64 = extracted_image_b64 or payload.image_base64@@ -31948,13 +31924,6 @@ async def produce_openai_chat_completions()if message.get("role") not in ("system", "developer")]- # Rebuilt from the payload, so the date note added to the original messages is gone.- chat_messages = _append_current_date_note(- chat_messages,- request,- thread_id = getattr(payload, "thread_id", None),- system_prompt = system_prompt,- )if any(isinstance(message.get("content"), list)and any(part.get("type") == "image_url" for part in message["content"])@@ -32360,10 +32329,13 @@ async def produce_openai_chat_completions(_sf_nudge = _apply_compaction_nudge(_sf_nudge, _sf_tools_to_use)_sf_system_prompt = _apply_current_date_prompt(- system_prompt,+ _user_system_prompt,request,include_api_key = True,- thread_id = getattr(payload, "thread_id", None),+ template_default = False,+ image = _sf_has_any_image or _video_clip is not None,+ tools = True,+ controls = _date_controls,)if _sf_nudge:if _sf_system_prompt:@@ -32996,6 +32968,34 @@ async def produce_openai_chat_completions(# Re-derive from payload.messages so tool_calls / role="tool" history# survives templating; fold system/developer into one leading system# message (templates reject "developer") and clear prompt to avoid a dup.+ #+ # tool_choice="none": keep history templating but advertise no tools+ # (heal_gate is off, markup would relay as prose). A forced function+ # narrows templating to that one schema. Both mirror the GGUF path,+ # where llama-server honors tool_choice itself.+ _sf_tc = payload.tool_choice+ _sf_forced = None+ if isinstance(_sf_tc, dict) and isinstance(_sf_tc.get("function"), dict):+ _sf_forced = _sf_tc["function"].get("name")+ if _sf_tc == "none":+ _sf_client_catalog = None+ elif isinstance(_sf_forced, str):+ _sf_client_catalog = [+ t+ for t in payload.tools or []+ if isinstance(t, dict)+ and isinstance(t.get("function"), dict)+ and t["function"].get("name") == _sf_forced+ ] or None+ else:+ _sf_client_catalog = payload.tools+ _sf_client_system_prompt = _apply_current_date_prompt(+ _user_system_prompt,+ request,+ image = _sf_has_any_image or _video_clip is not None,+ tools = bool(_sf_client_catalog),+ controls = _date_controls,+ )if served_images:# One pass over the conversation this renders, so markers and payloads stay in step._sf_rebuilt, _sf_payloads = _conversation_with_image_markers(@@ -33013,13 +33013,8 @@ async def produce_openai_chat_completions(gen_kwargs["images"] = await _decode_request_images(backend, _sf_rebuilt_images, dict(zip(served_images, images)), reject = _reject)- gen_kwargs["messages"] = _append_current_date_note(- _set_or_prepend_system_message(- _structured_tool_history_for_local_template(_sf_rebuilt), system_prompt- ),- request,- thread_id = getattr(payload, "thread_id", None),- system_prompt = system_prompt,+ gen_kwargs["messages"] = _set_or_prepend_system_message(+ _structured_tool_history_for_local_template(_sf_rebuilt), _sf_client_system_prompt)else:#@@ -33043,11 +33038,8 @@ async def produce_openai_chat_completions(trim_mcp_image_turns(_sf_rebuilt, _sf_rebuilt_images, limit = _MCP_MAX_TOTAL_MODEL_IMAGES - 1)- gen_kwargs["messages"] = _append_current_date_note(- _set_or_prepend_system_message(_sf_rebuilt, system_prompt),- request,- thread_id = getattr(payload, "thread_id", None),- system_prompt = system_prompt,+ gen_kwargs["messages"] = _set_or_prepend_system_message(+ _sf_rebuilt, _sf_client_system_prompt)gen_kwargs["images"] = _sf_rebuilt_images or None# Mark the turn that owns the image so the newest-user-turn scan does not move an@@ -33062,26 +33054,7 @@ async def produce_openai_chat_completions(gen_kwargs["messages"], _sf_image_ordinal)gen_kwargs["system_prompt"] = ""- # tool_choice="none": keep history templating but advertise no tools- # (heal_gate is off, markup would relay as prose). A forced function- # narrows templating to that one schema. Both mirror the GGUF path,- # where llama-server honors tool_choice itself.- _sf_tc = payload.tool_choice- _sf_forced = None- if isinstance(_sf_tc, dict) and isinstance(_sf_tc.get("function"), dict):- _sf_forced = _sf_tc["function"].get("name")- if _sf_tc == "none":- gen_kwargs["tools"] = None- elif isinstance(_sf_forced, str):- gen_kwargs["tools"] = [- t- for t in payload.tools or []- if isinstance(t, dict)- and isinstance(t.get("function"), dict)- and t["function"].get("name") == _sf_forced- ] or None- else:- gen_kwargs["tools"] = payload.tools+ gen_kwargs["tools"] = _sf_client_catalogelif _sf_renders_image and not sf_mcp_images:# The plain route too: later turns then share the prefix that holds the image.#@@ -39001,15 +38974,21 @@ async def _mlx_count_chat_tokens(payload, request = None) -> Optional[JSONResponsystem_prompt, messages, _image = await _extract_content_parts_async(payload.messages)# The completion applies this once for both non-GGUF backends before it branches.# Only with a request: the helper's requestless mode injects the date unconditionally.+ _user_system_prompt = system_prompt+ # what makes generation render through the processor's template: an attachment, a clip, or an+ # MCP picture it really promotes, checked off the loop as the envelope can be 12 MB.+ _renders_media = (+ _request_has_attached_image(payload)+ or _request_has_video(payload)+ or (+ _request_has_promotable_mcp_images(payload, exact = False)+ and await asyncio.to_thread(_request_has_promotable_mcp_images, payload)+ )+ )+ _date_controls = _template_controls(payload)if request is not None:system_prompt = _apply_current_date_prompt(- system_prompt, request, thread_id = getattr(payload, "thread_id", None)- )- messages = _append_current_date_note(- messages,- request,- thread_id = getattr(payload, "thread_id", None),- system_prompt = system_prompt,+ system_prompt, request, image = _renders_media, controls = _date_controls)from state.tool_policy import get_tool_policy as _get_tool_policy_mlx@@ -39125,12 +39104,16 @@ async def _mlx_count_chat_tokens(payload, request = None) -> Optional[JSONRespon),vision = False,)- messages = _append_current_date_note(- _set_or_prepend_system_message(messages, system_prompt),- request,- thread_id = getattr(payload, "thread_id", None),- system_prompt = system_prompt,- )+ _client_system_prompt = _user_system_prompt+ if request is not None:+ _client_system_prompt = _apply_current_date_prompt(+ _user_system_prompt,+ request,+ image = _renders_media,+ tools = bool(_tools_to_use),+ controls = _date_controls,+ )+ messages = _set_or_prepend_system_message(messages, _client_system_prompt)system_prompt = ""elif _tools_to_use:# A PENDING turn is the shape this loop answers from exactly these messages, splicing@@ -39184,10 +39167,13 @@ async def _mlx_count_chat_tokens(payload, request = None) -> Optional[JSONRespon# so without this the count is short exactly that line for these completions.if request is not None:system_prompt = _apply_current_date_prompt(- system_prompt,+ _user_system_prompt,request,include_api_key = True,- thread_id = getattr(payload, "thread_id", None),+ template_default = False,+ image = _renders_media,+ tools = True,+ controls = _date_controls,)if _nudge:system_prompt = (system_prompt.rstrip() + "\n\n" + _nudge) if system_prompt else _nudge@@ -39392,18 +39378,13 @@ async def chat_count_tokens(openai_messages = _coalesce_consecutive_user_turns(openai_messages)_system_prompt, _, _ = await _extract_content_parts_async(payload.messages)# the verbatim passthrough carries no date line, so counting one here would overcount it.+ _user_system_prompt = _system_prompt+ _date_controls = _template_controls(payload)if not _takes_passthrough:_system_prompt = _apply_current_date_prompt(- _system_prompt, request, thread_id = getattr(payload, "thread_id", None)+ _system_prompt, request, controls = _date_controls)openai_messages = _set_or_prepend_system_message(openai_messages, _system_prompt)- if not _takes_passthrough:- openai_messages = _append_current_date_note(- openai_messages,- request,- thread_id = getattr(payload, "thread_id", None),- system_prompt = _system_prompt,- )# A PENDING turn (unanswered user message or tool result) is the one shape the tool loop# answers from exactly these messages, splicing in whatever build_rag_autoinject retrieves --@@ -39471,10 +39452,12 @@ async def chat_count_tokens(openai_messages = _set_or_prepend_system_message(openai_messages,_apply_current_date_prompt(- _system_prompt,+ _user_system_prompt,request,include_api_key = True,- thread_id = getattr(payload, "thread_id", None),+ template_default = False,+ tools = True,+ controls = _date_controls,),)_count_nudge = await _apply_rag_nudge(@@ -39723,7 +39706,6 @@ async def anthropic_count_tokens(openai_messages,request,include_api_key = _count_server_tools,- thread_id = getattr(payload, "thread_id", None),)if _count_server_tools:openai_tools = _count_selected_server_tools@@ -40117,7 +40099,6 @@ async def anthropic_messages(openai_messages,request,include_api_key = server_tools,- thread_id = getattr(payload, "thread_id", None),)# Anthropic tool_choice.disable_parallel_tool_use caps the response to adiff --git a/studio/backend/tests/test_context_overflow_truncation.py b/studio/backend/tests/test_context_overflow_truncation.pyindex ee51b9724d3..c7fbad6cda3 100644--- a/studio/backend/tests/test_context_overflow_truncation.py+++ b/studio/backend/tests/test_context_overflow_truncation.py@@ -1676,35 +1676,6 @@ def test_a_pin_is_not_charged_for_a_tool_exchange_it_does_not_hold():assert instruction_pin.pinned_instruction_ids(with_reply, groups = 2, max_tokens = 1024) == set()-def test_an_evicted_first_turn_hands_its_date_note_to_the_oldest_kept_turn():- from core.inference import llama_cpp-- first = {"role": "user", "content": "[Current date: 2026-10-01]\n\n" + "A" * 400}- kept = {"role": "user", "content": "C" * 200}- messages = [- first,- {"role": "assistant", "content": "B" * 400},- kept,- {"role": "assistant", "content": "D" * 100},- {"role": "user", "content": "what is the date?"},- ]- counter = lambda candidate: sum( # noqa: E731- len(str(message.get("content", ""))) for message in candidate- )- fitted, info = llama_cpp._fit_with_instruction_pins(- messages, context_length = 700, max_tokens = 64, count_tokens = counter- )-- assert info is not None and info["dropped_messages"] == 2- assert fitted[0] is kept- assert kept["content"] == "[Current date: 2026-10-01]\n\n" + "C" * 200- assert info["prompt_tokens_after"] == counter(fitted)- again, _ = llama_cpp._fit_with_instruction_pins(- messages, context_length = 700, max_tokens = 64, count_tokens = counter- )- assert again[0]["content"].count("[Current date:") == 1--def test_a_date_note_that_would_overflow_the_fit_is_not_moved():from core.inference import llama_cppdiff --git a/studio/backend/tests/test_current_date_prompt_settings.py b/studio/backend/tests/test_current_date_prompt_settings.pyindex 91fb760d667..da4faab67b9 100644--- a/studio/backend/tests/test_current_date_prompt_settings.py+++ b/studio/backend/tests/test_current_date_prompt_settings.py@@ -400,274 +400,354 @@ class TestResearchSystemPrompt:assert _system_prompt_with_instructions("BASE", {"currentDate": ""}) == "BASE"-class TestConversationStartDate:- @staticmethod- def _threads(monkeypatch, threads):- import storage.studio_db as studio_db- monkeypatch.setattr(studio_db, "get_chat_thread", lambda thread_id: threads.get(thread_id))-- def test_start_date_is_the_creation_day_in_the_browser_zone(self, monkeypatch):- # 2026-08-15T02:00Z is still the 14th in New York.- created = int(datetime(2026, 8, 15, 2, tzinfo = timezone.utc).timestamp() * 1000)- self._threads(monkeypatch, {"t": {"createdAt": created}})- request = _types.SimpleNamespace(headers = {"x-unsloth-timezone": "America/New_York"})- assert current_date_settings.conversation_start_date("t", request) == date(2026, 8, 14)-- def test_a_fork_keeps_its_root_threads_start_date(self, monkeypatch):- day = lambda d: int(datetime(2026, 8, d, 12, tzinfo = timezone.utc).timestamp() * 1000)- self._threads(- monkeypatch,- {- "root": {"createdAt": day(1)},- "mid": {"createdAt": day(5), "forkedFromThreadId": "root"},- "leaf": {"createdAt": day(9), "forkedFromThreadId": "mid"},- "orphan": {"createdAt": day(9), "forkedFromThreadId": "gone"},- "loop": {"createdAt": day(9), "forkedFromThreadId": "loop"},- },- )- request = _types.SimpleNamespace(headers = {"x-unsloth-timezone": "UTC"})- start = current_date_settings.conversation_start_date- assert start("leaf", request) == date(2026, 8, 1)- assert start("orphan", request) == date(2026, 8, 9)- assert start("loop", request) == date(2026, 8, 9)-- def test_a_deep_fork_chain_still_reaches_the_root(self, monkeypatch):- root = int(datetime(2026, 8, 1, 12, tzinfo = timezone.utc).timestamp() * 1000)- later = int(datetime(2026, 8, 9, 12, tzinfo = timezone.utc).timestamp() * 1000)- threads = {"t0": {"createdAt": root}}- for i in range(1, 40):- threads[f"t{i}"] = {"createdAt": later, "forkedFromThreadId": f"t{i - 1}"}- self._threads(monkeypatch, threads)- request = _types.SimpleNamespace(headers = {"x-unsloth-timezone": "UTC"})- assert current_date_settings.conversation_start_date("t39", request) == date(2026, 8, 1)-- @pytest.mark.parametrize("thread_id", [None, "", 7, "missing"])- def test_unknown_threads_have_no_start_date(self, monkeypatch, thread_id):- self._threads(monkeypatch, {})- assert current_date_settings.conversation_start_date(thread_id) is None-- def test_a_lookup_failure_has_no_start_date(self, monkeypatch):- import storage.studio_db as studio_db-- def _boom(_thread_id):- raise RuntimeError("db locked")-- monkeypatch.setattr(studio_db, "get_chat_thread", _boom)- assert current_date_settings.conversation_start_date("t") is None---class TestDateChangeNote:+_QWEN25_LIKE = (+ "{% if messages[0]['role'] == 'system' %}{% set sys = messages[0]['content'] %}"+ "{% set rest = messages[1:] %}{% else %}"+ "{% set sys = 'You are Qwen, a helpful assistant.' %}{% set rest = messages %}{% endif %}"+ "<|im_start|>system\n{{ sys }}<|im_end|>\n"+ "{% for m in rest %}<|im_start|>{{ m['role'] }}\n{{ m['content'] }}<|im_end|>\n{% endfor %}"+)+_CHATML = (+ "{% for m in messages %}<|im_start|>{{ m['role'] }}\n{{ m['content'] }}<|im_end|>\n"+ "{% endfor %}"+)+_GEMMA_LIKE = (+ "{% if messages[0]['role'] == 'system' %}{% set first = messages[0]['content'] + '\n\n' %}"+ "{% set rest = messages[1:] %}{% else %}{% set first = '' %}{% set rest = messages %}{% endif %}"+ "{% for m in rest %}<start_of_turn>{{ m['role'] }}\n"+ "{% if loop.first %}{{ first }}{% endif %}{{ m['content'] }}<end_of_turn>\n{% endfor %}"+)+++_REFUSES_SYSTEM = (+ "{% if messages[0]['role'] == 'system' %}{{ raise_exception('no system') }}{% endif %}"+ + _CHATML+)+_DROPS_SYSTEM = "{% for m in messages if m['role'] != 'system' %}{{ m['content'] }}{% endfor %}"+_DAY = date(2026, 10, 4)+++def _turn(+ template,+ tools = False,+ controls = (),+):+ return current_date_settings.template_system_turn(template, _DAY, tools, controls)+++class TestTemplateSystemTurn:+ def test_a_template_with_its_own_default_returns_it(self):+ assert _turn(_QWEN25_LIKE) == (True, "You are Qwen, a helpful assistant.")++ def test_a_template_with_generation_blocks_keeps_its_default(self):+ template = _QWEN25_LIKE.replace(+ "{{ m['content'] }}",+ "{% if m['role'] == 'assistant' %}{% generation %}{{ m['content'] }}"+ "{% endgeneration %}{% else %}{{ m['content'] }}{% endif %}",+ )+ assert _turn(template) == (True, "You are Qwen, a helpful assistant.")++ @pytest.mark.parametrize("template", [_CHATML, _GEMMA_LIKE, None, "", "{% if %}"])+ def test_templates_without_a_default_return_nothing(self, template):+ assert _turn(template) == (True, "")++ def test_a_default_that_dates_itself_is_dated_for_the_given_day(self):+ dated = _QWEN25_LIKE.replace(+ "You are Qwen, a helpful assistant.",+ "Today is ' + strftime_now('%B %d, %Y') + ', yesterday was the '"+ " + ((strftime_now('%d') | int) - 1) | string + 'th.",+ )+ assert _turn(dated) == (True, "Today is October 04, 2026, yesterday was the 3th.")++ def test_a_template_that_reads_content_parts_returns_its_default(self):+ parts = (+ "{% for m in messages if m['role'] == 'system' %}{% else %}<|system|>\nBe helpful.\n"+ "{% endfor %}{% for m in messages %}<|{{ m['role'] }}|>\n"+ "{{ m['content'][0]['text'] }}\n{% endfor %}"+ )+ assert _turn(parts) == (True, "Be helpful.")++ @pytest.mark.parametrize("template", [_REFUSES_SYSTEM, _DROPS_SYSTEM])+ def test_a_template_without_a_system_turn_takes_none(self, template):+ assert _turn(template) == (False, None)++ def test_a_tool_request_is_probed_with_a_catalog(self):+ tools_only = (+ "{% if tools and messages[0]['role'] == 'system' %}{{ raise_exception('no') }}"+ "{% endif %}" + _CHATML+ )+ assert _turn(tools_only) == (True, "")+ assert _turn(tools_only, True) == (False, None)++ def test_absent_tools_and_documents_are_passed_as_none_like_transformers(self):+ template = (+ "{% if messages[0]['role'] == 'system' %}{% set sys = messages[0]['content'] %}"+ "{% set rest = messages[1:] %}{% else %}"+ "{% if tools is none and documents is none %}"+ "{% set sys = 'You are a helpful function-calling assistant.' %}"+ "{% else %}{% set sys = '' %}{% endif %}{% set rest = messages %}{% endif %}"+ "<system>{{ sys }}</system>"+ "{% for m in rest %}<{{ m['role'] }}>{{ m['content'] }}</{{ m['role'] }}>"+ "{% endfor %}"+ )+ assert _turn(template) == (True, "You are a helpful function-calling assistant.")++ def test_a_template_is_probed_with_the_reasoning_controls(self):+ thinking_only = (+ "{% if enable_thinking and messages[0]['role'] == 'system' %}"+ "{{ raise_exception('no') }}{% endif %}" + _CHATML+ )+ assert _turn(thinking_only) == (True, "")+ assert _turn(thinking_only, controls = (("enable_thinking", True),)) == (False, None)++ @pytest.mark.parametrize("token", ["bos_token", "eos_token", "pad_token", "sep_token"])+ def test_a_default_carrying_a_control_token_is_not_replayed(self, token):+ with_token = _QWEN25_LIKE.replace(+ "{% set sys = 'You are Qwen, a helpful assistant.' %}",+ "{% set sys = " + token + " + 'You are Qwen, a helpful assistant.' %}",+ )+ assert _turn(with_token) == (True, None)++ def test_a_default_the_template_rewrites_is_not_replayed(self):+ escaped = _QWEN25_LIKE.replace(+ "'You are Qwen, a helpful assistant.'", "'Say \"hi\".'"+ ).replace("{{ sys }}", "{{ sys | tojson }}")+ assert _turn(escaped) == (True, None)+ plain = _QWEN25_LIKE.replace("{{ sys }}", "{{ sys | tojson }}")+ assert _turn(plain) == (True, "You are Qwen, a helpful assistant.")++ def test_a_structurally_different_system_branch_is_not_treated_as_default_free(self):+ structural = (+ "{% if messages[0]['role'] == 'system' %}"+ "{% set sys = 'Think deeply.\\n\\n' + messages[0]['content'] %}"+ "{% set rest = messages[1:] %}{% else %}"+ "{% set sys = 'You are helpful. Think deeply.' %}{% set rest = messages %}"+ "{% endif %}[SYSTEM]{{ sys }}[/SYSTEM]"+ "{% for m in rest %}{{ m['role'] }}:{{ m['content'] }}{% endfor %}"+ )+ assert _turn(structural) == (True, None)++ def test_default_words_inside_a_different_system_branch_are_not_treated_as_absent(self):+ structural = (+ "{% if messages[0]['role'] == 'system' %}"+ "{% set sys = 'Always follow policy. ' + messages[0]['content'] %}"+ "{% set rest = messages[1:] %}{% else %}"+ "{% set sys = 'A policy.' %}{% set rest = messages %}{% endif %}"+ "<system>{{ sys }}</system>"+ "{% for m in rest %}<{{ m['role'] }}>{{ m['content'] }}</{{ m['role'] }}>"+ "{% endfor %}"+ )+ assert _turn(structural) == (True, None)++ def test_transformers_tojson_does_not_html_escape_a_default(self):+ html = _QWEN25_LIKE.replace(+ "You are Qwen, a helpful assistant.", "Use <assistant> & answer questions."+ ).replace("{{ sys }}", "{{ sys | tojson }}")+ assert _turn(html) == (True, "Use <assistant> & answer questions.")+++class TestDateStaysInTheSystemTurn:@pytest.fixture(autouse = True)def _clock(self, monkeypatch):import routes.inference as inference- self.today = "2026-08-15"- self.started = date(2026, 8, 15)monkeypatch.setattr(inference,"current_date_prompt_line",- lambda **_kwargs: f"The current date is {self.today}.",- )- monkeypatch.setattr(- inference,- "conversation_start_date",- lambda thread_id, _request = None: self.started if thread_id == "t" else None,+ lambda **_kwargs: "The current date is 2026-10-04.",)monkeypatch.setattr(inference, "_request_has_api_key", lambda _request: False)+ monkeypatch.setattr(inference, "_local_template_system_turn", lambda *_a: (True, ""))self.inference = inference- def _proxy(- self,- messages,- thread_id = "t",- **kwargs,- ):- return self.inference._prepend_current_date_to_messages(- messages, object(), thread_id = thread_id, **kwargs+ def test_without_a_system_prompt_the_date_is_its_own_system_turn(self):+ assert (+ self.inference._apply_current_date_prompt("", object())+ == "The current date is 2026-10-04.")- def test_same_day_is_byte_identical_to_a_threadless_request(self):- messages = [{"role": "system", "content": "Be terse."}, {"role": "user", "content": "hi"}]- assert self._proxy(messages) == self._proxy(messages, thread_id = None)- assert self.inference._apply_current_date_prompt("Be terse.", object(), thread_id = "t") == (- "The current date is 2026-08-15.\n\nBe terse."+ def test_a_template_default_system_prompt_is_kept_after_the_date(self, monkeypatch):+ monkeypatch.setattr(+ self.inference, "_local_template_system_turn", lambda *_a: (True, "You are Qwen.")+ )+ assert self.inference._apply_current_date_prompt("", object()) == (+ "The current date is 2026-10-04.\n\nYou are Qwen."+ )+ assert self.inference._apply_current_date_prompt("Be terse.", object()) == (+ "The current date is 2026-10-04.\n\nBe terse.")- def test_midnight_keeps_the_system_turn_and_notes_the_newest_user_turn(self):- history = [- {"role": "system", "content": "Be terse."},- {"role": "user", "content": "first"},- {"role": "assistant", "content": "ok"},- {"role": "user", "content": "second"},- ]- before = self._proxy(history)- self.today = "2026-08-16"- after = self._proxy(history)- assert after[0] == before[0]- assert after[0]["content"] == "The current date is 2026-08-15.\n\nBe terse."- assert after[1:3] == history[1:3]- assert after[3]["content"] == "[Current date: 2026-08-16]\n\nsecond"- assert history[3]["content"] == "second"-- def test_next_request_after_midnight_restores_the_earlier_turn_to_its_own_text(self):- self.today = "2026-08-16"- turn1 = [{"role": "user", "content": "first"}]- sent1 = self._proxy(turn1)- turn2 = turn1 + [- {"role": "assistant", "content": "ok"},- {"role": "user", "content": "second"},- ]- sent2 = self._proxy(turn2)- assert sent2[0] == sent1[0]- assert sent1[1]["content"] == "[Current date: 2026-08-16]\n\nfirst"- assert sent2[1]["content"] == "first"- assert sent2[3]["content"] == "[Current date: 2026-08-16]\n\nsecond"-- def test_threadless_requests_still_refresh_to_today(self):- self.today = "2026-08-16"- out = self._proxy([{"role": "user", "content": "hi"}], thread_id = None)- assert out == [- {"role": "system", "content": "The current date is 2026-08-16."},- {"role": "user", "content": "hi"},- ]+ def test_a_template_default_that_dates_itself_is_dated_for_the_user(self, monkeypatch):+ monkeypatch.setattr(+ self.inference,+ "_local_template_system_turn",+ lambda today, *_a: (True, f"Today's Date: {today:%B %d, %Y}.\nYou are Granite."),+ )+ # the system turn the tool nudge is appended to, so a tool request keeps the date too.+ expected = (+ "The current date is 2026-10-04.\n\nToday's Date: October 04, 2026.\nYou are Granite."+ )+ assert self.inference._apply_current_date_prompt("", object()) == expected+ assert (+ self.inference._apply_current_date_prompt("", object(), include_api_key = True)+ == expected+ )- def test_a_stated_system_date_is_refreshed_to_the_start_date(self):- self.today = "2026-08-16"- out = self._proxy(- [- {"role": "system", "content": "The current date is 2026-08-16."},- {"role": "user", "content": "hi"},- ]+ def test_a_tool_turn_states_the_date_without_the_template_default(self, monkeypatch):+ monkeypatch.setattr(+ self.inference, "_local_template_system_turn", lambda *_a: (True, "You are Qwen.")+ )+ assert (+ self.inference._apply_current_date_prompt(+ "", object(), include_api_key = True, template_default = False+ )+ == "The current date is 2026-10-04.")- assert out[0]["content"] == "The current date is 2026-08-15."- assert out[1]["content"] == "[Current date: 2026-08-16]\n\nhi"- def test_note_leads_the_text_part_and_skips_tool_results(self):- self.today = "2026-08-16"- image = {"type": "image_url", "image_url": {"url": "data:image/png;base64,AA"}}- messages = [- {"role": "user", "content": [image, {"type": "text", "text": "what is this"}]},- {"role": "assistant", "content": "a cat"},- {"role": "user", "content": [{"type": "tool_result", "tool_use_id": "x"}]},- ]- out = self.inference._append_current_date_note(messages, object(), thread_id = "t")- assert out[2] is messages[2]- assert out[0]["content"] == [- image,- {"type": "text", "text": "[Current date: 2026-08-16]\n\nwhat is this"},- ]+ def test_a_template_without_a_system_turn_gets_no_date(self, monkeypatch):+ monkeypatch.setattr(+ self.inference, "_local_template_system_turn", lambda *_a: (False, None)+ )+ assert self.inference._apply_current_date_prompt("", object()) == ""+ # a system prompt the caller wrote is still theirs to send, dated.+ assert self.inference._apply_current_date_prompt("Be terse.", object()) == (+ "The current date is 2026-10-04.\n\nBe terse."+ )- def test_note_skips_a_tool_result_folded_into_a_user_turn(self):- from core.inference.anthropic_compat import fold_tool_results_into_user-- self.today = "2026-08-16"- messages = fold_tool_results_into_user(- [- {"role": "user", "content": "list files"},- {- "role": "assistant",- "content": "",- "tool_calls": [{"id": "c1", "function": {"name": "ls", "arguments": "{}"}}],- },- {"role": "tool", "tool_call_id": "c1", "content": "a.txt"},- ]- )- out = self.inference._append_current_date_note(messages, object(), thread_id = "t")- assert out[2] is messages[2]- assert out[0]["content"] == "[Current date: 2026-08-16]\n\nlist files"+ def test_an_audio_turn_keeps_its_transcription_instruction(self, monkeypatch):+ instruction = self.inference._AUDIO_INPUT_SYSTEM_PROMPT+ assert self.inference._audio_input_system_prompt("", object()) == (+ f"The current date is 2026-10-04.\n\n{instruction}"+ )+ assert self.inference._audio_input_system_prompt("Be terse.", object()) == (+ "The current date is 2026-10-04.\n\nBe terse."+ )+ monkeypatch.setattr(self.inference, "current_date_prompt_line", lambda **_kwargs: "")+ assert self.inference._audio_input_system_prompt("", object()) == instruction++ def test_a_managed_engine_gets_no_unrequested_system_turn(self, monkeypatch):+ from types import SimpleNamespace++ from core.inference import orchestrator++ monkeypatch.undo()+ info = {"engine": "vllm", "chat_template_info": {}}+ backend = SimpleNamespace(active_model_name = "served", models = {"served": info})+ monkeypatch.setattr(+ self.inference, "get_llama_cpp_backend", lambda: SimpleNamespace(is_loaded = False)+ )+ monkeypatch.setattr(orchestrator, "peek_inference_backend", lambda: backend)+ assert self.inference._local_template_system_turn(_DAY) == (False, None)+ assert self.inference._local_template_system_turn(_DAY, tools = True) == (False, None)@pytest.mark.parametrize(- "content",- ["", " ", [{"type": "image_url", "image_url": {"url": "data:image/png;base64,AA"}}]],+ "extra_args",+ [["--chat-template-file", "/srv/chat.jinja"], ["--chat-template=chatml"], ["--no-jinja"]],)- def test_a_media_only_turn_keeps_its_transcribe_or_describe_default(self, content):- from core.inference.chat_template_helpers import last_user_text+ def test_a_gguf_template_chosen_by_extra_args_gets_no_system_turn(+ self, monkeypatch, extra_args+ ):+ from types import SimpleNamespace- self.today = "2026-08-16"- messages = [{"role": "user", "content": content}]- out = self.inference._append_current_date_note(messages, object(), thread_id = "t")- assert out is messages- assert last_user_text(out) == ""-- def test_retrieval_queries_ignore_the_note(self):- from core.inference.tools import _last_user_text- self.today = "2026-08-16"- for content in ("refund policy", [{"type": "text", "text": "refund policy"}]):- noted = self.inference._append_current_date_note(- [{"role": "user", "content": content}], object(), thread_id = "t"- )- assert noted[0]["content"] != content- assert _last_user_text(noted) == "refund policy"+ monkeypatch.undo()+ llama = SimpleNamespace(+ is_loaded = True, chat_template = _CHATML, chat_template_override = None, extra_args = []+ )+ monkeypatch.setattr(self.inference, "get_llama_cpp_backend", lambda: llama)+ assert self.inference._local_template_system_turn(_DAY) == (True, "")+ llama.extra_args = extra_args+ assert self.inference._local_template_system_turn(_DAY) == (False, None)- def test_a_follow_up_coalesced_onto_a_folded_tool_result_gets_the_note(self):- from core.inference.anthropic_compat import fold_tool_results_into_user+ def test_a_tool_request_probes_the_tool_use_template(self, monkeypatch):+ from types import SimpleNamespace- self.today = "2026-08-16"- messages = self.inference._coalesce_consecutive_user_turns(- fold_tool_results_into_user(- [- {"role": "user", "content": "list files"},- {- "role": "assistant",- "content": "",- "tool_calls": [{"id": "c1", "function": {"name": "ls", "arguments": "{}"}}],- },- {"role": "tool", "tool_call_id": "c1", "content": "a.txt"},- {"role": "user", "content": "now read it"},- ]- )+ from core.inference import orchestrator++ monkeypatch.undo()+ named = [+ {"name": "default", "template": _CHATML},+ {"name": "tool_use", "template": _REFUSES_SYSTEM},+ ]+ info = {"chat_template_info": {"template": named}}+ backend = SimpleNamespace(active_model_name = "hermes", models = {"hermes": info})+ monkeypatch.setattr(+ self.inference, "get_llama_cpp_backend", lambda: SimpleNamespace(is_loaded = False))- out = self.inference._append_current_date_note(messages, object(), thread_id = "t")- assert out[0] is messages[0]- assert out[-1]["content"].startswith("[Current date: 2026-08-16]\n\n")- assert out[-1]["content"].endswith("now read it")-- def test_a_future_creation_day_never_becomes_the_stated_start(self):- self.today = "2026-08-15"- self.started = date(2026, 8, 20)- out = self._proxy([{"role": "user", "content": "hi"}])- assert out == [- {"role": "system", "content": "The current date is 2026-08-15."},- {"role": "user", "content": "hi"},+ monkeypatch.setattr(orchestrator, "peek_inference_backend", lambda: backend)+ assert self.inference._local_template_system_turn(_DAY) == (True, "")+ assert self.inference._local_template_system_turn(_DAY, tools = True) == (False, None)++ def test_a_text_request_probes_the_mapped_template(self, monkeypatch):+ from types import SimpleNamespace++ from core.inference import orchestrator++ monkeypatch.undo()+ info = {"chat_template_info": {"template": _CHATML, "mapped_template": _QWEN25_LIKE}}+ backend = SimpleNamespace(active_model_name = "mapped", models = {"mapped": info})+ monkeypatch.setattr(+ self.inference, "get_llama_cpp_backend", lambda: SimpleNamespace(is_loaded = False)+ )+ monkeypatch.setattr(orchestrator, "peek_inference_backend", lambda: backend)+ assert self.inference._local_template_system_turn(_DAY) == (+ True,+ "You are Qwen, a helpful assistant.",+ )+ # the mapper runs on the text path only; an image render keeps the loaded template.+ assert self.inference._local_template_system_turn(_DAY, True) == (True, "")++ def test_an_image_request_probes_the_processor_template(self, monkeypatch):+ from types import SimpleNamespace++ from core.inference import orchestrator++ monkeypatch.undo()+ info = {"chat_template_info": {"template": _CHATML, "processor_template": _QWEN25_LIKE}}+ backend = SimpleNamespace(active_model_name = "vlm", models = {"vlm": info})+ monkeypatch.setattr(+ self.inference, "get_llama_cpp_backend", lambda: SimpleNamespace(is_loaded = False)+ )+ monkeypatch.setattr(orchestrator, "peek_inference_backend", lambda: backend)+ assert self.inference._local_template_system_turn(_DAY) == (True, "")+ assert self.inference._local_template_system_turn(_DAY, True) == (+ True,+ "You are Qwen, a helpful assistant.",+ )+ info["chat_template_info"]["processor_template"] = _REFUSES_SYSTEM+ assert self.inference._local_template_system_turn(_DAY, True) == (False, None)++ def test_a_named_template_list_is_probed_through_its_default(self, monkeypatch):+ from types import SimpleNamespace++ from core.inference import orchestrator++ monkeypatch.undo()+ named = [+ {"name": "default", "template": _QWEN25_LIKE},+ {"name": "tool_use", "template": _CHATML},]+ backend = SimpleNamespace(+ active_model_name = "hermes",+ models = {"hermes": {"chat_template_info": {"template": named}}},+ )+ monkeypatch.setattr(+ self.inference, "get_llama_cpp_backend", lambda: SimpleNamespace(is_loaded = False)+ )+ monkeypatch.setattr(orchestrator, "peek_inference_backend", lambda: backend)+ assert self.inference._local_template_system_turn(_DAY) == (+ True,+ "You are Qwen, a helpful assistant.",+ )- def test_without_a_system_prompt_the_first_turn_keeps_the_start_date(self):- self.today = "2026-08-16"+ def test_a_chat_started_days_ago_keeps_every_user_turn_verbatim(self):history = [- {"role": "user", "content": "first"},+ {"role": "user", "content": "Mike and Alexis are in bed."},{"role": "assistant", "content": "ok"},- {"role": "user", "content": "second"},- ]- out = self.inference._append_current_date_note(- history, object(), thread_id = "t", system_prompt = ""- )- assert out[0]["content"] == "[Current date: 2026-08-15]\n\nfirst"- assert out[2]["content"] == "[Current date: 2026-08-16]\n\nsecond"- only = self.inference._append_current_date_note(- history[:1], object(), thread_id = "t", system_prompt = ""- )- assert only == [{"role": "user", "content": "[Current date: 2026-08-16]\n\nfirst"}]-- def test_note_is_not_added_twice(self):- self.today = "2026-08-16"- once = self.inference._append_current_date_note(- [{"role": "user", "content": "hi"}], object(), thread_id = "t"- )- assert self.inference._append_current_date_note(once, object(), thread_id = "t") is once-- def test_api_key_callers_never_use_a_thread_start_date(self, monkeypatch):- monkeypatch.setattr(self.inference, "_request_has_api_key", lambda _request: True)- monkeypatch.setattr(self.inference, "_request_is_internal_workflow", lambda _r: False)- self.today = "2026-08-16"- out = self._proxy([{"role": "user", "content": "hi"}], include_api_key = True)- assert out == [- {"role": "system", "content": "The current date is 2026-08-16."},- {"role": "user", "content": "hi"},+ {"role": "user", "content": "Turn the story back to dinner."},]+ out = self.inference._prepend_current_date_to_messages(history, object())+ assert out == [{"role": "system", "content": "The current date is 2026-10-04."}, *history]- def test_disabled_setting_adds_no_note(self, monkeypatch):+ def test_a_disabled_setting_adds_nothing(self, monkeypatch):monkeypatch.setattr(self.inference, "current_date_prompt_line", lambda **_kwargs: "")- messages = [{"role": "user", "content": "hi"}]- assert (- self.inference._append_current_date_note(messages, object(), thread_id = "t") is messages- )+ assert self.inference._apply_current_date_prompt("", object()) == ""diff --git a/studio/backend/tests/test_external_hosted_tool_passthrough.py b/studio/backend/tests/test_external_hosted_tool_passthrough.pyindex 8ee0b70dee9..1ad91005377 100644--- a/studio/backend/tests/test_external_hosted_tool_passthrough.py+++ b/studio/backend/tests/test_external_hosted_tool_passthrough.py@@ -750,26 +750,22 @@ def test_the_conversation_id_reaches_the_provider_client(monkeypatch):@pytest.mark.parametrize(- "provider_type, model, caching, noted",+ "provider_type, model, caching",[- ("anthropic", "claude-sonnet-4-6", None, False),- ("openrouter", "anthropic/claude-sonnet-4.6", None, False),- ("openrouter", "~anthropic/claude-opus-latest", True, False),- ("anthropic", "claude-sonnet-4-6", False, True),- ("openrouter", "anthropic/claude-sonnet-4.6", False, True),- ("openrouter", "deepseek/deepseek-v3.2", None, True),+ ("anthropic", "claude-sonnet-4-6", None),+ ("openrouter", "anthropic/claude-sonnet-4.6", None),+ ("openrouter", "~anthropic/claude-opus-latest", True),+ ("anthropic", "claude-sonnet-4-6", False),+ ("openrouter", "deepseek/deepseek-v3.2", None),],)-def test_a_thread_from_yesterday_keeps_the_claude_cache_breakpoint_stable(- monkeypatch, provider_type, model, caching, noted+def test_a_thread_from_yesterday_leaves_every_user_turn_verbatim(+ monkeypatch, provider_type, model, caching):- from datetime import date-inf = _install(monkeypatch, provider_type)monkeypatch.setattr(inf, "current_date_prompt_line", lambda **_k: "The current date is 2026-08-16.")- monkeypatch.setattr(inf, "conversation_start_date", lambda *_a, **_k: date(2026, 8, 15))monkeypatch.setattr(inf, "_request_has_api_key", lambda _request: False)history = [{"role": "user", "content": "first"},@@ -788,4 +784,8 @@ def test_a_thread_from_yesterday_keeps_the_claude_cache_breakpoint_stable()sent = FakeExternalClient.last["passthrough"]assert sent["thread_id"] == "thread-7"- assert sent["messages"][-1]["content"].startswith("[Current date: ") is noted+ assert [m["content"] for m in sent["messages"] if m["role"] != "system"] == [+ "first",+ "ok",+ "second",+ ]diff --git a/studio/backend/tests/test_managed_engine_tools.py b/studio/backend/tests/test_managed_engine_tools.pyindex 3cf53ac8d5b..1a7c65f80f8 100644--- a/studio/backend/tests/test_managed_engine_tools.py+++ b/studio/backend/tests/test_managed_engine_tools.py@@ -555,10 +555,15 @@ def test_a_catalog_with_tool_choice_none_is_plain_chat(native):@pytest.mark.parametrize("tools", [False, True])-def test_managed_messages_keep_the_current_date_note(native, monkeypatch, tools):+def test_managed_messages_get_no_unrequested_date(native, monkeypatch, tools):+ from core.inference import orchestrator+backend, requests = native+ monkeypatch.setattr(orchestrator, "peek_inference_backend", lambda: backend)monkeypatch.setattr(api, "_date_gate_blocks", lambda *a: False)- monkeypatch.setattr(api, "_current_date_parts", lambda *a: ("", "[DATE NOTE]"))+ monkeypatch.setattr(+ api, "current_date_prompt_line", lambda **_k: "The current date is 2026-08-15."+ )seen = []plain = backend._responder@@ -572,9 +577,12 @@ def test_managed_messages_keep_the_current_date_note(native, monkeypatch, tools)enable_tools = False, **({"tools": [route_test.LOOKUP_TOOL]} if tools else {})))- messages = requests[-1]["messages"] if tools else seen[-1]- user = [m for m in messages if m["role"] == "user"][-1]- assert "[DATE NOTE]" in json.dumps(user["content"])+ if tools:+ sent = requests[-1]["messages"]+ else:+ sent = [*seen[-1], {"role": "system", "content": backend.calls[-1]["system_prompt"]}]+ # vLLM/SGLang render a template Studio never sees, so no system turn is made up for it.+ assert "2026-08-15" not in json.dumps(sent)@pytest.mark.parametrize("vision", [False, True])diff --git a/studio/backend/tests/test_mlx_inference_backend.py b/studio/backend/tests/test_mlx_inference_backend.pyindex 0196372d6f3..9ea81d9471c 100644--- a/studio/backend/tests/test_mlx_inference_backend.py+++ b/studio/backend/tests/test_mlx_inference_backend.py@@ -4753,6 +4753,15 @@ _TOOL_TEMPLATE = ("{% if m.tool_calls %}<tool_call>{{ m.tool_calls[0].function.name }}</tool_call>{% endif %}""{% endfor %}")+_DEFAULT_TOOL_TEMPLATE = (+ "{% if messages[0]['role'] == 'system' %}{% set system = messages[0]['content'] %}"+ "{% set rest = messages[1:] %}{% else %}{% set system = 'You are Qwen.' %}"+ "{% set rest = messages %}{% endif %}{{ system }}"+ "{% if tools %}{% for tool in tools %}{{ tool.function.name }}{% endfor %}{% endif %}"+ "{% for message in rest %}{{ message['content'] }}"+ "{% if message.tool_calls %}<tool_call>{{ message.tool_calls[0].function.name }}"+ "</tool_call>{% endif %}{% endfor %}"+)_NAMED_TOOL_TEMPLATE = {"default": _PLAIN_TEMPLATE, "tool_use": _TOOL_TEMPLATE}@@ -5490,10 +5499,49 @@ def test_an_mlx_count_prices_the_current_date_the_completion_prepends(monkeypatcline = current_date_prompt_line(request = interactive)assert line, "the harness must actually produce a date line"- note = f"[Current date: {line.removeprefix('The current date is ')[:-1]}]"- assert backend.messages[-1]["content"].startswith(- note- ), f"the count dropped the date the completion adds: {backend.messages!r}"+ assert (+ backend.system == line+ ), f"the count dropped the date the completion adds: {backend.system!r}"+ assert backend.messages == [{"role": "user", "content": "hi"}]+++def test_an_mlx_client_tool_count_keeps_the_template_default_after_the_date(monkeypatch):+ from starlette.datastructures import Headers++ from routes import inference as route++ monkeypatch.setattr(+ route,+ "_local_template_system_turn",+ lambda _today, image = False, tools = False, controls = (): (+ True,+ "You are Qwen." if tools else "",+ ),+ )+ interactive = SimpleNamespace(headers = Headers({}), query_params = {}, cookies = {})+ backend = _RenderRecordingBackend()+ _count_hi(+ monkeypatch,+ backend,+ template = _DEFAULT_TOOL_TEMPLATE,+ request = interactive,+ enable_tools = False,+ tools = [+ {+ "type": "function",+ "function": {+ "name": "lookup",+ "description": "Look up a value.",+ "parameters": {"type": "object", "properties": {}},+ },+ }+ ],+ )+ from routes.inference import current_date_prompt_line++ line = current_date_prompt_line(request = interactive)+ assert backend.system == ""+ assert backend.messages[0] == {"role": "system", "content": f"{line}\n\nYou are Qwen."}def test_an_mlx_count_prices_the_archive_tool_and_its_compaction_nudge(monkeypatch):diff --git a/studio/backend/tests/test_openai_auto_switch.py b/studio/backend/tests/test_openai_auto_switch.pyindex e4052745366..274ad2bbd69 100644--- a/studio/backend/tests/test_openai_auto_switch.py+++ b/studio/backend/tests/test_openai_auto_switch.py@@ -5208,10 +5208,14 @@ def test_chat_count_tokens_prices_the_current_date(monkeypatch):"current_date_prompt_line",lambda **_kwargs: "The current date is 2026-08-15.",)+ monkeypatch.setattr(inference_route, "_local_template_system_turn", lambda *_a: (True, ""))thread = [{"role": "user", "content": "hi"}]_counted_body(_count_request(thread))- assert counted["messages"] == [{"role": "user", "content": "[Current date: 2026-08-15]\n\nhi"}]+ assert counted["messages"] == [+ {"role": "system", "content": "The current date is 2026-08-15."},+ {"role": "user", "content": "hi"},+ ]# The passthrough forwards the caller's request verbatim, so counting a date it never sends# would overcount exactly those prompts.diff --git a/studio/backend/tests/test_openai_tool_passthrough.py b/studio/backend/tests/test_openai_tool_passthrough.pyindex f588bc532fe..ac98434e6ac 100644--- a/studio/backend/tests/test_openai_tool_passthrough.py+++ b/studio/backend/tests/test_openai_tool_passthrough.py@@ -2631,6 +2631,79 @@ class TestChatCompletionRequestToolFields:assert "confirm_tool_calls requires stream=true" in entry["error"]assert monitor.active_count() == 0+ def test_safetensors_client_tools_keep_the_template_default_after_the_date(self, monkeypatch):+ import routes.inference as inference_route++ from core.inference import chat_template_helpers++ captured = {}++ class _NoGGUFBackend:+ is_loaded = False+ supports_tools = False++ class _InferenceBackend:+ active_model_name = "test-model"+ models = {+ "test-model": {+ "is_vision": False,+ "chat_template_info": {"template": "tool-template"},+ "context_length": 4096,+ }+ }++ def generate_chat_response(self, **kwargs):+ captured.update(kwargs)+ yield "ok"++ def reset_generation_state(self, cancel_event = None):+ pass++ monkeypatch.setattr(+ inference_route,+ "_detect_safetensors_features",+ lambda *a, **k: {"supports_tools": True},+ )+ monkeypatch.setattr(+ inference_route,+ "_local_template_system_turn",+ lambda _today, image = False, tools = False, controls = (): (+ True,+ "You are Qwen." if tools else "",+ ),+ )+ monkeypatch.setattr(+ inference_route,+ "current_date_prompt_line",+ lambda **_kwargs: "The current date is 2026-10-04.",+ )+ monkeypatch.setattr(+ chat_template_helpers,+ "renderable_tool_catalog_for_targets",+ lambda tools, *_args, **_kwargs: tools,+ )+ monkeypatch.setattr(inference_route, "api_monitor", ApiMonitor(max_entries = 3))+ client = self._v1_client(monkeypatch, _NoGGUFBackend(), _InferenceBackend())+ resp = client.post(+ "/v1/chat/completions",+ json = {+ "messages": [{"role": "user", "content": "use client tool"}],+ "enable_tools": False,+ "tools": _lookup_tools(),+ },+ )++ assert resp.status_code == 200, resp.text+ assert captured["system_prompt"] == ""+ assert captured["messages"][:2] == [+ {+ "role": "system",+ "content": "The current date is 2026-10-04.\n\nYou are Qwen.",+ },+ {"role": "user", "content": "use client tool"},+ ]+ assert captured["tools"] == _lookup_tools()+def test_multiturn_tool_loop_messages(self):req = ChatCompletionRequest(messages = [@@ -6463,6 +6536,7 @@ class TestGgufVisionToolRouting:monkeypatch,date_line: str,messages = None,+ chat_template = None,) -> list[dict]:"""Run one non-tool GGUF completion with the current-date setting pinned."""import routes.inference as inf_mod@@ -6485,6 +6559,8 @@ class TestGgufVisionToolRouting:model_identifier = "test-gguf",context_length = 4096,generate_chat_completion = _generate,+ chat_template = chat_template,+ chat_template_override = None,)monkeypatch.setattr(inf_mod, "get_llama_cpp_backend", lambda: backend)# Pinned, not left to the host's stored setting, so the assertion is the same everywhere.@@ -6521,21 +6597,39 @@ class TestGgufVisionToolRouting:{"role": "user", "content": "hi"},]- def test_standard_gguf_without_a_system_prompt_keeps_the_template_default(self, monkeypatch):+ @pytest.mark.parametrize(+ ("chat_template", "system"),+ [+ (+ "{% if messages[0]['role'] == 'system' %}{% set s = messages[0]['content'] %}"+ "{% set rest = messages[1:] %}{% else %}{% set s = 'You are Qwen.' %}"+ "{% set rest = messages %}{% endif %}<|im_start|>system\n{{ s }}<|im_end|>\n"+ "{% for m in rest %}<|im_start|>{{ m['role'] }}\n{{ m['content'] }}<|im_end|>\n"+ "{% endfor %}",+ "The current date is 2026-08-15.\n\nYou are Qwen.",+ ),+ (+ "{% for m in messages %}<start_of_turn>{{ m['role'] }}\n{{ m['content'] }}"+ "<end_of_turn>\n{% endfor %}",+ "The current date is 2026-08-15.",+ ),+ ],+ )+ def test_standard_gguf_without_a_system_prompt_dates_a_system_turn(+ self, monkeypatch, chat_template, system+ ):+ history = [+ {"role": "user", "content": "first"},+ {"role": "assistant", "content": "ok"},+ {"role": "user", "content": "second"},+ ]sent = self._drive_standard_gguf(monkeypatch,"The current date is 2026-08-15.",- messages = [- {"role": "user", "content": "first"},- {"role": "assistant", "content": "ok"},- {"role": "user", "content": "second"},- ],+ messages = history,+ chat_template = chat_template,)- assert sent == [- {"role": "user", "content": "[Current date: 2026-08-15]\n\nfirst"},- {"role": "assistant", "content": "ok"},- {"role": "user", "content": "second"},- ]+ assert sent == [{"role": "system", "content": system}, *history]@pytest.mark.parametrize(("seed", "expected"),diff --git a/studio/backend/utils/current_date_prompt_settings.py b/studio/backend/utils/current_date_prompt_settings.pyindex 7633ac833a2..0540391095d 100644--- a/studio/backend/utils/current_date_prompt_settings.py+++ b/studio/backend/utils/current_date_prompt_settings.py@@ -6,6 +6,8 @@from __future__ import annotationsfrom datetime import date, datetime, timedelta, timezone+from functools import lru_cache+import jsonimport refrom typing import Anyfrom zoneinfo import ZoneInfo, ZoneInfoNotFoundError@@ -113,32 +115,144 @@ def current_date_prompt_line(today: date | None = None, request: Any = None) ->return f"{CURRENT_DATE_PROMPT_PREFIX}{resolved_date.isoformat()}."-def conversation_start_date(thread_id: Any, request: Any = None) -> date | None:- """Local date the thread (or the root of its fork chain) was created, None when unknown."""- if not isinstance(thread_id, str) or not thread_id:- return None- try:- from storage.studio_db import get_chat_thread-- thread = get_chat_thread(thread_id)- seen = {thread_id}- # a fork keeps its parent's history, so it keeps the parent's prompt prefix too.- while thread:- parent_id = thread.get("forkedFromThreadId")- if not parent_id or parent_id in seen:- break- seen.add(parent_id)- parent = get_chat_thread(parent_id)- if not parent:- break- thread = parent- created_ms = thread.get("createdAt") if thread else None- if isinstance(created_ms, bool) or not isinstance(created_ms, (int, float)):- return None- created = datetime.fromtimestamp(created_ms / 1000, timezone.utc)- except Exception:- return None- return _request_local_date(request, now = created)+_PROBE_SYSTEM = "UNSLOTH_DATE_PROBE_SYSTEM"+_PROBE_USER = "UNSLOTH_DATE_PROBE_USER"+# stand-ins for the tokenizer's control tokens, so a default that carries one can be told apart.+_PROBE_SPECIAL_TOKENS = {+ f"{name}_token": f"UNSLOTH_DATE_PROBE_{name.upper()}"+ for name in ("bos", "eos", "pad", "unk", "sep", "cls", "mask")+}+# a catalog a tool request's branch can render, for probing the template it selects.+PROBE_TOOLS = [+ {+ "type": "function",+ "function": {+ "name": "probe",+ "description": "Probe.",+ "parameters": {"type": "object", "properties": {}},+ },+ }+]+++def _render_probe(+ chat_template: str,+ messages: list[dict],+ today: date,+ tools: list | None = None,+ controls: dict | None = None,+) -> str:+ from jinja2.exceptions import TemplateError+ from jinja2.ext import Extension+ from jinja2.sandbox import ImmutableSandboxedEnvironment++ def raise_exception(message):+ raise TemplateError(message)++ def tojson(+ value,+ ensure_ascii = False,+ indent = None,+ separators = None,+ sort_keys = False,+ ):+ # Transformers replaces Jinja's HTML-safe filter, so templates render ordinary JSON rather+ # than escaping <, >, &, and apostrophes. The probe must compare the same bytes.+ return json.dumps(+ value,+ ensure_ascii = ensure_ascii,+ indent = indent,+ separators = separators,+ sort_keys = sort_keys,+ )++ class _GenerationTag(Extension):+ tags = {"generation"}++ def parse(self, parser):+ next(parser.stream)+ return parser.parse_statements(["name:endgeneration"], drop_needle = True)++ # Match the syntax accepted by transformers' chat-template renderer. The generation block only+ # marks assistant-token spans there, so it is a transparent wrapper for this text-only probe.+ env = ImmutableSandboxedEnvironment(+ trim_blocks = True,+ lstrip_blocks = True,+ extensions = [_GenerationTag, "jinja2.ext.loopcontrols"],+ )+ env.filters["tojson"] = tojson+ env.globals["raise_exception"] = raise_exception+ # the user's day, so a default that dates itself (or works out yesterday) is dated for them.+ env.globals["strftime_now"] = today.strftime+ return env.from_string(chat_template).render(+ messages = messages,+ tools = tools,+ documents = None,+ add_generation_prompt = False,+ **_PROBE_SPECIAL_TOKENS,+ **(controls or {}),+ )+++def _is_whitespace_normalized_substring(value: str, container: str) -> bool:+ """Whether ``value`` remains intact after ignoring formatting whitespace."""+ return " ".join(value.split()) in " ".join(container.split())+++@lru_cache(maxsize = 16)+def template_system_turn(+ chat_template: str | None,+ today: date,+ tools: bool = False,+ controls: tuple = (),+) -> tuple[bool, str | None]:+ """How a system turn the chat did not send renders in this template on ``today``.++ Whether one renders at all, and the default system prompt it has to carry so the prompt reads as+ the template's own render: "" when there is none, None when no system turn reproduces it (the+ template rewrites what it is given). A template the probe cannot render takes one carrying nothing.+ """+ if not chat_template:+ return True, ""+ catalog = PROBE_TOOLS if tools else None+ kwargs = dict(controls)+ renders_chat = False+ # some templates read message text only from content parts, as a vision processor sends it.+ for content in (lambda text: text, lambda text: [{"type": "text", "text": text}]):+ user = {"role": "user", "content": content(_PROBE_USER)}++ def render(system: str | None) -> str:+ turns = [{"role": "system", "content": content(system)}] if system is not None else []+ return _render_probe(chat_template, [*turns, user], today, catalog, kwargs)++ try:+ bare = render(None)+ except Exception:+ continue+ renders_chat = True+ try:+ with_system = render(_PROBE_SYSTEM)+ except Exception:+ continue+ if with_system.count(_PROBE_SYSTEM) != 1:+ continue+ head, tail = with_system.split(_PROBE_SYSTEM)+ without_system = head + tail+ if _is_whitespace_normalized_substring(bare, without_system):+ return True, ""+ if len(bare) <= len(without_system) or not bare.startswith(head) or not bare.endswith(tail):+ # The explicit-system branch changes more than inserting the supplied text. Treat its+ # native default as non-replayable instead of silently replacing it with the date.+ return True, None+ default = bare[len(head) : len(bare) - len(tail)]+ try:+ replayed = render(default)+ except Exception:+ replayed = None+ # replayed as text, a control token in the default would no longer be one.+ carries_token = any(token in default for token in _PROBE_SPECIAL_TOKENS.values())+ return True, (default.strip() if replayed == bare and not carries_token else None)+ return not renders_chat, ("" if not renders_chat else None)def strip_current_date_update_note(text: str) -> str: