* Studio: Add inline confirmation (Allow/Always allow/Deny) for tool calls * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fix race in tool-call confirmation gate * Studio: gate built-in tool calls and harden the confirmation handshake The Allow / Always allow / Deny controls only lived in the fallback tool card, but the built-in tools (web search, python, terminal, code execution, image generation) render with their own components and so never showed the buttons. Those calls paused after tool_start with no way to approve them, hanging until the 1 hour timeout. Only MCP tools, which use the fallback renderer, actually worked. Render the controls for every tool card by wrapping each registered tool component (and the fallback) in thread.tsx with a shared ToolConfirmationControls, so the gate applies uniformly. Also make the handshake robust: - The gate keys on a per-call approval_id minted by the backend and echoed in tool_start, instead of session_id alone, so a stale or concurrent confirmation can no longer resolve the wrong call. - The approval slot is registered before tool_start is yielded, closing the race where a fast click or an auto "Always allow" could reach the backend before the waiter existed. - The frontend resolves with the same session id the request was sent with (plus the approval_id), fixing the new-thread mismatch where the confirmation targeted a different session than the blocked stream. - The confirm endpoint returns {resolved}; the UI keeps the buttons and shows a retry hint until the backend confirms a match, instead of hiding them on a failed or mistargeted post. - The gate runs after the disabled-tool and duplicate-call checks, so a call that will not execute is not put up for approval. A denied call is still excluded from duplicate detection, so re-issuing and approving it works. - "Always allow" is scoped per session to match the backend gate. Add backend tests for the approval registry, the SSE no-deadlock handshake, and the loop integration (allow, deny, disabled, duplicate, re-issue after deny). * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Move "Confirm tool calls" to the Tools section * Studio: Keep tool group open while a tool call awaits confirmation * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fix tool confirmation session scope for PR #5869 * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fix confirmation follow-ups for PR #5869 * Apply pre-commit formatting for PR #5869 * Fix confirmation cleanup for PR #5869 * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Harden confirmation lookups for PR #5869 * Studio: make the tool-call confirmation decision immutable resolve_tool_decision accepted a second confirmation for the same approval_id and overwrote slot["decision"] in the window before the waiter reads it and pops the slot, so a duplicate or out-of-order POST could flip an Allow to Deny (and returned a misleading resolved:true). Reject once the slot's event is already set so the first decision wins. Adds a regression test. * Fix/adjust tool confirmations for PR #5869 * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Daniel Han <danielhanchen@gmail.com> Co-authored-by: wasimysaid <wasimysdev@gmail.com>
170 lines
5.4 KiB
Python
170 lines
5.4 KiB
Python
# SPDX-License-Identifier: AGPL-3.0-only
|
|
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
|
|
|
|
"""Integration tests for the confirmation gate inside the real tool loop.
|
|
|
|
These drive ``run_safetensors_tool_loop`` (no model -- hand-crafted fake
|
|
generators) with ``confirm_tool_calls=True`` and resolve each pending
|
|
decision inline. The slot is registered before ``tool_start`` is yielded,
|
|
so resolving right after receiving that event always lands before the
|
|
loop blocks. Covers: allow executes once, deny skips execution and feeds
|
|
back the rejection, disabled/duplicate calls are not prompted, and a
|
|
denied call does not pollute duplicate detection.
|
|
"""
|
|
|
|
import pytest
|
|
|
|
from core.inference.safetensors_agentic import run_safetensors_tool_loop
|
|
from state import tool_approvals
|
|
from state.tool_approvals import TOOL_REJECTED_MESSAGE, resolve_tool_decision
|
|
|
|
_SESSION = "loop-session"
|
|
|
|
|
|
@pytest.fixture(autouse = True)
|
|
def _clear_pending():
|
|
with tool_approvals._lock:
|
|
tool_approvals._pending.clear()
|
|
yield
|
|
with tool_approvals._lock:
|
|
tool_approvals._pending.clear()
|
|
|
|
|
|
class _FakeExecuteTool:
|
|
def __init__(self):
|
|
self.calls = []
|
|
|
|
def __call__(
|
|
self,
|
|
name,
|
|
arguments,
|
|
*,
|
|
cancel_event = None,
|
|
timeout = None,
|
|
session_id = None,
|
|
rag_scope = None,
|
|
):
|
|
self.calls.append((name, arguments))
|
|
return f"RESULT[{name}]"
|
|
|
|
|
|
def _tool_call(name, args_json):
|
|
return f'<tool_call>{{"name": "{name}", "arguments": {args_json}}}</tool_call>'
|
|
|
|
|
|
def _multi_turn(turns):
|
|
"""A single_turn generator that yields one full snapshot per turn."""
|
|
turn_iter = iter(turns)
|
|
|
|
def _gen(_messages):
|
|
try:
|
|
yield next(turn_iter)
|
|
except StopIteration:
|
|
return
|
|
|
|
return _gen
|
|
|
|
|
|
_DEFAULT_TOOLS = [
|
|
{"type": "function", "function": {"name": "python"}},
|
|
{"type": "function", "function": {"name": "web_search"}},
|
|
]
|
|
|
|
|
|
def _drive(
|
|
turns,
|
|
decisions,
|
|
*,
|
|
tools = None,
|
|
):
|
|
"""Run the loop, resolving each gated tool_start with the next decision.
|
|
|
|
The advertised ``tools`` list drives the loop's enabled-tool filter
|
|
(pass a list omitting a tool to make a call to it "disabled").
|
|
Returns (events, execute_calls).
|
|
"""
|
|
decision_iter = iter(decisions)
|
|
exec_fn = _FakeExecuteTool()
|
|
gen = run_safetensors_tool_loop(
|
|
single_turn = _multi_turn(turns),
|
|
messages = [{"role": "user", "content": "hi"}],
|
|
tools = _DEFAULT_TOOLS if tools is None else tools,
|
|
execute_tool = exec_fn,
|
|
session_id = _SESSION,
|
|
confirm_tool_calls = True,
|
|
)
|
|
events = []
|
|
for ev in gen:
|
|
events.append(ev)
|
|
if ev["type"] == "tool_start" and ev.get("awaiting_confirmation"):
|
|
# Slot is already registered (begin ran before this yield), so
|
|
# the decision lands before the loop enters its blocking wait.
|
|
resolve_tool_decision(ev["approval_id"], next(decision_iter), session_id = _SESSION)
|
|
return events, exec_fn.calls
|
|
|
|
|
|
def _tool_starts(events):
|
|
return [e for e in events if e["type"] == "tool_start"]
|
|
|
|
|
|
def _tool_ends(events):
|
|
return [e for e in events if e["type"] == "tool_end"]
|
|
|
|
|
|
def test_allow_executes_the_tool_once():
|
|
events, calls = _drive(
|
|
[_tool_call("python", '{"code": "print(1)"}'), "final answer"],
|
|
["allow"],
|
|
)
|
|
starts = _tool_starts(events)
|
|
assert len(starts) == 1
|
|
assert starts[0]["awaiting_confirmation"] is True
|
|
assert starts[0]["approval_id"]
|
|
assert calls == [("python", {"code": "print(1)"})]
|
|
assert _tool_ends(events)[0]["result"] == "RESULT[python]"
|
|
|
|
|
|
def test_deny_skips_execution_and_feeds_rejection():
|
|
events, calls = _drive(
|
|
[_tool_call("python", '{"code": "print(1)"}'), "final answer"],
|
|
["deny"],
|
|
)
|
|
assert calls == [] # tool never ran
|
|
assert _tool_ends(events)[0]["result"] == TOOL_REJECTED_MESSAGE
|
|
|
|
|
|
def test_disabled_tool_is_not_prompted():
|
|
events, calls = _drive(
|
|
[_tool_call("python", '{"code": "print(1)"}'), "final answer"],
|
|
[],
|
|
tools = [{"type": "function", "function": {"name": "web_search"}}],
|
|
)
|
|
assert _tool_starts(events) == []
|
|
assert _tool_ends(events) == []
|
|
assert calls == []
|
|
|
|
|
|
def test_duplicate_call_is_not_prompted():
|
|
same = _tool_call("python", '{"code": "print(1)"}')
|
|
events, calls = _drive([same, same, "final answer"], ["allow"])
|
|
starts = _tool_starts(events)
|
|
assert len(starts) == 1
|
|
assert starts[0]["awaiting_confirmation"] is True
|
|
assert calls == [("python", {"code": "print(1)"})]
|
|
assert len(_tool_ends(events)) == 1
|
|
|
|
|
|
def test_denied_call_can_be_reissued_and_approved():
|
|
# Deny, then the model re-issues the identical call -> approving it must
|
|
# execute, not get suppressed as a duplicate (denied calls are not added
|
|
# to the duplicate-detection history).
|
|
same = _tool_call("python", '{"code": "print(1)"}')
|
|
events, calls = _drive([same, same, "final answer"], ["deny", "allow"])
|
|
starts = _tool_starts(events)
|
|
assert len(starts) == 2
|
|
assert starts[0]["awaiting_confirmation"] is True
|
|
assert starts[1]["awaiting_confirmation"] is True # not treated as dup
|
|
assert calls == [("python", {"code": "print(1)"})] # ran once, on approve
|
|
ends = _tool_ends(events)
|
|
assert ends[0]["result"] == TOOL_REJECTED_MESSAGE
|
|
assert ends[1]["result"] == "RESULT[python]"
|