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 chat: skip the transcript read when no message recorded a context fit (#16995)

Daniel Han29d4733fad93
diff --git a/studio/backend/core/inference/llama_cpp.py b/studio/backend/core/inference/llama_cpp.py
index d9bbbeca6a0..a4719025f49 100644
--- a/studio/backend/core/inference/llama_cpp.py
+++ b/studio/backend/core/inference/llama_cpp.py
@@ -2039,6 +2039,10 @@ def _sticky_compaction_details(
try:
from storage import studio_db
+ # Boundaries live only in a stored row's contextTruncation; most threads have none.
+ has_truncation = getattr(studio_db, "chat_thread_has_context_truncation", None)
+ if has_truncation is not None and not has_truncation(thread_id):
+ return 0, False, False
stored = list(studio_db.list_chat_messages(thread_id) or [])
states = _compaction_branch_states(stored, branch_messages)
if not states:
diff --git a/studio/backend/routes/inference.py b/studio/backend/routes/inference.py
index 5577ab958a3..4f0943321bd 100644
--- a/studio/backend/routes/inference.py
+++ b/studio/backend/routes/inference.py
@@ -6101,6 +6101,9 @@ def _thread_has_checkpoint(thread_id, branch_messages = None) -> bool:
from core.inference.llama_cpp import _compaction_branch_states
from storage import studio_db
+ has_truncation = getattr(studio_db, "chat_thread_has_context_truncation", None)
+ if has_truncation is not None and not has_truncation(str(thread_id)):
+ return False
# As dicts: the completions path sends `ChatMessage`, the resolver uses `.get`.
states = _compaction_branch_states(
list(studio_db.list_chat_messages(str(thread_id)) or []),
diff --git a/studio/backend/storage/studio_db.py b/studio/backend/storage/studio_db.py
index 591302bf7b1..fc64baaeee9 100644
--- a/studio/backend/storage/studio_db.py
+++ b/studio/backend/storage/studio_db.py
@@ -5036,6 +5036,20 @@ def fork_counts_for_thread(thread_id: str) -> dict[str, int]:
conn.close()
+def chat_thread_has_context_truncation(thread_id: str) -> bool:
+ """Whether any message in the thread records a context fit (metadata `contextTruncation`), without loading the transcript."""
+ conn = get_connection()
+ try:
+ row = conn.execute(
+ "SELECT 1 FROM chat_messages WHERE thread_id = ? "
+ "AND instr(metadata_json, 'contextTruncation') > 0 LIMIT 1",
+ (thread_id,),
+ ).fetchone()
+ return row is not None
+ finally:
+ conn.close()
+
+
def list_chat_messages(thread_id: str) -> list[dict]:
conn = get_connection()
try:
diff --git a/studio/backend/tests/test_compaction_boundary_gate.py b/studio/backend/tests/test_compaction_boundary_gate.py
new file mode 100644
index 00000000000..60e12add3c3
--- /dev/null
+++ b/studio/backend/tests/test_compaction_boundary_gate.py
@@ -0,0 +1,82 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""A thread whose rows never recorded a context fit answers "no boundary" without loading its transcript."""
+
+import pytest
+
+from core.inference import llama_cpp
+from routes import inference as inference_routes
+from storage import studio_db
+
+
+def _seed(thread_id, metadata):
+ studio_db.upsert_chat_thread(
+ {"id": thread_id, "title": "Chat", "modelType": "base", "modelId": "local", "createdAt": 1}
+ )
+ studio_db.upsert_chat_message(
+ {
+ "id": f"{thread_id}-u",
+ "threadId": thread_id,
+ "role": "user",
+ "content": [{"type": "text", "text": "question"}],
+ "createdAt": 2,
+ }
+ )
+ studio_db.upsert_chat_message(
+ {
+ "id": f"{thread_id}-a",
+ "threadId": thread_id,
+ "parentId": f"{thread_id}-u",
+ "role": "assistant",
+ "content": [{"type": "text", "text": "answer"}],
+ "metadata": metadata,
+ "createdAt": 3,
+ }
+ )
+
+
+BRANCH = [
+ {"role": "user", "content": "question"},
+ {"role": "assistant", "content": "answer"},
+ {"role": "user", "content": "next"},
+]
+
+
+def counted_reads(monkeypatch):
+ calls = []
+ real = studio_db.list_chat_messages
+
+ def _counting(thread_id):
+ calls.append(thread_id)
+ return real(thread_id)
+
+ monkeypatch.setattr(studio_db, "list_chat_messages", _counting)
+ return calls
+
+
+def test_gate_reads_only_threads_with_a_recorded_fit():
+ _seed("plain", {"custom": {"generationStatus": "completed"}})
+ _seed("top", {"contextTruncation": {"fits": True, "boundary_messages": 2}})
+ _seed("custom", {"custom": {"contextTruncation": {"fits": True, "boundary_messages": 2}}})
+ assert studio_db.chat_thread_has_context_truncation("plain") is False
+ assert studio_db.chat_thread_has_context_truncation("top") is True
+ assert studio_db.chat_thread_has_context_truncation("custom") is True
+ assert studio_db.chat_thread_has_context_truncation("missing") is False
+
+
+def test_no_recorded_fit_skips_the_transcript_read(counted_reads):
+ _seed("plain", {"custom": {"generationStatus": "completed"}, "note": "contextTruncatio n"})
+ assert llama_cpp._sticky_compaction_details("plain", BRANCH) == (0, False, False)
+ assert inference_routes._thread_has_checkpoint("plain", BRANCH) is False
+ assert counted_reads == []
+
+
+def test_recorded_fit_still_reads_the_transcript(counted_reads):
+ _seed(
+ "top", {"contextTruncation": {"fits": True, "boundary_messages": 2, "dropped_messages": 2}}
+ )
+ llama_cpp._sticky_compaction_details("top", BRANCH)
+ inference_routes._thread_has_checkpoint("top", BRANCH)
+ assert counted_reads == ["top", "top"]
diff --git a/studio/backend/tests/test_openai_auto_switch.py b/studio/backend/tests/test_openai_auto_switch.py
index 5a3da4cbbd2..06d4f2128ba 100644
--- a/studio/backend/tests/test_openai_auto_switch.py
+++ b/studio/backend/tests/test_openai_auto_switch.py
@@ -5110,6 +5110,7 @@ def test_chat_count_tokens_replays_saved_compaction_without_archive_work(monkeyp
}
}
monkeypatch.setattr(studio_db, "list_chat_messages", lambda _thread: stored)
+ monkeypatch.setattr(studio_db, "chat_thread_has_context_truncation", lambda _thread: True)
payload = _count_request(
messages,
thread_id = "compacted-thread",
@@ -5204,6 +5205,7 @@ def test_chat_count_tokens_does_not_replay_studio_compaction_on_passthrough(monk
backend.count_chat_tokens = count
monkeypatch.setattr(_tp, "get_tool_policy", lambda: None)
monkeypatch.setattr(studio_db, "list_chat_messages", lambda _thread: stored)
+ monkeypatch.setattr(studio_db, "chat_thread_has_context_truncation", lambda _thread: True)
payload = _count_request(
messages,
thread_id = "compacted-thread",