okami.mom/strata

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

Niko1221Trial2 merge f-0291-pr-nv (97b3d614f)1306424be

main
4.9 KiB97 linesraw
1"""#4701 decision data: a file whose text lists </parameter>, </function>, </tool_call> on lines of their own, written through a real call.
2
3Option C (decided 10-30, phaseA-tests/urgent029/OPTIONS-4701-4704.md): the closing sequence ends the value unless prose follows it AND a later
4closing sequence follows that prose, with no new <tool_call> opener in between.  While the turn is still streaming a call that might go on is
5held, bounded by the end of the turn, a new <tool_call>, or CLOSE_LOOKAHEAD_CHARS of text.
6
7    python -m unittest serve.test_param_end_options
8"""
9import unittest
10
11from serve.frontend import OutputParser
12
13
14def tool(name, **props):
15    return {"name": name, "parameters": {"type": "object", "properties": props}}
16
17
18S = {"type": "string"}
19TOOLS = [tool("read", path=S), tool("write_file", path=S, content=S)]
20DOC = "# Tool calls\n\nThe closing tags, in order:\n\n</parameter>\n</function>\n</tool_call>\n\nNothing may follow them."
21READ = "<tool_call>\n<function=read>\n<parameter=path>\na.txt\n</parameter>\n</function>\n</tool_call>"
22WRITE = ("<tool_call>\n<function=write_file>\n<parameter=path>\nd.md\n</parameter>\n<parameter=content>\n" + DOC
23         + "\n</parameter>\n</function>\n</tool_call>")
24
25
26def parse(text):
27    p = OutputParser(thinking=False, tools=TOOLS, stream_tools=False, recover=False, declared_only=True)
28    evs = p.feed(text) + p.finish("stop")
29    return [(e.call.name, e.call.arguments) for e in evs if e.kind == "tool_call"], "".join(e.text for e in evs if e.kind == "content")
30
31
32class ParamEndOptions(unittest.TestCase):
33    def test_a_file_that_lists_the_closing_tags_keeps_all_its_text(self):
34        calls, answer = parse(WRITE)
35        self.assertEqual(calls, [("write_file", {"path": "d.md", "content": DOC})])
36        self.assertEqual(answer, "")
37
38    def test_a_call_followed_by_a_sentence_is_still_a_call(self):
39        calls, answer = parse(READ + "\n\nI will read that file now.")
40        self.assertEqual(calls, [("read", {"path": "a.txt"})])
41        self.assertIn("I will read", answer)
42
43    def test_two_calls_with_a_sentence_between_are_two_calls(self):
44        calls, _ = parse(READ + "\nOkay.\n" + READ)
45        self.assertEqual(len(calls), 2)
46
47
48def stream(text, piece, finish=True):
49    p = OutputParser(thinking=False, tools=TOOLS, stream_tools=False, recover=False, declared_only=True)
50    seen = []                      # (characters fed so far, calls delivered so far)
51    n = 0
52    for i in range(0, len(text), piece):
53        n += len(text[i:i + piece])
54        got = [e for e in p.feed(text[i:i + piece]) if e.kind == "tool_call"]
55        seen.append((n, len(got)))
56    evs = p.finish("stop") if finish else []
57    calls = [(e.call.name, e.call.arguments) for e in evs if e.kind == "tool_call"]
58    return seen, calls, "".join(e.text for e in evs if e.kind == "content")
59
60
61class Streaming(unittest.TestCase):
62    def test_the_file_comes_whole_however_it_is_cut_into_pieces(self):
63        for piece in (1, 3, 7, 50, len(WRITE)):
64            p = OutputParser(thinking=False, tools=TOOLS, stream_tools=False, recover=False, declared_only=True)
65            evs = []
66            for i in range(0, len(WRITE), piece):
67                evs += p.feed(WRITE[i:i + piece])
68            evs += p.finish("stop")
69            calls = [(e.call.name, e.call.arguments) for e in evs if e.kind == "tool_call"]
70            self.assertEqual(calls, [("write_file", {"path": "d.md", "content": DOC})], piece)
71            self.assertEqual("".join(e.text for e in evs if e.kind == "content"), "", piece)
72
73    def test_an_ordinary_call_is_delivered_when_the_turn_ends_and_not_before_a_second_call_is_needed(self):
74        for piece in (1, 7):
75            p = OutputParser(thinking=False, tools=TOOLS, stream_tools=False, recover=False, declared_only=True)
76            got = []
77            for i in range(0, len(READ), piece):
78                got += [e for e in p.feed(READ[i:i + piece]) if e.kind == "tool_call"]
79            got += [e for e in p.finish("stop") if e.kind == "tool_call"]
80            self.assertEqual(len(got), 1)
81
82    def test_a_second_call_releases_the_first_at_once(self):
83        p = OutputParser(thinking=False, tools=TOOLS, stream_tools=False, recover=False, declared_only=True)
84        first = [e for e in p.feed(READ + "\n<tool_call>") if e.kind == "tool_call"]
85        self.assertEqual(len(first), 1)
86
87    def test_a_long_sentence_after_a_call_cannot_hold_it_for_ever(self):
88        from serve.frontend import CLOSE_LOOKAHEAD_CHARS
89        p = OutputParser(thinking=False, tools=TOOLS, stream_tools=False, recover=False, declared_only=True)
90        got = [e for e in p.feed(READ + "\n\n" + "word " * 200) if e.kind == "tool_call"]
91        self.assertEqual(len(got), 0)                       # still inside the bound
92        got = [e for e in p.feed("more " * 200) if e.kind == "tool_call"]
93        self.assertEqual(len(got), 1)                       # past it: the call goes out while the model is still talking
94
95
96if __name__ == "__main__":
97    unittest.main()