Repository navigation
Studio: do not let the default tool policy reject every plain Anthropic message - #8023
Conversation
`unsloth studio run` resolves the server-side tool policy to on unless --disable-tools (resolve_tool_policy: "tools default on for every bind", ce193c2). The permission gate added in #7079 read that resolved policy as "this request selected server tools", so for an ordinary tool-free POST /v1/messages it collected the whole catalog, found terminal in it, saw no permission_mode, and returned 400. Every plain Anthropic chat request against a default server answered: permission_mode 'ask' has no confirmation channel for Anthropic Messages server tools A process-wide default is not a per-request selection. The gate now fires on an explicit ask only, enable_tools/mcp_enabled or an Anthropic server-tool type in `tools`, and --disable-tools still vetoes both. Everything the gate rejected because the request asked for something it cannot confirm still rejects: enable_tools with ask or an omitted mode, a bare terminal/python tool type, render_html, and ask for a safe-only selection. Nothing caught it because the four e2e cases that exercise this path live in tests/test_studio_api.py, which studio-backend-ci.yml ignores: they need a live model and a 2 GB GGUF download.
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: bb7b29507a
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| ) and not _anthropic_request_has_image(payload) | ||
| not _has_client_tool | ||
| and _policy_pre is not False | ||
| and (_asked_pre or bool(requested_studio_tools)) |
There was a problem hiding this comment.
Keep gating tools enabled by the process policy
When the process tool policy is True—the default for unsloth studio run—a tool-free request makes this condition false and skips the permission rejection, but the later routing still resolves _effective_enable_tools(payload) to true (inference.py:15759-15764) and enters the server-tool loop with the full catalog, including terminal and python. That call does not enable confirm_tool_calls, so an omitted permission mode cannot pause model-emitted local calls for approval. Ordinary /v1/messages requests can therefore execute server tools without a confirmation channel; either retain the gate for policy-enabled tool routing or ensure policy alone does not enter the server-tool loop.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Correct, and reproduced: with the process policy at its unsloth studio run default, a tool-free request took the tools path with ['python', 'render_html', 'search_knowledge_base', 'terminal', 'web_search'] and confirm_tool_calls=None. Lifting the gate off the default fixed the rejection and left exactly the execution the gate existed to stop.
Fixed in d47a686 by your second suggestion: the routing no longer enters the loop on the policy alone. Both sites now call one predicate, _anthropic_selects_server_tools, since deciding "did this request select server tools" twice is what let the gate and the router disagree in the first place.
plain, policy default-on -> plain path, tools=[]
enable_tools=true, off -> tools path, full catalog
enable_tools=true, omitted -> HTTP 400
terminal type, full -> tools path, tools=['terminal']
terminal type, omitted -> HTTP 400
The test now asserts the path rather than that a response came back, and fails on the old routing with "a tool-free request took the server-tool loop on the process default alone". 592 passed across the tool-policy suites.
Lifting the gate off the process default fixed the rejection but not its twin: the tool routing below decides server-tool mode from `_effective_enable_tools(payload)` too, so a tool-free request on a default server still entered the local tool loop with the full catalog, python/terminal/render_html included, and confirm_tool_calls unset. That is the execution this channel has no way to confirm, which is what the gate existed to stop. Deciding "did this request select server tools" twice is what let the two disagree, so both now call `_anthropic_selects_server_tools`. A tool-free request takes the plain chat path, an explicit enable_tools/mcp_enabled/server-tool type still routes to the loop under a mode that permits it, and --disable-tools or enable_tools=false still vetoes.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: d47a686dbe
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| """ | ||
| if has_client_tool or _effective_enable_tools(payload) is False: | ||
| return False | ||
| asked = payload.enable_tools is True or bool(getattr(payload, "mcp_enabled", False)) |
There was a problem hiding this comment.
Honor the forced-on tool policy for Anthropic requests
When the server is launched with unsloth studio run --enable-tools, the launcher stores True as the process policy, which is documented as forcing tools on for every request (unsloth_cli/_tool_policy.py:31-35 and studio/backend/state/tool_policy.py:8-10). This predicate only treats False specially and then requires a second per-request tool signal, so an ordinary /v1/messages request now takes the plain path even under the explicit forced-on flag. Distinguish the default-on state from an explicit forced-on policy, or otherwise preserve the flag's documented behavior while avoiding the default permission rejection.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
The divergence from the documented flag is real, but restoring the behaviour is not available: honouring it reinstates the outage rather than forcing tools on.
resolve_tool_policy is return True if flag is None else flag, so --enable-tools and no flag at all both store the same True. There is no explicit-forced-on state to distinguish today. And if there were, a plain request on such a server would select terminal with permission_mode omitted, which is exactly the condition the gate above rejects, so the flag would produce a 400 rather than tools. The only way to get tools out of it is to read the flag as consent to skip confirmation, which its documentation does not claim and which bypass_permissions already exists to express.
So the flag's stated meaning and the permission gate are in direct conflict on this channel, and that predates this PR: since #7079 an --enable-tools server has answered 400 to every plain Messages request, so nothing working is being taken away here.
I have made the documentation match instead, in f372686: both CLI help strings and the tool_policy docstring now say the on direction comes from the request on /v1/messages because it has no confirmation channel, while the off direction still applies everywhere. /v1/chat/completions is unchanged and still forces the loop on, which I have left alone rather than widen this PR.
| """ | ||
| if has_client_tool or _effective_enable_tools(payload) is False: | ||
| return False | ||
| asked = payload.enable_tools is True or bool(getattr(payload, "mcp_enabled", False)) |
There was a problem hiding this comment.
Resolve MCP-only requests to MCP tools
When the process policy is unset, this newly makes mcp_enabled: true enter the Anthropic server-tool path, but that path later calls _select_anthropic_server_tools(ALL_TOOLS, ...) and never loads enabled MCP schemas. Consequently an MCP-only request with the default permission mode is rejected because terminal/python are considered selected, while permission_mode: off or full exposes those built-ins instead of the requested MCP tools. The regular tool route explicitly handles this case by starting with no built-ins and appending get_enabled_mcp_tools() (inference.py:2790-2812); the Anthropic route needs equivalent MCP-aware selection before treating this flag as server-tool intent.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Right, and my assumption that the field was inert was wrong. AnthropicMessagesRequest sets model_config = {"extra": "allow"}, and pydantic serves extras through BaseModel.__getattr__, so mcp_enabled: true does arrive:
extra stored: {'mcp_enabled': True}
getattr mcp_enabled: True
On a stock server with no CLI flags, that request went from the plain path on main to the tool loop holding ['python', 'render_html', 'search_knowledge_base', 'terminal', 'web_search'] on this branch, and anthropic_messages contains no MCP discovery at all, so an MCP-only caller got built-ins under an MCP-shaped name.
Dropped in f372686. It is an ask on the OpenAI routes because those consult get_enabled_mcp_tools; mirroring it here created the divergence rather than preventing it. Before this branch the key did nothing on this route, so dropping it restores that, and the test now pins it.
…g stops AnthropicMessagesRequest is extra="allow", so `mcp_enabled: true` really does arrive and pydantic serves it through __getattr__: reading it as intent routed such a request into a loop holding ALL_TOOLS' terminal/python and not one MCP tool, since nothing on this route loads MCP schemas. It is an ask on the OpenAI routes because those wire MCP discovery. Before this branch it did nothing here, so dropping it restores that. The CLI help and the tool_policy docstring said --enable-tools forces tools on for every request. On /v1/messages it no longer does, deliberately: that channel has no confirmation prompt, so the on direction has to come from the request. The off direction still applies everywhere. Documented rather than left to be discovered.
|
@codex review |
|
Codex Review: Didn't find any major issues. 🚀 Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
On a default
unsloth studio runserver, every ordinaryPOST /v1/messagesanswers 400:Two changes met:
ce193c243made the server-side tool policy default to on for every bind (resolve_tool_policy:return True if flag is None else flag), sounsloth studio runcallsset_tool_policy(True)unless--disable-toolsis passed.#7079added a pre-switch permission gate that reads the resolved policy as "this request selected server tools". For a tool-free request it therefore collected the whole catalog, foundterminalin it, saw nopermission_mode, and rejected.A process-wide default is not a per-request selection. The gate now fires on an explicit ask only:
enable_tools/mcp_enabled, or an Anthropic server-tool type intools.--disable-toolsstill vetoes both forms.What still rejects
Verified by driving the real route with
set_tool_policy(True), the default a server actually runs with:enable_tools: true, mode omittedenable_tools: true,permission_mode: ask{"type": "terminal"}, mode omitted orauto{"type": "terminal"},permission_mode: fullweb_searchonly,permission_mode: ask{"type": "terminal"}under--disable-toolsrender_htmlunder ask/auto/omittedWhy CI never saw it
The four end-to-end cases that cover this path (
test_anthropic_basic,test_anthropic_streaming,test_anthropic_with_tools,test_anthropic_tool_choice_any) live intests/test_studio_api.py, whichstudio-backend-ci.ymlignores because it needs a live model and a ~2 GB GGUF download.test_the_process_tool_default_alone_is_not_a_server_tool_selectionnow pins the case in the unit suite, where it fails before this change and passes after.What this removes
Two behaviours change beyond the rejection, both deliberate:
--enable-toolsno longer enables server tools on/v1/messages. The policy is veto-only on that channel now: the off direction still applies everywhere, the on direction has to come from the request. This is not a state the flag could reach anyway, becauseresolve_tool_policyisreturn True if flag is None else flag, so the explicit flag and the bare default store the sameTrue./v1/chat/completionsis untouched and still forces the loop on. The CLI help and thetool_policydocstring have been corrected to say so.toolspath withpython/terminal/render_htmlandconfirm_tool_callsunset. Both the gate and the router now call one predicate,_anthropic_selects_server_tools, since deciding "did this request select server tools" twice is what let them disagree.mcp_enabledis deliberately not an ask here. It is one on the OpenAI routes because those consultget_enabled_mcp_tools; this route has no MCP discovery, so honouring it would answer an MCP-only request with the built-interminal/python. The request model isextra = "allow", so the key does arrive and this is not theoretical.Still reachable, unchanged from main
{"enable_tools": true, "permission_mode": "off"}hands the full catalog to the loop, andanthropic_messagesnever passesconfirm_tool_calls, whose callee defaults it toFalse-- soneeds_confirmis False on this channel in every mode. The sandbox stays on unlessfullorbypass_permissions. That is pre-existing and out of scope here, but it is the reason the on direction is per-request rather than a server default.