Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Studio: keep a local model's built-in system prompt when the date setting is on (#12382)

Nilay101b2d66b732
diff --git a/studio/backend/core/inference/checkpoint.py b/studio/backend/core/inference/checkpoint.py
index 3b2579b4323..5f6f5a66fab 100644
--- a/studio/backend/core/inference/checkpoint.py
+++ b/studio/backend/core/inference/checkpoint.py
@@ -39,6 +39,7 @@ from core.inference.context_window import (
truncate_oldest_messages,
)
from core.inference.instruction_pin import is_substantive
+from utils.current_date_prompt_settings import strip_current_date_update_note
# "checkpoint" resets the epoch; "rolling" is the pre-existing window, byte for byte, and is both the A/B arm and the
# escape hatch for a template family that misbehaves.
@@ -288,7 +289,7 @@ def _select_items(
head = group[0]
if not is_substantive(head, min_chars = min_chars):
return None
- text = _text_of(head)
+ text = strip_current_date_update_note(_text_of(head))
attachment = _ATTACHMENT.search(text)
text = (text[: attachment.start()] if attachment else text).strip()
if not text:
diff --git a/studio/backend/core/inference/context_window.py b/studio/backend/core/inference/context_window.py
index c7b77a93cc1..cf68ba67add 100644
--- a/studio/backend/core/inference/context_window.py
+++ b/studio/backend/core/inference/context_window.py
@@ -11,6 +11,12 @@ import re
from collections.abc import Callable
from 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")
@@ -135,6 +141,95 @@ 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/instruction_pin.py b/studio/backend/core/inference/instruction_pin.py
index a9a60f17660..7d97599dd89 100644
--- a/studio/backend/core/inference/instruction_pin.py
+++ b/studio/backend/core/inference/instruction_pin.py
@@ -36,6 +36,7 @@ import os
import re
from core.inference.context_window import estimate_messages_tokens_dense, group_turns
+from utils.current_date_prompt_settings import strip_current_date_update_note
# 80 characters: someone who typed a paragraph wrote an instruction. Nothing inspects meaning or keywords, which are the
# heuristics a user trips by accident.
@@ -103,15 +104,16 @@ what when where which while who whom whose why will with would you your yours
def _text_of(message: dict) -> str:
+ # without Studio's date note: it pads a short first turn past the bar and is no instruction.
content = message.get("content")
if isinstance(content, str):
- return content
+ return strip_current_date_update_note(content)
if isinstance(content, list):
parts = []
for part in content:
if isinstance(part, dict) and isinstance(part.get("text"), str):
parts.append(part["text"])
- return "\n".join(parts)
+ return strip_current_date_update_note("\n".join(parts))
return ""
diff --git a/studio/backend/core/inference/llama_cpp.py b/studio/backend/core/inference/llama_cpp.py
index 458146b5c02..84326ad27b2 100644
--- a/studio/backend/core/inference/llama_cpp.py
+++ b/studio/backend/core/inference/llama_cpp.py
@@ -396,6 +396,8 @@ def _fit_with_instruction_pins(
)
except Exception: # noqa: BLE001 -- a protection heuristic must never break a chat
pins = set()
+ from core.inference.context_window import keep_date_note
+
fitted, truncation = _fit_context(
messages, protected_message_ids = (anchors | pins) or None, **kwargs
)
@@ -409,7 +411,19 @@ def _fit_with_instruction_pins(
and not truncation.get("fits")
and not int(truncation.get("dropped_messages") or 0)
):
- return _fit_context(messages, protected_message_ids = anchors or None, **kwargs)
+ 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 fitted, truncation
diff --git a/studio/backend/routes/inference.py b/studio/backend/routes/inference.py
index a952d15a16e..038aafb3694 100644
--- a/studio/backend/routes/inference.py
+++ b/studio/backend/routes/inference.py
@@ -3913,6 +3913,7 @@ import zlib
from utils.current_date_prompt_settings import (
CURRENT_DATE_PROMPT_LINE_RE,
CURRENT_DATE_PROMPT_PREFIX,
+ CURRENT_DATE_UPDATE_NOTE_RE,
CURRENT_DATE_UPDATE_PREFIX,
contains_current_date_prompt_line,
conversation_start_date,
@@ -6361,15 +6362,22 @@ def _append_current_date_note(
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
- note = _current_date_parts(request, thread_id)[1]
+ 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) - 1, -1, -1):
+ 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
@@ -6377,7 +6385,7 @@ def _append_current_date_note(
if _is_folded_tool_result(content):
continue
if isinstance(content, str):
- if content.startswith(note):
+ 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())
@@ -6395,7 +6403,10 @@ def _append_current_date_note(
None,
)
has_text = first is not None
- if has_text and content[first]["text"].startswith(note):
+ 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:
@@ -6407,6 +6418,8 @@ def _append_current_date_note(
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}
@@ -6426,6 +6439,9 @@ def _apply_current_date_prompt(
Kept ahead of the user's own text so a system prompt that ends in an instruction still reads
as 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_prompt
if _date_gate_blocks(request, include_api_key):
return system_prompt
date_line = _current_date_parts(request, thread_id)[0]
@@ -26978,7 +26994,10 @@ async def produce_openai_chat_completions(
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)
+ chat_messages,
+ request,
+ thread_id = getattr(payload, "thread_id", None),
+ system_prompt = system_prompt,
)
except _DecodedAudioTooLongError as e:
# A limit the caller can act on, not a server fault.
@@ -27444,7 +27463,10 @@ async def produce_openai_chat_completions(
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)
+ chat_messages,
+ request,
+ thread_id = getattr(payload, "thread_id", None),
+ system_prompt = system_prompt,
)
if not chat_messages:
@@ -27516,7 +27538,10 @@ async def produce_openai_chat_completions(
)
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)
+ gguf_messages,
+ request,
+ thread_id = getattr(payload, "thread_id", None),
+ system_prompt = system_prompt,
)
image_b64 = None
for audio_b64, audio_format in prepared_audio:
@@ -29309,7 +29334,10 @@ async def produce_openai_chat_completions(
):
chat_messages, served_images = _msgs, _payloads
chat_messages = _append_current_date_note(
- chat_messages, request, thread_id = getattr(payload, "thread_id", None)
+ chat_messages,
+ request,
+ thread_id = getattr(payload, "thread_id", None),
+ system_prompt = system_prompt,
)
# Decode image (from content parts OR legacy field)
@@ -30332,6 +30360,7 @@ async def produce_openai_chat_completions(
),
request,
thread_id = getattr(payload, "thread_id", None),
+ system_prompt = system_prompt,
)
else:
#
@@ -30359,6 +30388,7 @@ async def produce_openai_chat_completions(
_set_or_prepend_system_message(_sf_rebuilt, system_prompt),
request,
thread_id = getattr(payload, "thread_id", None),
+ system_prompt = 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
@@ -36194,7 +36224,10 @@ async def _mlx_count_chat_tokens(payload, request = None) -> Optional[JSONRespon
system_prompt, request, thread_id = getattr(payload, "thread_id", None)
)
messages = _append_current_date_note(
- messages, request, thread_id = getattr(payload, "thread_id", None)
+ messages,
+ request,
+ thread_id = getattr(payload, "thread_id", None),
+ system_prompt = system_prompt,
)
from state.tool_policy import get_tool_policy as _get_tool_policy_mlx
@@ -36309,6 +36342,7 @@ async def _mlx_count_chat_tokens(payload, request = None) -> Optional[JSONRespon
_set_or_prepend_system_message(messages, system_prompt),
request,
thread_id = getattr(payload, "thread_id", None),
+ system_prompt = system_prompt,
)
system_prompt = ""
elif _tools_to_use:
@@ -36577,7 +36611,10 @@ async def chat_count_tokens(
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)
+ 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
@@ -36640,11 +36677,14 @@ async def chat_count_tokens(
tools_to_use = tools_to_use + _mcp_tools
if tools_to_use:
openai_tools = tools_to_use
- openai_messages = _prepend_current_date_to_messages(
+ openai_messages = _set_or_prepend_system_message(
openai_messages,
- request,
- include_api_key = True,
- thread_id = getattr(payload, "thread_id", None),
+ _apply_current_date_prompt(
+ _system_prompt,
+ request,
+ include_api_key = True,
+ thread_id = getattr(payload, "thread_id", None),
+ ),
)
_count_nudge = await _apply_rag_nudge(
_build_tool_action_nudge(
diff --git a/studio/backend/tests/test_context_overflow_truncation.py b/studio/backend/tests/test_context_overflow_truncation.py
index 658a0671b67..ee51b9724d3 100644
--- a/studio/backend/tests/test_context_overflow_truncation.py
+++ b/studio/backend/tests/test_context_overflow_truncation.py
@@ -1674,3 +1674,80 @@ def test_a_pin_is_not_charged_for_a_tool_exchange_it_does_not_hold():
{"role": "user", "content": "continue"},
]
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_cpp
+
+ kept = {"role": "user", "content": "C" * 200}
+ messages = [
+ {"role": "user", "content": "[Current date: 2026-10-01]\n\n" + "A" * 400},
+ {"role": "assistant", "content": "B" * 400},
+ kept,
+ {"role": "assistant", "content": "D" * 100},
+ {"role": "user", "content": "what is the date?"},
+ ]
+ # the note is priced far above its length, so moving it would leave the fit over target.
+ counter = lambda candidate: sum( # noqa: E731
+ len(str(message.get("content", "")))
+ + 400 * str(message.get("content", "")).count("[Current date:")
+ 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 and kept["content"] == "C" * 200
+ assert info["prompt_tokens_after"] == counter(fitted)
+
+
+def test_the_date_note_neither_makes_nor_quotes_a_standing_instruction():
+ from core.inference import checkpoint, instruction_pin
+
+ short = "Explain how photosynthesis works in C4 plants, in brief."
+ noted = {"role": "user", "content": f"[Current date: 2026-10-01]\n\n{short}"}
+ assert not instruction_pin.is_substantive(noted)
+ assert (
+ instruction_pin.last_substantive_instruction([noted, {"role": "user", "content": "ok"}])
+ is None
+ )
+
+ long = "Always answer in French and keep every reply under three short sentences, please."
+ evicted = [
+ {"role": "user", "content": f"[Current date: 2026-10-01]\n\n{long}"},
+ {"role": "assistant", "content": "D'accord."},
+ ]
+ items = checkpoint._select_items(
+ evicted, max_tokens = 4096, max_items = 8, min_chars = instruction_pin.INSTRUCTION_MIN_CHARS
+ )
+ assert items == [long]
diff --git a/studio/backend/tests/test_current_date_prompt_settings.py b/studio/backend/tests/test_current_date_prompt_settings.py
index 598137dbf63..91fb760d667 100644
--- a/studio/backend/tests/test_current_date_prompt_settings.py
+++ b/studio/backend/tests/test_current_date_prompt_settings.py
@@ -631,6 +631,23 @@ class TestDateChangeNote:
{"role": "user", "content": "hi"},
]
+ def test_without_a_system_prompt_the_first_turn_keeps_the_start_date(self):
+ self.today = "2026-08-16"
+ history = [
+ {"role": "user", "content": "first"},
+ {"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(
diff --git a/studio/backend/tests/test_mlx_inference_backend.py b/studio/backend/tests/test_mlx_inference_backend.py
index bca2559b8ff..f0bc752ae93 100644
--- a/studio/backend/tests/test_mlx_inference_backend.py
+++ b/studio/backend/tests/test_mlx_inference_backend.py
@@ -5393,9 +5393,10 @@ def test_an_mlx_count_prices_the_current_date_the_completion_prepends(monkeypatc
line = current_date_prompt_line(request = interactive)
assert line, "the harness must actually produce a date line"
- assert backend.system.startswith(
- line
- ), f"the count dropped the date line the completion prepends: {backend.system!r}"
+ 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}"
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.py
index 116de71a9ac..e3eb8ea946d 100644
--- a/studio/backend/tests/test_openai_auto_switch.py
+++ b/studio/backend/tests/test_openai_auto_switch.py
@@ -5193,10 +5193,7 @@ def test_chat_count_tokens_prices_the_current_date(monkeypatch):
thread = [{"role": "user", "content": "hi"}]
_counted_body(_count_request(thread))
- assert counted["messages"][0] == {
- "role": "system",
- "content": "The current date is 2026-08-15.",
- }
+ assert counted["messages"] == [{"role": "user", "content": "[Current date: 2026-08-15]\n\nhi"}]
# 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.py
index 23330de3729..d5a0259e53d 100644
--- a/studio/backend/tests/test_openai_tool_passthrough.py
+++ b/studio/backend/tests/test_openai_tool_passthrough.py
@@ -4859,9 +4859,11 @@ class TestGgufVisionToolRouting:
yield {"type": "content", "text": "done"}
class ApiRequest(self._Request):
- headers = {"authorization": "Bearer sk-unsloth-test"}
state = SimpleNamespace(skip_api_monitor = True)
+ def __init__(self):
+ self.headers = {"authorization": "Bearer sk-unsloth-test"}
+
monkeypatch.setattr(
inf_mod,
"current_date_prompt_line",
@@ -6455,7 +6457,12 @@ class TestGgufVisionToolRouting:
assert monitor.active_count() == 0
assert get_llama_admission_queue("http://llama.disconnect.test").snapshot().active == 0
- def _drive_standard_gguf(self, monkeypatch, date_line: str) -> list[dict]:
+ def _drive_standard_gguf(
+ self,
+ monkeypatch,
+ date_line: str,
+ messages = None,
+ ) -> list[dict]:
"""Run one non-tool GGUF completion with the current-date setting pinned."""
import routes.inference as inf_mod
@@ -6484,7 +6491,8 @@ class TestGgufVisionToolRouting:
payload = ChatCompletionRequest(
model = "default",
- messages = [
+ messages = messages
+ or [
{"role": "system", "content": "original system"},
{"role": "developer", "content": "developer rules"},
{"role": "user", "content": "hi"},
@@ -6512,6 +6520,22 @@ class TestGgufVisionToolRouting:
{"role": "user", "content": "hi"},
]
+ def test_standard_gguf_without_a_system_prompt_keeps_the_template_default(self, monkeypatch):
+ 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"},
+ ],
+ )
+ assert sent == [
+ {"role": "user", "content": "[Current date: 2026-08-15]\n\nfirst"},
+ {"role": "assistant", "content": "ok"},
+ {"role": "user", "content": "second"},
+ ]
+
@pytest.mark.parametrize(
("seed", "expected"),
[