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: avoid a TypeError after a tools module reload (#11506)

Jiang Wu06c2ef19bba8
diff --git a/studio/backend/core/inference/tools.py b/studio/backend/core/inference/tools.py
index 089b108afb7..f1469b6edce 100644
--- a/studio/backend/core/inference/tools.py
+++ b/studio/backend/core/inference/tools.py
@@ -13978,7 +13978,10 @@ def execute_tool(
"arguments to save room, not content, so nothing ran. Write the actual content "
"out in full."
)
- effective_timeout = _EXEC_TIMEOUT if timeout is _TIMEOUT_UNSET else timeout
+ # By type, not `is _TIMEOUT_UNSET`: see `_request_context_tokens`.
+ effective_timeout = (
+ timeout if timeout is None or isinstance(timeout, (int, float)) else _EXEC_TIMEOUT
+ )
if name == "create_skill":
from .skills import SkillError, create_skill
@@ -16259,6 +16262,15 @@ def _loaded_context_tokens() -> int | None:
return None
+def _request_context_tokens() -> int | None:
+ """The request's window (an int, or None = unknowable: never probed), else the process probe. By type, not
+ `is _UNSET_CONTEXT_TOKENS`: an `execute_tool` held across a reload stores the old sentinel (#11384)."""
+ scoped = _REQUEST_CONTEXT_TOKENS.get()
+ if scoped is None or isinstance(scoped, int):
+ return scoped
+ return _loaded_context_tokens()
+
+
def _result_char_budget(cap: int) -> int:
"""`cap`, lowered to what the serving window can actually hold. Shared by fetched pages and by
terminal/python results, because the failure is the same: a fixed character cap has no
@@ -16267,10 +16279,7 @@ def _result_char_budget(cap: int) -> int:
that does not fit and the request goes irreducible. Measured live on a 5120-token window:
7043 and 6684 token requests refused, both on the code tools, whose 16,000-character cap is
about 4,000 tokens on its own."""
- scoped = _REQUEST_CONTEXT_TOKENS.get()
- # An explicit 0/None means asked, and unknowable (external provider), and must NOT fall through to the probe. Only
- # an absent value keeps the process-global read.
- ctx = _loaded_context_tokens() if scoped is _UNSET_CONTEXT_TOKENS else scoped
+ ctx = _request_context_tokens()
if not ctx:
return cap
# Clamped to `cap` on the way out, not only on the way in. The floor keeps a result worth reading when the WINDOW
@@ -16298,10 +16307,7 @@ def _page_char_budget() -> int:
Above roughly an 11k window this returns the old constant unchanged, so only the models that
cannot afford a whole page are affected.
"""
- scoped = _REQUEST_CONTEXT_TOKENS.get()
- # An explicit 0/None means asked, and unknowable (external provider), and must NOT fall through to the probe. Only
- # an absent value keeps the process-global read.
- ctx = _loaded_context_tokens() if scoped is _UNSET_CONTEXT_TOKENS else scoped
+ ctx = _request_context_tokens()
if not ctx:
return _MAX_PAGE_CHARS
return max(_MIN_PAGE_CHARS, min(_MAX_PAGE_CHARS, int(ctx * 4 * _PAGE_CONTEXT_SHARE)))
@@ -16323,10 +16329,7 @@ def _request_result_room() -> int | None:
def _window_context_tokens() -> int | None:
"""The window this request is served by, or None when it cannot be read."""
- scoped = _REQUEST_CONTEXT_TOKENS.get()
- # An explicit 0/None means asked, and unknowable (external provider), and must NOT fall through to the probe. Only
- # an absent value keeps the process-global read.
- ctx = _loaded_context_tokens() if scoped is _UNSET_CONTEXT_TOKENS else scoped
+ ctx = _request_context_tokens()
return ctx if ctx else None
diff --git a/studio/backend/tests/test_web_page_cap_fits_window.py b/studio/backend/tests/test_web_page_cap_fits_window.py
index 2dbbd7cfbe5..254b1fcc517 100644
--- a/studio/backend/tests/test_web_page_cap_fits_window.py
+++ b/studio/backend/tests/test_web_page_cap_fits_window.py
@@ -1116,3 +1116,31 @@ class TestTheProbeIsNotPaidForTwice:
for entry in tools._PROBE_COUNT_CACHE.values():
assert len(entry) <= 3
assert sum(map(len, entry)) <= 12_000
+
+
+def test_a_previous_generation_sentinel_reads_as_unset(monkeypatch):
+ """An `execute_tool` held across a reload of tools passes the old sentinels (#11384)."""
+ stale = object()
+ monkeypatch.setattr("state.tool_policy.require_tool_access", lambda **kw: None)
+ seen = []
+ monkeypatch.setattr(
+ tools,
+ "_search_knowledge_base_with_budget",
+ lambda a, s, timeout, c, **kw: seen.append(timeout) or "ok",
+ )
+
+ token = tools._REQUEST_CONTEXT_TOKENS.set(stale)
+ try:
+ assert tools._page_char_budget() == tools._MAX_PAGE_CHARS
+ assert tools._tool_result_char_budget() == tools._MAX_OUTPUT_CHARS
+ assert tools._window_context_tokens() is None
+ _window(monkeypatch, 4864)
+ assert tools._page_char_budget() == 6809
+ assert tools._tool_result_char_budget() == 6809
+ assert tools._window_context_tokens() == 4864
+
+ tools.execute_tool("search_knowledge_base", {}, timeout = stale, context_tokens = stale)
+ assert seen == [tools._EXEC_TIMEOUT]
+ assert tools._page_char_budget() == 6809
+ finally:
+ tools._REQUEST_CONTEXT_TOKENS.reset(token)