mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-04 07:18:50 +00:00
tool calls: a rejected undeclared call names the closest declared tool as a hint; nothing runs under a guessed name (#5904, #5933, #4622)
The strict default stays (coordinator's decision): a well-formed call of a tool the request did not declare
goes back as the text the model wrote, finish "stop", with strata_rejected_tool_calls beside it. Each entry
now also carries closest_declared when one declared tool is clearly the one meant (`read_file` -> `read`,
`Write` -> `write`, `serch` -> `search`): the one declared name the written one contains or is contained
in, ignoring case and _ - . , else a difflib match of 0.75 or more; no hint when two names fit or none is
close. Only a hint for the client or the model's next turn: executing a guessed tool is a safety risk, and
"undeclared_tool_calls": "call" stays the only way to run an undeclared call.
Tests: test_server ForcedToolChoice test_an_undeclared_call_names_the_closest_declared_tool_and_runs_nothing
and test_closest_declared. Before (2569c79ec, p2003-20261116-1817-da02): 6 subtests + 1 FAIL; after
(p2003-20261116-1820-fcd0): 13 passed with the existing rejected-call tests.
This commit is contained in:
1 parent
2569c79ec0
commit
198cb95c93
2 files changed
+52
-1
No files matched your search
+29
-1
@@ -6820,7 +6820,17 @@ class Service:
|
||||
with self.status_lock:
|
||||
self.totals["tool_calls_unread"] = self.totals.get("tool_calls_unread", 0) + parser.unread
|
||||
# #3796: the same names for the client (a machine-readable marker next to the text, which stays as the model wrote it)
|
||||
rejected = [{"name": nm, "reason": "undeclared"} for nm in named or []]
|
||||
# #5904 / #5933 / #4622: plus the declared tool the name comes closest to, as a hint for the client or the
|
||||
# model's next turn. Only a hint: the call is not run under that name (a guessed tool is a safety risk;
|
||||
# "undeclared_tool_calls": "call" stays the only way to run an undeclared call)
|
||||
declared = [t.get("name") for t in tools or [] if isinstance(t, dict) and isinstance(t.get("name"), str)]
|
||||
rejected = []
|
||||
for nm in named or []:
|
||||
entry = {"name": nm, "reason": "undeclared"}
|
||||
near = closest_declared(nm, declared)
|
||||
if near:
|
||||
entry["closest_declared"] = near
|
||||
rejected.append(entry)
|
||||
if stops is not None and stops.hit is None and stops.held:
|
||||
yield "event", Event("content", stops.flush()) # the held tail was not a stop string after all
|
||||
done = {"finish": finish, "completion_tokens": n, "reused": (timings or {}).get("cache_n", 0),
|
||||
@@ -6833,6 +6843,24 @@ class Service:
|
||||
yield "done", done
|
||||
|
||||
|
||||
def closest_declared(name: str, declared: list[str]) -> str | None:
|
||||
"""#5904 / #5933: the declared tool name an undeclared one comes closest to (`read_file` -> `read`), for the hint in
|
||||
strata_rejected_tool_calls; None when none is clearly close. The one declared name the written one contains or is
|
||||
contained in (ignoring case and `_` / `-` / `.`), else the best difflib match at a ratio of 0.75 or more."""
|
||||
import difflib
|
||||
if not name or not declared:
|
||||
return None
|
||||
norm = lambda s: re.sub(r"[_\-.\s]", "", s.lower()) # noqa: E731
|
||||
n = norm(name)
|
||||
inside = [d for d in declared if norm(d) and (norm(d) in n or n in norm(d))]
|
||||
if len(inside) == 1:
|
||||
return inside[0]
|
||||
best = difflib.get_close_matches(name.lower(), [d.lower() for d in declared], n=1, cutoff=0.75)
|
||||
if best:
|
||||
return next(d for d in declared if d.lower() == best[0])
|
||||
return None
|
||||
|
||||
|
||||
def prompt_tokens_seen(prompt_tokens: int, last: dict) -> int:
|
||||
"""#471: the prompt tokens a request got through - all of them, unless the engine's DONE line says a cancel stopped
|
||||
its prompt read part-way (then the reused ones plus those read). /metrics' history and totals count these, so a
|
||||
|
||||
@@ -4818,6 +4818,29 @@ class ForcedToolChoice(unittest.TestCase):
|
||||
if not stream:
|
||||
self.assertIn("<function=execute_gdscript>", b["choices"][0]["message"]["content"]) # the text is untouched
|
||||
|
||||
def test_an_undeclared_call_names_the_closest_declared_tool_and_runs_nothing(self):
|
||||
"""#5904 / #5933 / #4622: `read_file` for a declared `read` stays text with finish "stop" (nothing runs under a
|
||||
guessed name); the marker names the closest declared tool as a hint. A name close to nothing gets no hint."""
|
||||
tools = [{"type": "function", "function": {"name": n, "description": n, "parameters": {
|
||||
"type": "object", "properties": {"q": {"type": "string"}}}}} for n in ("read", "write", "search")]
|
||||
for written, hint in (("read_file", "read"), ("Write", "write"), ("serch", "search"), ("execute_gdscript", None)):
|
||||
self.says(self._undeclared_call_answer(written))
|
||||
for stream in (False, True):
|
||||
with self.subTest(written=written, stream=stream):
|
||||
code, b = self.openai(stream=stream, tools=tools)
|
||||
finish, calls, _ = self.call_of(code, b, stream)
|
||||
self.assertEqual((finish, calls), ("stop", []), b)
|
||||
want = {"name": written, "reason": "undeclared", **({"closest_declared": hint} if hint else {})}
|
||||
self.assertEqual(self._rejected_of(b, stream), [want])
|
||||
|
||||
def test_closest_declared(self):
|
||||
from serve.server import closest_declared
|
||||
self.assertEqual(closest_declared("read_file", ["read", "write"]), "read")
|
||||
self.assertEqual(closest_declared("ReadFile", ["read_file", "write"]), "read_file")
|
||||
self.assertIsNone(closest_declared("file", ["read_file", "write_file"])) # two candidates: no guess
|
||||
self.assertIsNone(closest_declared("gashfunction_calls", ["grep", "read"]))
|
||||
self.assertIsNone(closest_declared("x", []))
|
||||
|
||||
def test_undeclared_tool_calls_call_hands_the_call_to_the_client(self):
|
||||
"""#4622: "undeclared_tool_calls": "call" returns the well-formed call of an undeclared name as a tool call (the client has its
|
||||
own policy for unknown names); the default ("text") is the test above, unchanged."""
|
||||
|
||||
Reference in new issue
Block a user