okami.mom/strata

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

Niko1221Merge f-023-int into f-023-guifcb0c2d0e

main
6.0 KiB130 linesraw
1"""#3441 (a call whose envelope drifted by a token) and #3075 (a call for a tool the request never declared)."""
2from __future__ import annotations
3
4import sys
5import unittest
6from pathlib import Path
7
8sys.path.insert(0, str(Path(__file__).resolve().parent))
9import frontend  # noqa: E402
10
11TOOLS = [{"name": "functions.exec", "parameters": {"properties": {"input": {"type": "string"}}}},
12         {"name": "shell", "parameters": {"properties": {"cmd": {"type": "string"}}}},
13         {"name": "ns.web_search", "parameters": {"properties": {"q": {"type": "string"}}}}]
14
15
16def body(opener, param="input", value="1+1", name=None):
17    return f"<tool_call>\n{opener}\n<parameter={param}>\n{value}\n</parameter>\n</function>\n</tool_call>"
18
19
20def run(text, width=0, **kw):
21    """-> (events, parser); `width` 0 feeds the text whole, else that many characters at a time."""
22    p = frontend.OutputParser(thinking=False, stream_tools=True, **kw)
23    evs = []
24    if width:
25        for k in range(0, len(text), width):
26            evs += p.feed(text[k:k + width])
27    else:
28        evs += p.feed(text)
29    evs += p.finish()
30    return evs, p
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 == "content")
39
40
41class DriftedEnvelope(unittest.TestCase):
42    def test_a_dropped_function_keyword_is_still_the_call(self):
43        for opener in ("<=functions.exec>", "<function:functions.exec>", "<function =functions.exec>",
44                       "<functions.exec>", "<= functions.exec >"):
45            for width in (0, 1, 2, 3, 7):
46                with self.subTest(opener=opener, width=width):
47                    evs, p = run(body(opener), width, tools=TOOLS)
48                    self.assertEqual(calls(evs), [("functions.exec", {"input": "1+1"})])
49                    self.assertEqual(text(evs), "")
50                    self.assertEqual(p.unread, 0)
51
52    def test_the_template_form_is_unchanged(self):
53        evs, p = run(body("<function=functions.exec>"), 0, tools=TOOLS)
54        self.assertEqual(calls(evs), [("functions.exec", {"input": "1+1"})])
55
56    def test_an_undeclared_name_in_a_drifted_opener_stays_text(self):
57        for width in (0, 1, 4):
58            with self.subTest(width=width):
59                raw = body("<=missing>")
60                evs, p = run(raw, width, tools=TOOLS)
61                self.assertEqual(calls(evs), [])
62                self.assertEqual(text(evs), raw)
63                self.assertEqual(p.unread, 1)
64                self.assertFalse([e for e in evs if e.kind == "tool_start"])
65
66    def test_prose_about_the_tag_is_not_a_drift(self):
67        for raw in ("Use a <tool_call> block for that.", "<tool_call> then the name"):
68            evs, p = run(raw, 3, tools=TOOLS)
69            self.assertEqual(calls(evs), [])
70            self.assertEqual(text(evs), raw)
71            self.assertEqual(p.unread, 0)
72
73    def test_streamed_and_whole_agree(self):
74        raw = "Let me run it.\n" + body("<=shell>", "cmd", "ls") + "\nDone."
75        whole, _ = run(raw, 0, tools=TOOLS)
76        for width in (1, 2, 5, 11):
77            streamed, _ = run(raw, width, tools=TOOLS)
78            self.assertEqual(calls(streamed), calls(whole))
79            self.assertEqual(text(streamed), text(whole))
80        self.assertEqual(calls(whole), [("shell", {"cmd": "ls"})])
81        self.assertEqual(text(whole), "Let me run it.Done.")
82
83
84class OnlyDeclaredTools(unittest.TestCase):
85    def test_a_call_for_another_tool_stays_text(self):
86        raw = body("<function=get_indicators>", "symbol", "600076")
87        for width in (0, 1, 3, 9):
88            with self.subTest(width=width):
89                evs, p = run("I will check.\n" + raw, width, tools=TOOLS)
90                self.assertEqual(calls(evs), [])
91                self.assertFalse([e for e in evs if e.kind in ("tool_start", "tool_args")])
92                self.assertEqual(text(evs), "I will check.\n" + raw)
93
94    def test_an_unparsed_call_is_counted_and_named_for_the_log(self):
95        """T1 rail: a call that went back as text is no longer silent; the server prints its count and the names the model wrote."""
96        raw = body("<function=get_indicators>", "symbol", "600076")
97        evs, p = run("I will check."+chr(10) + raw, 4, tools=TOOLS)
98        self.assertEqual((p.unread, p.unread_names), (1, ["get_indicators"]))
99        evs, p = run(body("<function=shell>", "cmd", "ls"), 4, tools=TOOLS)         # a declared call is no unread call
100        self.assertEqual((p.unread, p.unread_names), (0, []))
101
102    def test_a_declared_call_after_an_undeclared_one_is_still_a_call(self):
103        raw = body("<function=get_indicators>") + "\n" + body("<function=shell>", "cmd", "ls")
104        evs, p = run(raw, 5, tools=TOOLS)
105        self.assertEqual(calls(evs), [("shell", {"cmd": "ls"})])
106        self.assertIn("get_indicators", text(evs))
107
108    def test_the_responses_spelling_of_a_namespace_tool_counts(self):
109        evs, p = run(body("<function=ns__web_search>", "q", "x"), 4, tools=TOOLS)
110        # PR #3556: the call carries the declared spelling (ns.web_search), the one the client offered, not the model's flat one
111        self.assertEqual(calls(evs), [("ns.web_search", {"q": "x"})])
112
113    def test_no_tools_declared(self):
114        raw = body("<function=search>", "q", "2+2")
115        evs, p = run(raw, 3, tools=None, declared_only=True)
116        self.assertEqual((calls(evs), text(evs)), ([], raw))
117        evs, p = run(raw, 3, tools=[], declared_only=True)
118        self.assertEqual((calls(evs), text(evs)), ([], raw))
119        # without the flag (the Responses route: a Codex compaction request keeps its tools in the prompt only)
120        evs, p = run(raw, 3, tools=None)
121        self.assertEqual(calls(evs), [("search", {"q": "2+2"})])
122
123    def test_the_reasoning_state_is_as_before(self):
124        p = frontend.OutputParser(thinking=True, tools=TOOLS, stream_tools=True)
125        evs = p.feed("think " + body("<function=shell>", "cmd", "ls") + "</think>\n\nok") + p.finish()
126        self.assertEqual(calls(evs), [])
127
128
129if __name__ == "__main__":
130    unittest.main()