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: pay for the tool result framing probe once per model, not once per result (#9541)
Daniel Han45b13db63b4c
diff --git a/studio/backend/core/inference/tools.py b/studio/backend/core/inference/tools.pyindex 444c5d7babb..5040e57398b 100644--- a/studio/backend/core/inference/tools.py+++ b/studio/backend/core/inference/tools.py@@ -11835,6 +11835,104 @@ _PROBE_ROLE = "user"# hundreds, or at infinity as the chunk grows, because its count does not move at all._MAX_PROBE_CHARS_PER_TOKEN = 256+# A measured count is a pure function of (model, chat template, window, chunk), and each+# `count_chat_tokens` is two llama-server calls (/apply-template then /tokenize) over a+# fresh connection. So the framing baseline -- one number for EVERY result the process+# truncates -- is worth remembering, and so is any prefix already priced.+#+# Keyed on the resident llama-server process, because the count depends on the EFFECTIVE+# chat template and the managed fields cannot reconstruct it: user pass-through args are+# appended verbatim after Studio's own flags (`llama_cpp.py`, "User pass-through args go+# last") and llama.cpp is last-wins, so `--chat-template` in extra args renders through a+# template `_chat_template_override` never sees. Reload the same GGUF into the same window+# with only those args changed and every managed field matches while the rendering does+# not, which would price a prefix by a template no longer serving it. `is_loaded` is+# `self._process is not None and self._healthy` and args reach llama-server only on its+# command line, so any change to them is a new process by construction -- which settles it+# without enumerating the flags that matter. The content fields ride along so a recycled+# pid still has to agree on everything before a count is reused.+_PROBE_COUNT_CACHE: dict = {}++# Tool calls run in worker threads (`tool_stream_exec.stream_tool_execution` runs each+# invocation in one), so concurrent chats reach this cache at the same time. A bare dict+# assignment is atomic under the GIL, but the LRU touch and the eviction below are+# read-then-mutate sequences and are not: measured with 24 threads over a 3-entry cache,+# `cache.pop(chunk)` raised KeyError after another thread evicted the same key, `del+# cache[victim]` raised on a victim already taken, and choosing a victim raised+# "dictionary changed size during iteration" -- 90 exceptions in one run, none of them+# caught on the way out of `_truncate`.+#+# Held only across the dict work, never across a `count_chat_tokens` call. Serialising the+# round trips themselves would trade a shared cache for a shared queue, which is the+# opposite of the point. Two threads may therefore measure the same chunk at once and both+# store it; the value is the same either way, so that costs one duplicate measurement,+# which is what the merge base did on every result anyway.+_PROBE_COUNT_LOCK = threading.Lock()++# The empty chunk: the framing baseline, and the entry eviction pins. Named so the two+# places that treat it specially cannot drift apart from a bare "".+_PROBE_BASELINE = ""++# One model's counts at a time (a new identity clears the map). Ten times the worst case+# for one result: `_EXACT_FIT_PASSES` prefixes plus the baseline.+_PROBE_COUNT_CACHE_ENTRIES = 64++# And a bound on what is HELD, since the entry count alone does not give one. Only a+# fetched page is capped at `_MAX_PAGE_CHARS`; a tool result's prefix is bounded by+# `min(UNSLOTH_TOOL_RESULT_MAX_CHARS, ctx * 4 * _PAGE_CONTEXT_SHARE)` and `_env_int`+# accepts any positive integer, so a large configured cap on a large window makes one+# prefix enormous. Measured: a cap of 1,000,000 on a 262k window cached 733,971 characters+# from a SINGLE result, which 64 entries would then multiply. This also drops an oversized+# prefix rather than storing it. The baseline is 0 characters, so the entry that earns the+# most is never the one squeezed out.+_PROBE_COUNT_CACHE_CHARS = 1_000_000+++def _probe_identity(llama, ctx: int):+ """A key that changes whenever a measured count could, or None to disable the cache.++ None is the safe answer: it costs round trips, it never returns a stale number.+ """+ try:+ # The resident llama-server. No process is no key: a backend this module cannot+ # tie a count to keeps paying for its round trips, which is the safe direction.+ pid = getattr(getattr(llama, "_process", None), "pid", None)+ if not isinstance(pid, int):+ return None+ key = (+ pid,+ ctx,+ getattr(llama, "model_identifier", None),+ getattr(llama, "_gguf_load_identity", None),+ getattr(llama, "_chat_template_override", None),+ # The gap the process id closes, spelled out: whatever the user appended to+ # the command line, including a template that overrides the managed one.+ tuple(getattr(llama, "_extra_args", None) or ()),+ )+ hash(key) # an unhashable field is also "do not cache", not a TypeError upstream+ return key+ except Exception: # noqa: BLE001 -- an unreadable identity is "do not cache"+ return None+++def _probe_cache(llama, ctx: int) -> dict:+ """The count cache for the model serving this request.++ A fresh per-call dict when the model has no identity, so the caller's code path is the+ same either way and an unidentifiable backend simply gets no reuse between calls.+ """+ identity = _probe_identity(llama, ctx)+ if identity is None:+ return {}+ with _PROBE_COUNT_LOCK:+ cache = _PROBE_COUNT_CACHE.get(identity)+ if cache is None:+ # A different model is serving now. Drop the previous one's numbers rather than+ # keep them around to be matched against.+ _PROBE_COUNT_CACHE.clear()+ cache = _PROBE_COUNT_CACHE[identity] = {}+ return cache+def _loaded_token_counter(ctx: int):"""The tokenizer of the model serving this request, or None when there is not one.@@ -11859,27 +11957,116 @@ def _loaded_token_counter(ctx: int):except Exception: # noqa: BLE001 -- no tokenizer is "unknown", never an errorreturn None- def _rendered(chunk: str):- try:- spent = counter([{"role": _PROBE_ROLE, "content": chunk}], None, None, strict = False)- except Exception: # noqa: BLE001 -- same rule: fall back to the estimate- logger.debug("result budget: exact count failed", exc_info = True)- return None- return int(spent) if isinstance(spent, (int, float)) and spent > 0 else None+ cache = _probe_cache(llama, ctx)+ # Whether anything said here will outlive this call, and whether `/apply-template` has+ # already refused once. Both only gate the strict attempt, never a returned value.+ retained = bool(_probe_identity(llama, ctx))+ template_down: list[bool] = []++ def _remember(chunk: str, value: int) -> None:+ """Hold `value` for `chunk`, evicting least-recently-used entries to stay in bounds.++ Refusing new entries once full was worse than not caching at all. Most tool results+ are one-offs, so the first `_PROBE_COUNT_CACHE_ENTRIES` distinct prefixes froze the+ cache on text that would never be asked about again -- and because the baseline is+ only priced when a count comes in OVER budget, a process that handled 64 results+ that FIT first locked it out for good. Measured: after 64 English results, every+ later dense result paid 4 counter calls (8 HTTP) again, exactly the merge base's+ cost, for the life of the process.++ So evict, and pin the baseline: it is 0 characters, it is the same number for every+ result this process truncates, and it is the one entry a bounded cache most needs.+ """+ if len(chunk) > _PROBE_COUNT_CACHE_CHARS:+ return # too large to hold at all; evicting the rest would not help+ with _PROBE_COUNT_LOCK:+ held = sum(map(len, cache))+ while len(cache) >= _PROBE_COUNT_CACHE_ENTRIES or (+ held + len(chunk) > _PROBE_COUNT_CACHE_CHARS+ ):+ # `list()` so the scan cannot trip over another thread's insert, and+ # `pop(..., None)` so a victim someone else already took is not an error.+ victim = next((key for key in list(cache) if key != _PROBE_BASELINE), None)+ if victim is None:+ return # only the pinned baseline is left, and it stays+ held -= len(victim)+ cache.pop(victim, None)+ cache[chunk] = value- # What the turn costs with nothing in it, priced once. Two jobs: it is the baseline the- # guard measures growth against, so a template that renders no content is caught by its- # count not moving rather than by a guess about density; and it is the only part of the- # count that is not the chunk. Left IN the total rather than subtracted -- 8 tokens on- # Qwen3 and 11 on the Gemma-4 templates, under 1% of a 1,792-token share, and the real- # tool turn pays its own framing anyway, so counting it errs toward a smaller result.- framing = _rendered("") or 0+ def _rendered(chunk: str):+ with _PROBE_COUNT_LOCK:+ hit = cache.get(chunk)+ if hit is not None and chunk != _PROBE_BASELINE:+ # Most recently used moves to the back. `pop(..., None)` and the re-check+ # keep this a no-op rather than a KeyError if it lost a race to an evictor.+ if cache.pop(chunk, None) is not None:+ cache[chunk] = hit+ if hit is not None:+ return hit+ # Strict, so that a count is only retained when the chat template really rendered+ # it. With `strict = False` a failed `/apply-template` still returns the plain-text+ # fallback, which drops role markers and special tokens -- fine as a one-off answer,+ # but it prices a prompt the model will never be sent, and caching it would let one+ # bad moment quietly under-count that prefix for the life of the process.+ #+ # Asked at most once per counter, and not at all when nothing would be retained+ # anyway. Strictness exists only to decide whether a count may be KEPT, so paying+ # for it twice would spend round trips to answer a question already settled: a+ # template that would not render is not going to start, and this whole change is+ # about not making calls that cannot change an answer. So a template outage costs+ # one extra attempt for the first probe of a result rather than one for every probe.+ message = [{"role": _PROBE_ROLE, "content": chunk}]+ rendered = False+ if retained and not template_down:+ try:+ spent = counter(message, None, None, strict = True)+ rendered = True+ except Exception: # noqa: BLE001 -- not fatal: the fallback still prices bytes+ template_down.append(True)+ if not rendered:+ try:+ spent = counter(message, None, None, strict = False)+ except Exception: # noqa: BLE001 -- now it is: fall back to the estimate+ logger.debug("result budget: exact count failed", exc_info = True)+ return None+ value = int(spent) if isinstance(spent, (int, float)) and spent > 0 else None+ # The fallback's count is USED, exactly as before -- it still tokenizes the real+ # bytes, which is what catches dense ASCII, and the estimate it would otherwise fall+ # back to undercharges base64 several fold. It is simply not retained: like a+ # failure, it is a property of the moment rather than of the text.+ if value is not None and rendered:+ _remember(chunk, value)+ return value- def _count(chunk: str):+ # What the turn costs with nothing in it: the baseline the guard measures growth+ # against, so a template that renders no content is caught by its count not moving+ # rather than by a guess about density. Left IN the total rather than subtracted -- 8+ # tokens on Qwen3 and 11 on the Gemma-4 templates, under 1% of a 1,792-token share, and+ # the real tool turn pays its own framing anyway, so counting it errs toward a smaller+ # result. Priced on demand: see `_count`.+ baseline: list[int] = []++ def _framing() -> int:+ if not baseline:+ baseline.append(_rendered(_PROBE_BASELINE) or 0)+ return baseline[0]++ def _count(chunk: str, token_budget: float = 0.0):+ """Tokens for `chunk`, or None when the count did not measure it.++ `token_budget` is an optimisation and nothing more. A count within budget and a+ count the guard rejects both make the caller return its own estimate unchanged, so+ when `spent` fits, the baseline that separates those two paths cannot change the+ answer and is not priced -- which is why an English result costs one round trip+ rather than two. The default of 0 means "no budget", so the guard always runs.+ """spent = _rendered(chunk)if spent is None:return None+ if spent <= token_budget:+ return spent# A count that barely moves off the framing measured nothing, whatever it reports.+ framing = _framing()if spent - framing < len(chunk) / _MAX_PROBE_CHARS_PER_TOKEN:logger.debug("result budget: template priced %d chars at %d tokens over %d of framing; "@@ -11915,6 +12102,15 @@ def _exact_prefix_chars(text: str, chars: int, token_budget: float, ctx: int) ->costs and shrinks every page that was already fine. So when a tokenizer is serving therequest, ask it; when none is, keep the estimate exactly as it was."""+ # An estimate already at or below the readable floor cannot be improved on, so nothing+ # measured here could change the caller's answer. Every value below is at most `chars`+ # or is exactly `_MIN_PAGE_CHARS`, and `_dense_char_limit` clamps with+ # `max(_MIN_PAGE_CHARS, ...)`, so with `chars <= _MIN_PAGE_CHARS` the caller lands on+ # the floor whichever branch is taken. Checked before the counter is even looked up:+ # this is the small-window case, and it used to spend a full set of round trips+ # rediscovering a number the caller already had.+ if chars <= _MIN_PAGE_CHARS:+ return charscounter = _loaded_token_counter(ctx)if counter is None:return chars@@ -11929,7 +12125,9 @@ def _exact_prefix_chars(text: str, chars: int, token_budget: float, ctx: int) -># to prevent.previous = None # the last (chars, tokens) pair, for the secant step belowfor _ in range(_EXACT_FIT_PASSES):- spent = counter(text[:chars])+ # The budget goes with the chunk so a count that already fits can skip pricing the+ # framing baseline it would only be compared against. See `_count`.+ spent = counter(text[:chars], token_budget)if spent is None:return chars # nothing to measure with; the estimate stands, as beforeif spent <= token_budget:diff --git a/studio/backend/tests/test_web_page_cap_fits_window.py b/studio/backend/tests/test_web_page_cap_fits_window.pyindex 9f715d30d34..10bd009009b 100644--- a/studio/backend/tests/test_web_page_cap_fits_window.py+++ b/studio/backend/tests/test_web_page_cap_fits_window.py@@ -20,6 +20,8 @@ web_search calls):from __future__ import annotations+import random+import sysfrom types import SimpleNamespaceimport pytest@@ -34,7 +36,11 @@ def _unknown_window(monkeypatch):# The request-scoped window is module state that outlives a test, and execute_tool# sets it deliberately. Restore it so one test cannot decide another's budget.token = tools._REQUEST_CONTEXT_TOKENS.set(tools._UNSET_CONTEXT_TOKENS)+ # Measured counts are cached per model for the life of the process, so one test's+ # backend double must not answer the next one's questions.+ tools._PROBE_COUNT_CACHE.clear()yield+ tools._PROBE_COUNT_CACHE.clear()tools._REQUEST_CONTEXT_TOKENS.reset(token)@@ -593,3 +599,524 @@ class TestAConfiguredCapIsNeverRaised:assert tools._tool_result_char_budget() == tools._MIN_PAGE_CHARSassert tools._page_char_budget() == tools._MIN_PAGE_CHARS+++class TestTheProbeIsNotPaidForTwice:+ """The measurement is worth its round trips; paying for it again is not.++ `count_chat_tokens` is two llama-server calls -- `/apply-template` then `/tokenize` --+ over a fresh connection each time, so every counter call here is two HTTP round trips+ on the path between a tool finishing and the model seeing its result. Measured on the+ merge base, with a 5,120-token window: an English result cost 2 counter calls and a+ dense one (base64, hexdump, sha256sum) cost 4, on every single result, forever.++ Three things were being bought and thrown away. The framing baseline is the same+ number for every result the process ever truncates, and it was priced per result. Its+ value cannot change the answer for a result that fits on its first count, and it was+ priced before that count was taken. And an estimate already at or below the readable+ floor is the answer whatever the tokenizer says, and it was measured anyway.++ Nothing here may change what is returned. Every test below asserts the number the+ merge base returns alongside the round trips it no longer takes.+ """++ _RATE = 1.33++ def _serving(+ self,+ monkeypatch,+ ctx,+ rate = None,+ identified = True,+ pid = 4242,+ extra_args = None,+ gguf = "/models/qwen3-4b.gguf",+ ):+ """A loaded llama.cpp backend that counts the calls it is asked to make.++ `is_loaded` really is `self._process is not None and self._healthy`, so a resident+ backend always has a process, and `pid` is what a reload changes.+ """+ rate = self._RATE if rate is None else rate+ calls = []++ def count_chat_tokens(messages, *a, **k):+ body = "".join(m["content"] for m in messages)+ calls.append(len(body))+ return 8 + int(len(body) / rate)++ backend = SimpleNamespace(+ is_loaded = True, context_length = ctx, count_chat_tokens = count_chat_tokens+ )+ if identified:+ # What a real backend exposes once a GGUF is resident.+ backend._process = SimpleNamespace(pid = pid)+ backend.model_identifier = "Qwen3-4B"+ backend._gguf_load_identity = ((gguf, 66306, 4242, 1),)+ backend._chat_template_override = None+ backend._extra_args = list(extra_args) if extra_args else None+ monkeypatch.setattr("routes.inference.get_llama_cpp_backend", lambda: backend)+ return calls, backend++ def test_an_estimate_at_the_floor_is_not_measured_at_all(self, monkeypatch):+ """A 1,024-token window leaves a 358-token share, so the estimate is already below+ the 2,000-character floor and `_dense_char_limit` clamps up to it whatever comes+ back. The merge base spent 2 counter calls (4 HTTP round trips) rediscovering it.+ """+ _window(monkeypatch, 1024)+ calls, _ = self._serving(monkeypatch, 1024)++ kept = tools._dense_char_limit("0123456789abcdef" * 2000, tools._MAX_PAGE_CHARS)++ assert kept == tools._MIN_PAGE_CHARS # the merge base's answer, unchanged+ assert calls == []++ def test_a_result_that_fits_does_not_price_the_framing_baseline(self, monkeypatch):+ """English is the common case and it fits on its first count. The baseline only+ ever decides whether a count that came in OVER budget is a real measurement, so+ for this result it is bought and never read: 2 counter calls where 1 answers."""+ _window(monkeypatch, 5120)+ calls, _ = self._serving(monkeypatch, 5120, rate = 4.2)+ text = ("The build finished and the archive was uploaded to the release bucket. ") * 400+ budget = tools._tool_result_char_budget()++ kept = tools._dense_char_limit(text, budget)++ assert kept == budget # English keeps every character it was allowed, as before+ assert len(calls) == 1+ assert calls == [budget], "the one call is the measurement, not the baseline"++ def test_the_baseline_is_priced_once_per_model_not_once_per_result(self, monkeypatch):+ """A dense result does need the baseline. It is the same number for the next one."""+ _window(monkeypatch, 5120)+ calls, _ = self._serving(monkeypatch, 5120)+ first = "0123456789abcdef" * 2000+ second = "fedcba9876543210" * 1500++ cold = tools._dense_char_limit(first, tools._tool_result_char_budget())+ cold_calls = list(calls)+ calls.clear()+ tools._dense_char_limit(second, tools._tool_result_char_budget())++ # A chunk of length 0 IS the baseline: the empty probe the guard measures against.+ assert 0 in cold_calls, "the first dense result pays for the baseline"+ assert calls, "the second result is still measured"+ assert 0 not in calls, "but the baseline is answered from the cache"+ assert len(calls) == len(cold_calls) - 1+ # And the answer is still the measured one.+ assert cold / self._RATE <= 5120 * tools._PAGE_CONTEXT_SHARE++ def test_the_same_result_twice_costs_nothing_the_second_time(self, monkeypatch):+ """Retries, regenerations and a model that runs the same command again."""+ _window(monkeypatch, 5120)+ calls, _ = self._serving(monkeypatch, 5120)+ text = "0123456789abcdef" * 2000+ budget = tools._tool_result_char_budget()++ first = tools._dense_char_limit(text, budget)+ assert calls, "the first pass must actually measure"+ calls.clear()+ second = tools._dense_char_limit(text, budget)++ assert second == first+ assert calls == []++ def test_a_different_model_never_reads_the_previous_one_s_counts(self, monkeypatch):+ """Same window, different tokenizer. The cache key carries the model's identity,+ so a reload cannot be answered from the model it replaced."""+ _window(monkeypatch, 5120)+ text = "0123456789abcdef" * 2000+ budget = tools._tool_result_char_budget()++ dense_calls, _ = self._serving(+ monkeypatch, 5120, rate = 1.33, pid = 111, gguf = "/models/dense.gguf"+ )+ dense = tools._dense_char_limit(text, budget)++ sparse_calls, _ = self._serving(+ monkeypatch, 5120, rate = 4.2, pid = 222, gguf = "/models/sparse.gguf"+ )+ sparse = tools._dense_char_limit(text, budget)++ assert sparse_calls, "the new model must be measured, not looked up"+ assert sparse > dense, "and priced by its own tokenizer"+ assert sparse == budget and dense_calls++ def test_a_backend_with_no_resident_process_is_not_cached(self, monkeypatch):+ """Nothing to tie a count to means no key guaranteed to change when the rendering+ does, so the safe answer is to keep paying. Every lightweight double lands here."""+ _window(monkeypatch, 5120)+ calls, _ = self._serving(monkeypatch, 5120, identified = False)+ text = "0123456789abcdef" * 2000+ budget = tools._tool_result_char_budget()++ first = tools._dense_char_limit(text, budget)+ spent = len(calls)+ calls.clear()+ second = tools._dense_char_limit(text, budget)++ assert second == first+ assert len(calls) == spent+ assert not tools._PROBE_COUNT_CACHE++ def test_a_count_that_failed_is_never_remembered_as_an_answer(self, monkeypatch):+ """A busy server is a property of the moment, not of the text. Caching the failure+ would turn one timeout into a permanent estimate for that result."""+ _window(monkeypatch, 5120)+ state = {"fail": True}++ def count_chat_tokens(messages, *a, **k):+ if state["fail"]:+ raise RuntimeError("llama-server is busy")+ return 8 + int(sum(len(m["content"]) for m in messages) / self._RATE)++ monkeypatch.setattr(+ "routes.inference.get_llama_cpp_backend",+ lambda: SimpleNamespace(+ is_loaded = True,+ context_length = 5120,+ count_chat_tokens = count_chat_tokens,+ _process = SimpleNamespace(pid = 4242),+ model_identifier = "Qwen3-4B",+ _gguf_load_identity = (("/models/qwen3-4b.gguf", 66306, 4242, 1),),+ _chat_template_override = None,+ _extra_args = None,+ ),+ )+ text = "0123456789abcdef" * 2000+ budget = tools._tool_result_char_budget()++ assert tools._dense_char_limit(text, budget) == budget # the estimate stands+ state["fail"] = False++ assert tools._dense_char_limit(text, budget) < budget # and is measured next time++ def test_the_cache_cannot_grow_without_bound(self, monkeypatch):+ _window(monkeypatch, 5120)+ self._serving(monkeypatch, 5120)+ budget = tools._tool_result_char_budget()++ for index in range(tools._PROBE_COUNT_CACHE_ENTRIES + 40):+ tools._dense_char_limit(f"{index:04d}" + "0123456789abcdef" * 2000, budget)++ held = sum(len(entry) for entry in tools._PROBE_COUNT_CACHE.values())+ assert held <= tools._PROBE_COUNT_CACHE_ENTRIES++ def test_the_cache_is_bounded_by_characters_and_not_only_by_entries(self, monkeypatch):+ """The entry count says nothing about size. Only a fetched page is capped at+ `_MAX_PAGE_CHARS`; a tool result's prefix is bounded by the configured cap and the+ window, and `_env_int` takes any positive integer, so one prefix can be enormous.+ Measured on this path: a 1,000,000-character cap on a 262k window cached 733,971+ characters from a single result, which 64 entries would multiply.+ """+ monkeypatch.setattr(tools, "_MAX_OUTPUT_CHARS", 1_000_000)+ _window(monkeypatch, 262_144)+ calls, _ = self._serving(monkeypatch, 262_144, rate = 4.0)+ budget = tools._tool_result_char_budget()++ assert budget > tools._MAX_PAGE_CHARS, "the premise: prefixes far exceed the page cap"++ for index in range(8):+ tools._dense_char_limit(f"{index:04d}" + "A" * 6_000_000, budget)++ held = sum(len(key) for entry in tools._PROBE_COUNT_CACHE.values() for key in entry)+ assert held <= tools._PROBE_COUNT_CACHE_CHARS+ # And the baseline, which is 0 characters, is still in there earning its keep.+ assert any("" in entry for entry in tools._PROBE_COUNT_CACHE.values())++ def test_a_prefix_too_large_to_hold_is_skipped_not_stored(self, monkeypatch):+ monkeypatch.setattr(tools, "_PROBE_COUNT_CACHE_CHARS", 5000)+ _window(monkeypatch, 5120)+ calls, _ = self._serving(monkeypatch, 5120)+ text = "0123456789abcdef" * 2000+ budget = tools._tool_result_char_budget()++ first = tools._dense_char_limit(text, budget)+ held = sum(len(key) for entry in tools._PROBE_COUNT_CACHE.values() for key in entry)+ calls.clear()+ second = tools._dense_char_limit(text, budget)++ assert second == first, "the answer never depends on what was cached"+ assert held <= 5000++ def test_the_guard_still_rejects_a_template_that_renders_no_content(self, monkeypatch):+ """The baseline is deferred, not dropped. A count that does not move off it is+ still not a measurement, and the caller still keeps its estimate."""+ _window(monkeypatch, 5120)+ monkeypatch.setattr(+ "routes.inference.get_llama_cpp_backend",+ lambda: SimpleNamespace(+ is_loaded = True,+ context_length = 5120,+ count_chat_tokens = lambda messages, *a, **k: 11,+ _process = SimpleNamespace(pid = 7),+ model_identifier = "Gemma-4",+ _gguf_load_identity = (("/models/gemma-4.gguf", 66306, 7, 1),),+ _chat_template_override = None,+ _extra_args = None,+ ),+ )++ assert tools._loaded_token_counter(5120)("0123456789abcdef" * 250) is None+ assert tools._dense_char_limit("0123456789abcdef" * 2000, 7168) == 7168++ def test_a_pass_through_chat_template_is_not_answered_from_the_managed_one(self, monkeypatch):+ """The gap `_chat_template_override` cannot see.++ User extra args are appended verbatim AFTER Studio's own flags and llama.cpp is+ last-wins, so `--chat-template` / `--chat-template-file` in extra args changes what+ `/apply-template` renders while every managed field stays exactly as it was. Same+ GGUF, same window, same managed override: reuse the counts and a prefix gets a+ price from a template that is no longer serving it, which is the irreducible+ overflow this budget exists to prevent, reintroduced through the cache.+ """+ _window(monkeypatch, 5120)+ text = "0123456789abcdef" * 2000+ budget = tools._tool_result_char_budget()++ self._serving(monkeypatch, 5120, rate = 1.33, pid = 900)+ dense = tools._dense_char_limit(text, budget)++ # Reloaded with only a pass-through template added. Everything managed is identical.+ calls, backend = self._serving(+ monkeypatch,+ 5120,+ rate = 4.2,+ pid = 901,+ extra_args = ["--chat-template", "chatml"],+ )+ sparse = tools._dense_char_limit(text, budget)++ assert backend.model_identifier == "Qwen3-4B"+ assert backend._chat_template_override is None+ assert calls, "the new template must be measured, not looked up"+ assert sparse > dense, "and priced by the template actually rendering"++ def test_the_extra_args_alone_are_enough_to_miss(self, monkeypatch):+ """Belt to the process id's braces: even holding the pid fixed, counts are not+ shared across a different command line."""+ _window(monkeypatch, 5120)+ text = "0123456789abcdef" * 2000+ budget = tools._tool_result_char_budget()++ self._serving(monkeypatch, 5120, rate = 1.33, pid = 5)+ tools._dense_char_limit(text, budget)++ calls, _ = self._serving(+ monkeypatch, 5120, rate = 1.33, pid = 5, extra_args = ["--chat-template-file", "/x.jinja"]+ )+ tools._dense_char_limit(text, budget)++ assert calls, "a different command line is a different rendering"++ def test_a_reload_of_the_very_same_configuration_still_misses(self, monkeypatch):+ """A restart is a new process whatever its arguments, so nothing survives it. This+ is what makes the key safe against flags nobody has thought of yet."""+ _window(monkeypatch, 5120)+ text = "0123456789abcdef" * 2000+ budget = tools._tool_result_char_budget()++ self._serving(monkeypatch, 5120, pid = 1000)+ first = tools._dense_char_limit(text, budget)++ calls, _ = self._serving(monkeypatch, 5120, pid = 1001)+ second = tools._dense_char_limit(text, budget)++ assert second == first, "same configuration, same answer"+ assert calls, "but re-measured rather than carried over the restart"++ def test_an_unhashable_identity_field_disables_the_cache_rather_than_raising(self, monkeypatch):+ _window(monkeypatch, 5120)+ calls, backend = self._serving(monkeypatch, 5120)+ backend._gguf_load_identity = {"not": "hashable"}+ text = "0123456789abcdef" * 2000+ budget = tools._tool_result_char_budget()++ first = tools._dense_char_limit(text, budget)+ spent = len(calls)+ calls.clear()++ assert tools._dense_char_limit(text, budget) == first+ assert len(calls) == spent+ assert not tools._PROBE_COUNT_CACHE++ def _template_down(+ self,+ monkeypatch,+ ctx,+ rate = None,+ fallback_rate = None,+ ):+ """`/apply-template` is down but `/tokenize` is not.++ `count_chat_tokens(strict = False)` then returns the plain-text fallback, which+ prices the bytes but drops the template's role markers and special tokens.+ """+ rate = self._RATE if rate is None else rate+ calls = []++ def count_chat_tokens(messages, *a, **k):+ body = "".join(m["content"] for m in messages)+ calls.append((len(body), bool(k.get("strict"))))+ if k.get("strict"):+ raise RuntimeError("llama-server could not render the chat template")+ return int(len(body) / (fallback_rate or rate)) or 1 # no framing: the fallback++ backend = SimpleNamespace(+ is_loaded = True,+ context_length = ctx,+ count_chat_tokens = count_chat_tokens,+ _process = SimpleNamespace(pid = 77),+ model_identifier = "Qwen3-4B",+ _gguf_load_identity = (("/models/qwen3-4b.gguf", 66306, 4242, 1),),+ _chat_template_override = None,+ _extra_args = None,+ )+ monkeypatch.setattr("routes.inference.get_llama_cpp_backend", lambda: backend)+ return calls++ def test_a_plain_text_fallback_count_is_used_but_never_retained(self, monkeypatch):+ """The count is still used: it tokenizes the real bytes, which is what catches+ dense ASCII, and the estimate it would otherwise fall back to undercharges base64+ several fold. It is simply not KEPT -- it prices a prompt the model will never be+ sent, so caching it would let one bad moment under-count that prefix for the life+ of the process.+ """+ _window(monkeypatch, 5120)+ calls = self._template_down(monkeypatch, 5120)+ text = "0123456789abcdef" * 2000+ budget = tools._tool_result_char_budget()++ kept = tools._dense_char_limit(text, budget)++ assert kept < budget, "the fallback still measured the bytes"+ assert not any(cache for cache in tools._PROBE_COUNT_CACHE.values())+ # And a later result re-measures rather than trusting it.+ calls.clear()+ tools._dense_char_limit(text, budget)+ assert calls++ def test_the_strict_attempt_is_made_once_per_result_not_once_per_probe(self, monkeypatch):+ """A template that will not render is not going to start mid-result, so asking+ again would spend round trips on a settled question. One extra attempt for the+ first probe, not one for every probe."""+ _window(monkeypatch, 5120)+ calls = self._template_down(monkeypatch, 5120)++ tools._dense_char_limit("0123456789abcdef" * 2000, tools._tool_result_char_budget())++ assert sum(1 for _, strict in calls if strict) == 1+ assert len(calls) > 2, "and the result really did take several passes"++ def test_a_healthy_template_pays_nothing_for_the_strict_check(self, monkeypatch):+ """Strict costs the same two llama-server calls as non-strict when the template+ renders, so verification is free in the case that matters."""+ _window(monkeypatch, 5120)+ calls, _ = self._serving(monkeypatch, 5120, rate = 4.2)+ text = ("The build finished and the archive was uploaded to the release bucket. ") * 400+ budget = tools._tool_result_char_budget()++ assert tools._dense_char_limit(text, budget) == budget+ assert len(calls) == 1++ def test_a_full_cache_evicts_rather_than_refusing_every_later_result(self, monkeypatch):+ """Refusing new entries once full was worse than not caching at all: most tool+ results are one-offs, so the first 64 distinct prefixes froze the cache on text+ nothing would ask about again."""+ _window(monkeypatch, 5120)+ calls, _ = self._serving(monkeypatch, 5120)+ budget = tools._tool_result_char_budget()+ for index in range(tools._PROBE_COUNT_CACHE_ENTRIES + 10):+ tools._dense_char_limit(f"{index:04d}" + "0123456789abcdef" * 2000, budget)++ repeated = "ZZZZ" + "0123456789abcdef" * 2000+ tools._dense_char_limit(repeated, budget)+ calls.clear()+ tools._dense_char_limit(repeated, budget)++ assert calls == [], "a recently measured result is still answered from the cache"++ def test_the_baseline_survives_a_cache_full_of_one_off_results(self, monkeypatch):+ """The sharp edge. The baseline is only priced when a count comes in OVER budget,+ so a process that handled 64 results that FIT first could never get it in at all,+ and every later dense result paid for it again -- the merge base's cost, for the+ life of the process. Measured before the fix: 4 counter calls (8 HTTP) every time.+ """+ _window(monkeypatch, 5120)+ budget = tools._tool_result_char_budget()++ # 64 English results, each of which fits on its first count, so `_framing()` never+ # runs and the baseline is never offered to the cache.+ self._serving(monkeypatch, 5120, rate = 4.2)+ for index in range(tools._PROBE_COUNT_CACHE_ENTRIES):+ tools._dense_char_limit(+ f"{index:04d}" + ("The build finished and the archive was uploaded. ") * 400,+ budget,+ )+ held = list(tools._PROBE_COUNT_CACHE.values())[0]+ assert len(held) == tools._PROBE_COUNT_CACHE_ENTRIES, "the cache really is full"+ assert tools._PROBE_BASELINE not in held, "and the baseline really is not in it"++ # Now dense results arrive. The first pays for the baseline; the rest must not.+ calls, _ = self._serving(monkeypatch, 5120, rate = 1.33)+ tools._dense_char_limit("D1" + "0123456789abcdef" * 2000, budget)+ calls.clear()+ tools._dense_char_limit("D2" + "0123456789abcdef" * 2000, budget)++ assert 0 not in [chars for chars in calls], "the baseline is held, not re-priced"+ held = list(tools._PROBE_COUNT_CACHE.values())[0]+ assert tools._PROBE_BASELINE in held, "and pinned against eviction"++ def test_concurrent_chats_do_not_corrupt_or_crash_on_the_shared_cache(self, monkeypatch):+ """Tool calls run in worker threads (`tool_stream_exec.stream_tool_execution` runs+ each invocation in one), so concurrent chats reach this process-global cache at the+ same time.++ A bare dict assignment is atomic under the GIL, but the LRU touch and the eviction+ are read-then-mutate sequences and are not. Measured before the lock, with this+ exact harness at 24 threads: 69 exceptions out of 12,000 truncations -- `KeyError`+ from popping a key another thread had just evicted, and "dictionary changed size+ during iteration" from choosing a victim while another thread inserted. None of+ them were caught on the way out of `_truncate`.+ """+ import threading++ _window(monkeypatch, 5120)+ self._serving(monkeypatch, 5120)+ # Cache pressure, so eviction fires on nearly every insert, and aggressive+ # preemption so the read-then-mutate windows are actually interleaved.+ monkeypatch.setattr(tools, "_PROBE_COUNT_CACHE_ENTRIES", 3)+ monkeypatch.setattr(tools, "_PROBE_COUNT_CACHE_CHARS", 12_000)+ previous_interval = sys.getswitchinterval()+ sys.setswitchinterval(1e-9)+ budget = tools._tool_result_char_budget()+ texts = {f"t{i}": f"{i:04d}" + "0123456789abcdef" * (300 + i * 40) for i in range(8)}+ errors: list[BaseException] = []+ answers: dict[str, set] = {name: set() for name in texts}++ def worker(seed):+ rng = random.Random(seed)+ for _ in range(150):+ name = rng.choice(list(texts))+ try:+ answers[name].add(tools._dense_char_limit(texts[name], budget))+ except BaseException as exc: # noqa: BLE001 -- the whole point is to catch it+ errors.append(exc)++ try:+ threads = [threading.Thread(target = worker, args = (seed,)) for seed in range(12)]+ for thread in threads:+ thread.start()+ for thread in threads:+ thread.join()+ finally:+ sys.setswitchinterval(previous_interval)++ assert errors == [], f"the shared cache raised under concurrency: {errors[:3]}"+ # And every thread agreed on every answer, which is the point of the whole change.+ for name, seen in answers.items():+ assert len(seen) == 1, f"{name} got different answers in different threads: {seen}"+ # The bounds still hold when several threads insert at once.+ for entry in tools._PROBE_COUNT_CACHE.values():+ assert len(entry) <= 3+ assert sum(map(len, entry)) <= 12_000