okami.mom/strata

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

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

main
3.1 KiB62 linesraw
1"""Wrapped-call precheck parity against the original structural walk."""
2import itertools
3import random
4import unittest
5from unittest.mock import patch
6
7from serve import frontend
8
9
10class ReferenceParser(frontend.OutputParser):
11    def _wrapped_call_end(self):
12        return frontend.call_end(self.buf, self._final)
13
14
15def events_without_ids(events):
16    return [(e.kind, e.text, (e.call.name, e.call.arguments) if e.call else None) for e in events]
17
18
19class CallScanTests(unittest.TestCase):
20    def test_chunk_boundaries_literal_tags_multiple_calls_and_recovery(self):
21        def call(value):
22            return "<tool_call><function=write><parameter=text>" + value + "</parameter></function></tool_call>"
23        bodies = [call("ordinary"), call("literal </tool_call> text </function> here"),
24                  call("literal </parameter> prose"), call("a") + call("b"),
25                  call("unfinished")[:-5], call("incomplete </tool_call> prose")[:-30],
26                  "<tool_call><function=write></function></tool_call>",
27                  "<function=write><parameter=text>bare</parameter></function>",
28                  "<tool_call><function=write><parameter=text>a</parameter></function>"
29                  "<parameter=write><parameter=text>b</parameter></function></tool_call>"]
30        tools = [{"name": "write", "parameters": {"properties": {"text": {"type": "string"}}}}]
31        rng = random.Random(1030)
32        for text, stream, recover in itertools.product(bodies, (False, True), (False, True)):
33            # Every fixed chunk width, plus random partitions; compare after EACH feed.
34            for width in range(1, 16):
35                a = frontend.OutputParser(thinking=False, tools=tools, stream_tools=stream, recover=recover)
36                b = ReferenceParser(thinking=False, tools=tools, stream_tools=stream, recover=recover)
37                pos = 0
38                while pos < len(text):
39                    end = pos + (rng.randint(1, 20) if width == 15 else width)
40                    delta = text[pos:end]
41                    self.assertEqual(events_without_ids(a.feed(delta)), events_without_ids(b.feed(delta)))
42                    pos = end
43                self.assertEqual(events_without_ids(a.finish()), events_without_ids(b.finish()))
44
45    def test_no_structural_rescans_until_closer_and_reset_for_next_call(self):
46        parser = frontend.OutputParser(thinking=False)
47        with patch.object(frontend, "call_end", wraps=frontend.call_end) as walk:
48            for _ in range(2):
49                parser.feed("<tool_call><function=write><parameter=text>")
50                before = walk.call_count
51                for _ in range(100):
52                    parser.feed("abcdefgh")
53                self.assertEqual(walk.call_count, before)
54                for char in "</parameter></function></tool_call>":
55                    parser.feed(char)
56                self.assertEqual(walk.call_count, before + 1)
57                parser.finish()                    # #4701: a closed call waits for what might follow it; the end of the turn settles it
58                self.assertEqual(parser._call_end_checked, 0)
59
60
61if __name__ == "__main__":
62    unittest.main()