okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-0212-tools (ea765a571): tool-call fixes in serve/ and the native gateway, with their tests and docs311926416

main
5.7 KiB132 linesraw
1"""serve/test_tool_name_prefix.py - #5409: `<function=tools actio>` and the bare `<function=tools>` are a declared tool's call.
2
3Seen live (Qwen3.8-Flash-Next IQ3_S through Pi, after code-mode work that spells its tools `tools.actio(...)`): the model wrote
4the prefix of #4751 with a space for the dot, or alone.  Neither named a declared tool, so the block came back as answer text and
5the turn ended.  The space form is `tools.NAME` again; the bare prefix is the one declared tool that takes the call's parameters
6(only with tool_call_recovery).  Anything else stays text as before, and an exactly declared name always wins.
7
8    python -m unittest serve.test_tool_name_prefix -v
9"""
10import sys
11import unittest
12from pathlib import Path
13
14sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
15
16from serve.frontend import OutputParser  # noqa: E402
17
18ACTIO = {"name": "actio", "parameters": {"properties": {"command": {"type": "string"}}, "required": ["command"]}}
19READ = {"name": "read", "parameters": {"properties": {"path": {"type": "string"}}, "required": ["path"]}}
20BASH = {"name": "bash", "parameters": {"properties": {"command": {"type": "string"}, "timeout": {"type": "integer"}}}}
21
22
23def call(name, param="command", value="ls"):
24    return f"<tool_call>\n<function={name}>\n<parameter={param}>\n{value}\n</parameter>\n</function>\n</tool_call>"
25
26
27def run(text, tools, stream_tools=False, recover=True, thinking=False, step=3):
28    p = OutputParser(thinking=thinking, tools=tools, stream_tools=stream_tools, recover=recover)
29    evs = []
30    for i in range(0, len(text), step):
31        evs += p.feed(text[i:i + step])
32    evs += p.finish("stop")
33    return evs
34
35
36def calls(evs):
37    return [(e.call.name, e.call.arguments) for e in evs if e.kind == "tool_call"]
38
39
40def text(evs):
41    return "".join(e.text or "" for e in evs if e.kind in ("content", "reasoning"))
42
43
44class SpaceForm(unittest.TestCase):
45    def test_every_prefix_and_mode(self):
46        for prefix in ("tools", "functions", "default_api"):
47            for stream in (False, True):
48                for step in (1, 3, 1000):
49                    evs = run(call(f"{prefix} actio"), [ACTIO, READ], stream_tools=stream, step=step)
50                    self.assertEqual(calls(evs), [("actio", {"command": "ls"})], (prefix, stream, step))
51                    self.assertEqual(text(evs).strip(), "")
52
53    def test_without_recovery_too(self):
54        evs = run(call("tools actio"), [ACTIO], recover=False)
55        self.assertEqual(calls(evs), [("actio", {"command": "ls"})])
56
57    def test_text_before_the_call_is_kept(self):
58        evs = run("Let me look.\n" + call("tools actio"), [ACTIO])
59        self.assertEqual(calls(evs), [("actio", {"command": "ls"})])
60        self.assertEqual(text(evs).strip(), "Let me look.")
61
62    def test_in_the_reasoning(self):
63        evs = run("thinking...\n" + call("tools actio") + "\n</think>\n", [ACTIO], thinking=True)
64        self.assertEqual(calls(evs), [("actio", {"command": "ls"})])
65
66    def test_undeclared_name_stays_text(self):
67        for nm in ("tools nothing", "tools read now"):
68            evs = run(call(nm), [ACTIO, READ])
69            self.assertEqual(calls(evs), [], nm)
70            self.assertIn(f"<function={nm}>", text(evs))
71
72    def test_cut_off_name_is_flushed_as_text(self):
73        t = "<tool_call>\n<function=tools ac"
74        evs = run(t, [ACTIO])
75        self.assertEqual(calls(evs), [])
76        self.assertEqual(text(evs), t)
77
78    def test_two_calls(self):
79        evs = run(call("tools actio") + "\n" + call("tools read", "path", "/x"), [ACTIO, READ], stream_tools=True)
80        self.assertEqual(calls(evs), [("actio", {"command": "ls"}), ("read", {"path": "/x"})])
81
82
83class BarePrefix(unittest.TestCase):
84    def test_one_tool_fits(self):
85        for prefix in ("tools", "functions"):
86            for stream in (False, True):
87                for step in (1, 3, 1000):
88                    evs = run(call(prefix, "path", "/x"), [ACTIO, READ], stream_tools=stream, step=step)
89                    self.assertEqual(calls(evs), [("read", {"path": "/x"})], (prefix, stream, step))
90
91    def test_two_tools_fit_stays_text(self):
92        both = {"name": "also", "parameters": {"properties": {"path": {"type": "string"}}}}
93        evs = run(call("tools", "path", "/x"), [READ, both])
94        self.assertEqual(calls(evs), [])
95        self.assertIn("<function=tools>", text(evs))
96
97    def test_none_fits_stays_text(self):
98        evs = run(call("tools", "nope", "1"), [ACTIO, READ])
99        self.assertEqual(calls(evs), [])
100        self.assertIn("<function=tools>", text(evs))
101
102    def test_required_parameter_missing(self):
103        evs = run(call("tools", "timeout", "5"), [ACTIO, READ])    # `command` is required by actio
104        self.assertEqual(calls(evs), [])
105
106    def test_optional_only_call_fits_one(self):
107        evs = run(call("tools", "timeout", "5"), [BASH, READ])
108        self.assertEqual(calls(evs), [("bash", {"timeout": 5})])
109
110    def test_off_without_recovery(self):
111        evs = run(call("tools", "path", "/x"), [READ], recover=False)
112        self.assertEqual(calls(evs), [])
113        self.assertIn("<function=tools>", text(evs))
114
115    def test_a_declared_tool_named_tools_wins(self):
116        mine = {"name": "tools", "parameters": {"properties": {"x": {"type": "string"}}}}
117        evs = run(call("tools", "x", "1"), [mine, READ])
118        self.assertEqual(calls(evs), [("tools", {"x": "1"})])
119
120    def test_unfinished_bare_call_is_flushed_as_text(self):
121        t = "<tool_call>\n<function=tools>\n<parameter=path>\n/x"
122        evs = run(t, [READ], stream_tools=True)
123        self.assertEqual(calls(evs), [])
124        self.assertEqual(text(evs), t)
125
126    def test_no_tools_declared_is_unchanged(self):
127        evs = run(call("tools actio"), [])
128        self.assertEqual(calls(evs), [])
129
130
131if __name__ == "__main__":
132    unittest.main()