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: carry a restated instruction once, not eight times (#9368)

Daniel Hance568f725e80
diff --git a/studio/backend/core/inference/checkpoint.py b/studio/backend/core/inference/checkpoint.py
index f278ef19797..0d345d7dc2e 100644
--- a/studio/backend/core/inference/checkpoint.py
+++ b/studio/backend/core/inference/checkpoint.py
@@ -123,10 +123,13 @@ def carried_forward_items(
reversed for rendering, because reading order decides which of two conflicting
instructions the model treats as current. Instructions older than the budget are
silently dropped, which is why `max_items` is small and the header says "lossy".
+
+ Repeats collapse to their newest copy, on the same key `_recap` uses.
"""
if not evicted or max_tokens <= 0 or max_items <= 0:
return []
chosen: list[str] = []
+ seen: set[str] = set()
spent = 0
for group in reversed(group_turns(evicted)):
if len(chosen) >= max_items:
@@ -137,12 +140,19 @@ def carried_forward_items(
text = _text_of(head).strip()
if not text:
continue
+ item = _neutralise(text)
+ if item in seen:
+ # Users restate a standing rule, and each copy used to take a slot out of
+ # eight: one rule repeated eight times crowded out the user's other rule.
+ # Checked before the cost is charged, so a repeat cannot exhaust the budget.
+ continue
cost = estimate_message_tokens(head)
if spent + cost > max_tokens:
# Skipped, not truncated, and the loop continues: an older instruction that
# still fits beats nothing.
continue
- chosen.append(_neutralise(text))
+ chosen.append(item)
+ seen.add(item)
spent += cost
return list(reversed(chosen))
diff --git a/studio/backend/tests/test_checkpoint_compaction.py b/studio/backend/tests/test_checkpoint_compaction.py
index ad256623f64..0a3bb45d4ec 100644
--- a/studio/backend/tests/test_checkpoint_compaction.py
+++ b/studio/backend/tests/test_checkpoint_compaction.py
@@ -408,6 +408,34 @@ def test_at_most_max_items_instructions_are_carried():
assert "number 19" in items[-1]
+def test_a_restated_instruction_does_not_crowd_out_every_other_rule():
+ """Users restate a standing rule, and each copy used to take a slot and its tokens.
+
+ What that costs is the OTHER rules: with the repeats counted, the second instruction
+ here fell off the end of the list entirely. `_recap` already collapsed duplicates when
+ it merged a block on the second reset, so the two paths disagreed about one thread.
+ """
+ rule = (
+ "Standing instruction: always end every reply with STATUS::ZQXVARA123-ALPHA "
+ "and report any results as a markdown table."
+ )
+ other = (
+ "Second standing rule: cite the section number in every answer, spelled out in "
+ "words rather than digits, and never abbreviate it."
+ )
+ evicted = [{"role": "user", "content": other}, {"role": "assistant", "content": "ok"}]
+ for _ in range(8):
+ evicted += [
+ {"role": "user", "content": rule},
+ {"role": "assistant", "content": "ok"},
+ ]
+
+ items = carried_forward_items(evicted, max_tokens = 1024)
+
+ assert sum(1 for item in items if item.startswith("Standing instruction")) == 1
+ assert any(item.startswith("Second standing rule") for item in items)
+
+
def test_a_process_with_tools_disabled_never_resets(monkeypatch):
"""`supports_tools` is the TEMPLATE's capability, not "this request gets the tool".