Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor(studio): simplify the whole-document retry's tool-choice han…
…dling

Only tool_choice="required" is satisfied by an accepted document, which leaves the round's tool catalogue unchanged and only switches the choice to auto, so drop the candidate catalogue, the document_satisfies_choice parameter and the _round_tool_names helper (restoring the original execution-gate code and its source check). Recost admission only when the document grew the prompt.
  • Loading branch information
Lyxot committed Nov 27, 2026
commit 694bf9160ee7d30e1019c948cb02aed2de527dc0
43 changes: 12 additions & 31 deletions studio/backend/core/inference/llama_cpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -42071,7 +42071,7 @@ def _loop_budget_left(spent_this_attempt: int) -> "Optional[int]":
context_overflow == "truncate_oldest" and self._effective_context_length
)

def _round_tool_choice(active_tools, *, document_satisfies_choice = False):
def _round_tool_choice(active_tools):
from core.inference.chat_template_helpers import (
neutralize_tool_descriptions,
reconciled_tool_choice,
Expand All @@ -42082,10 +42082,7 @@ def _round_tool_choice(active_tools, *, document_satisfies_choice = False):
active_tools, _markup_cache, self.markup_profile
)
requested_choice = "auto" if tool_choice is None else tool_choice
if (_forced_choice_resolved or document_satisfies_choice) and requested_choice not in (
"auto",
"none",
):
if _forced_choice_resolved and requested_choice not in ("auto", "none"):
requested_choice = "auto"
requested_choice = reconciled_tool_choice(requested_choice, tools, safe_tools) or "auto"
forced_name = forced_tool_name(requested_choice)
Expand All @@ -42100,13 +42097,6 @@ def _round_tool_choice(active_tools, *, document_satisfies_choice = False):
requested_choice = "required"
return safe_tools, requested_choice, forced_name

def _round_tool_names(safe_tools, forced_name):
return {
(tool.get("function") or {}).get("name")
for tool in safe_tools
if (tool.get("function") or {}).get("name")
} | (set() if forced_name else _loadable_mcp_names)

def _round_reasoning_kw():
return _with_template_extras(
self._request_reasoning_kwargs(
Expand Down Expand Up @@ -42196,7 +42186,11 @@ def _count() -> None:

safe_tools, requested_choice, forced_name = _round_tool_choice(active_tools)
# Gate the markerless bare-JSON form on enabled names so an ordinary JSON answer isn't misread as a call.
_enabled_tool_names = _round_tool_names(safe_tools, forced_name)
_enabled_tool_names = {
(tool.get("function") or {}).get("name")
for tool in safe_tools
if (tool.get("function") or {}).get("name")
} | (set() if forced_name else _loadable_mcp_names)
# Shared signal tuple so GGUF BUFFERING wakes on every format the parser knows (like safetensors).
_tool_xml_signals = _SHARED_TOOL_XML_SIGNALS

Expand Down Expand Up @@ -42300,15 +42294,8 @@ def _count() -> None:
len(base),
)
candidate = base[:at] + refreshed["messages"] + base[at:]
# A forced name stays forced: this fit priced and worded the prompt for that one tool.
satisfies_choice = tool_choice == "required"
candidate_tools, candidate_choice, candidate_name = (
_round_tool_choice(
active_tools, document_satisfies_choice = satisfies_choice
)
)
measured = _fit_count_tokens(
candidate, candidate_tools, _reasoning_kw
candidate, safe_tools, _reasoning_kw
)
# The fit's own target, also on a replay: headroom, plus the recall reserve a rolling fit holds back.
budget = prompt_budget(
Expand All @@ -42328,20 +42315,14 @@ def _count() -> None:
# A larger prompt leaves this fit's recall the room up to the headroom, not into it.
if measured > fitted_tokens:
_fit_recall_cap = max(0, headroom_line - measured)
_fit_grew_prompt = True
_auto = refreshed
conversation = candidate
truncation["prompt_tokens_after"] = measured
_fit_grew_prompt = True
if satisfies_choice:
# A forced name stays forced: this fit priced and worded the prompt for that one tool.
if tool_choice == "required":
_forced_choice_resolved = True
safe_tools, requested_choice, forced_name = (
candidate_tools,
candidate_choice,
candidate_name,
)
_enabled_tool_names = _round_tool_names(
safe_tools, forced_name
)
requested_choice = "auto"
except Exception:
logger.debug(
"Whole-document retry after compaction failed", exc_info = True
Expand Down
4 changes: 1 addition & 3 deletions studio/backend/tests/test_control_markup_neutralize_7066.py
Original file line number Diff line number Diff line change
Expand Up @@ -2727,15 +2727,13 @@ def test_gguf_execution_gate_is_built_from_the_sanitized_catalog():
source = (_REPO_ROOT / "studio" / "backend" / "core" / "inference" / "llama_cpp.py").read_text(
encoding = "utf-8"
)
gate = source.index("def _round_tool_names(safe_tools, forced_name):")
gate = source.index("_enabled_tool_names = {")
sweep = source.index(
"neutralize_tool_descriptions(\n active_tools, _markup_cache, self.markup_profile\n )"
)
assert sweep < gate, "the catalog must be sanitized before the execution gate is built"
window = source[gate : gate + 220]
assert "for tool in safe_tools" in window
call = source.index("_enabled_tool_names = _round_tool_names(safe_tools, forced_name)")
assert "= _round_tool_choice(active_tools)" in source[call - 300 : call]


@pytest.mark.parametrize("root", ["parameters", "input_schema", "inputSchema", "outputSchema"])
Expand Down