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
4.7 KiB104 linesraw
1"""serve/test_tool_opener_misspelt.py - #5276: `<toolcall>` (no underscore) before `<function=NAME>` of a declared tool opens the call.
2
3Seen live (Qwen3.8-Flash-Next UD-Q4_K_XL, OpenMinis on /v1/messages): the model wrote the opener as ordinary text, misspelled
4`<toolcall>`, then a whole call of a declared tool and the real `</tool_call>`.  The block came back as answer text, the turn
5ended and the agent waited for a result that never came.  The misspelling counts only where a `<tool_call>` would open a call
6(start of a line, outside code) and only when a declared tool's `<function=NAME>` follows; prose never triggers it.
7
8    python -m unittest serve.test_tool_opener_misspelt -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
18BASH = {"name": "bash", "parameters": {"properties": {"command": {"type": "string"}, "tooltitle": {"type": "string"}}}}
19BODY = ("<function=bash>\n<parameter=command>\ncd proj && rg -o 'x' landing.html | sort | uniq -c\n</parameter>\n"
20        "<parameter=tooltitle>\nCount\n</parameter>\n</function>\n")
21ARGS = {"command": "cd proj && rg -o 'x' landing.html | sort | uniq -c", "tooltitle": "Count"}
22
23
24def run(text, stream_tools=False, recover=True, thinking=False, step=3, tools=(BASH,)):
25    p = OutputParser(thinking=thinking, tools=list(tools), stream_tools=stream_tools, recover=recover)
26    evs = []
27    for i in range(0, len(text), step):
28        evs += p.feed(text[i:i + step])
29    evs += p.finish("stop")
30    return evs
31
32
33def calls(evs):
34    return [(e.call.name, e.call.arguments) for e in evs if e.kind == "tool_call"]
35
36
37def text(evs):
38    return "".join(e.text or "" for e in evs if e.kind in ("content", "reasoning"))
39
40
41class Misspelt(unittest.TestCase):
42    def test_issue_block(self):
43        for opener in ("<toolcall>", "<tool-call>"):
44            for stream in (False, True):
45                for step in (1, 3, 1000):
46                    for recover in (False, True):
47                        evs = run(opener + "\n" + BODY + "</tool_call>", stream, recover, step=step)
48                        self.assertEqual(calls(evs), [("bash", ARGS)], (opener, stream, step, recover))
49                        self.assertEqual(text(evs).strip(), "")
50
51    def test_without_the_closing_tag_as_the_real_opener(self):
52        self.assertEqual(calls(run("<toolcall>\n" + BODY)), calls(run("<tool_call>\n" + BODY)))
53
54    def test_mid_line_as_the_real_opener(self):
55        t = "ok <toolcall>\n" + BODY + "</tool_call>"
56        self.assertEqual(calls(run(t)), calls(run(t.replace("<toolcall>", "<tool_call>"))))
57        self.assertEqual(calls(run(t)), [("bash", ARGS)])
58
59    def test_in_the_reasoning_mid_line_stays_text(self):
60        evs = run("hm <toolcall>\n" + BODY + "</tool_call>\n</think>\n", thinking=True)
61        self.assertEqual(calls(evs), [])
62
63    def test_text_before_is_kept(self):
64        evs = run("I will count.\n<toolcall>\n" + BODY + "</tool_call>")
65        self.assertEqual(calls(evs), [("bash", ARGS)])
66        self.assertEqual(text(evs).strip(), "I will count.")
67
68    def test_in_the_reasoning(self):
69        evs = run("hm\n<toolcall>\n" + BODY + "</tool_call>\n</think>\n", thinking=True)
70        self.assertEqual(calls(evs), [("bash", ARGS)])
71
72    def test_prose_naming_the_tag_stays_text(self):
73        for t in ("The model may write <toolcall> by mistake.",
74                  "Write <toolcall> and then a call.\n",
75                  "<toolcall>\nnot a call\n</tool_call>",
76                  "<toolcall>",
77                  "<toolcall>\n<function=nothing>\n<parameter=a>\n1\n</parameter>\n</function>\n</tool_call>"):
78            for stream in (False, True):
79                evs = run(t, stream)
80                self.assertEqual(calls(evs), [], t)
81                self.assertEqual(text(evs), t)
82
83    def test_code_fence_stays_text(self):
84        t = "Example:\n```\n<toolcall>\n" + BODY + "</tool_call>\n```\n"
85        evs = run(t)
86        self.assertEqual(calls(evs), [])
87        self.assertEqual(text(evs), t)
88
89    def test_indented_code_stays_text(self):
90        t = "Example:\n\n    <toolcall>\n    " + BODY.replace("\n", "\n    ") + "</tool_call>\n"
91        self.assertEqual(calls(run(t)), [])
92
93    def test_no_tools_declared(self):
94        t = "<toolcall>\n" + BODY + "</tool_call>"
95        self.assertEqual(calls(run(t, tools=())), [])
96        self.assertEqual(text(run(t, tools=())), t)
97
98    def test_the_real_opener_is_unchanged(self):
99        evs = run("<tool_call>\n" + BODY + "</tool_call>")
100        self.assertEqual(calls(evs), [("bash", ARGS)])
101
102
103if __name__ == "__main__":
104    unittest.main()