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.

Keep what you typed with a document when a long chat is compacted (#12238)

Nilay99318b0be6f4
diff --git a/studio/backend/core/inference/checkpoint.py b/studio/backend/core/inference/checkpoint.py
index 868a3d18496..3b2579b4323 100644
--- a/studio/backend/core/inference/checkpoint.py
+++ b/studio/backend/core/inference/checkpoint.py
@@ -81,6 +81,16 @@ _NOT_SEARCHABLE = (
)
# only the delimiters themselves, so a user who writes about the feature is not mangled
_DELIMITERS = re.compile(r"</?carried_forward>", re.IGNORECASE)
+# How the composer starts each attachment it appends after the typed words.
+_ATTACHMENT = re.compile(
+ r"^(?:\[(?:PDF|DOCX|HTML|ODS|ODT|XLSX|PPTX|RTF): [^\n]*\]\n"
+ r"|<(attachment|pasted_text) name=[^\n]*>\n(?s:.*?)\n</\1>"
+ r"|\[[^\n]* is saved at \.unsloth_attachments/[0-9a-f]{12}/[^\n]* in the python tool's working directory[^\n]*\]$"
+ r"|\[[^\n]*: its text is below, so answer from it\. For calculations, the python tool has the file at "
+ r"path = \"\.unsloth_attachments/[0-9a-f]{12}/[^\n]*\]$"
+ r"|\[[^\n]*(?:: only the python tool can read this file| could not be uploaded, so it cannot be read)\]$)",
+ re.MULTILINE,
+)
def enabled() -> bool:
@@ -278,7 +288,9 @@ def _select_items(
head = group[0]
if not is_substantive(head, min_chars = min_chars):
return None
- text = _text_of(head).strip()
+ text = _text_of(head)
+ attachment = _ATTACHMENT.search(text)
+ text = (text[: attachment.start()] if attachment else text).strip()
if not text:
return None
# Judged AND priced on the BULLET, the only part of the turn that reaches the
diff --git a/studio/backend/tests/test_checkpoint_compaction.py b/studio/backend/tests/test_checkpoint_compaction.py
index 003c63ffa49..ff856745cac 100644
--- a/studio/backend/tests/test_checkpoint_compaction.py
+++ b/studio/backend/tests/test_checkpoint_compaction.py
@@ -4085,3 +4085,79 @@ def test_a_rescued_reset_still_offers_recall_but_is_never_replayed(monkeypatch,
assert inference_routes._thread_has_checkpoint("t1", branch) is admitted
assert llama_cpp._sticky_compaction_state("t1", branch) == (0, False)
+
+
+CONTRACT = "1. The seller delivers the goods within thirty days of the order. " * 900
+SANDBOX_NOTE = (
+ "[contract.pdf: its text is below, so answer from it. For calculations, the python tool has the file at "
+ 'path = ".unsloth_attachments/0123456789ab/contract.pdf"; fitz.open(path)]'
+)
+
+
+ "attachment",
+ [
+ "[PDF: contract.pdf]\n" + CONTRACT,
+ "[DOCX: contract.docx]\n" + CONTRACT,
+ "[XLSX: prices.xlsx]\n[Sheet: Q3]\n" + CONTRACT,
+ "<attachment name=contract.txt>\n" + CONTRACT + "\n</attachment>",
+ "<pasted_text name=contract.txt bytes=60300>\n" + CONTRACT + "\n</pasted_text>",
+ SANDBOX_NOTE + "\n[PDF: contract.pdf]\n" + CONTRACT,
+ "[PDF: a.pdf]\n" + CONTRACT + "\n<attachment name=b.txt>\n" + CONTRACT + "\n</attachment>",
+ ],
+)
+def test_an_instruction_typed_with_a_document_is_carried_without_it(attachment):
+ turn = {"role": "user", "content": INSTRUCTION + "\n" + attachment}
+
+ assert carried_forward_items([turn], max_tokens = 1024) == [INSTRUCTION]
+
+
+def test_a_small_document_is_not_quoted_into_the_block():
+ turn = {
+ "role": "user",
+ "content": INSTRUCTION + "\n[PDF: memo.pdf]\nThe buyer pays for shipping.",
+ }
+
+ assert carried_forward_items([turn], max_tokens = 1024) == [INSTRUCTION]
+
+
+def test_a_document_sent_without_typed_words_carries_nothing():
+ turn = {"role": "user", "content": "[PDF: memo.pdf]\nThe buyer pays for shipping."}
+
+ assert carried_forward_items([turn], max_tokens = 1024) == []
+
+
+ "typed",
+ [
+ "Answer in this shape:\n[Summary: one line]\nthen the details, always in Spanish.",
+ "Review [PDF: contract.pdf] as the buyer's lawyer and answer in Spanish.",
+ "Treat <attachment name=x> as a literal tag in every answer from now on.",
+ ],
+)
+def test_bracketed_text_the_user_typed_is_carried_whole(typed):
+ assert carried_forward_items([{"role": "user", "content": typed}], max_tokens = 1024) == [typed]
+
+
+def test_a_thread_opened_with_a_document_still_names_its_task_after_a_reset():
+ messages = [
+ {"role": "system", "content": "you are helpful"},
+ {"role": "user", "content": INSTRUCTION + "\n[PDF: contract.pdf]\n" + CONTRACT},
+ {"role": "assistant", "content": "Understood."},
+ ]
+ for question in (
+ "What is the delivery deadline?",
+ "Who pays for shipping?",
+ "Can the buyer terminate early?",
+ ):
+ messages += [
+ {"role": "user", "content": question},
+ {"role": "assistant", "content": "It is in clause 1."},
+ ]
+ messages += [{"role": "user", "content": "And what about returns?"}]
+
+ fitted, truncation = _fit(messages, context_length = 16384, max_tokens = 2048)
+
+ assert truncation["checkpoint_started"] is True
+ assert INSTRUCTION in fitted[0]["content"]
+ assert "thirty days" not in fitted[0]["content"]