"""A missing wrapper closer ends before a following call, but prose alone does not confirm a call.
A call's own separates it from the next wrapper without merging their arguments.
Parameter values may still contain call-format tags; recovery batches stay opt-in.
python -m unittest serve.test_tool_call_unclosed -v
"""
import json
import sys
import unittest
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
from serve.frontend import OutputParser # noqa: E402
TOOLS = [{"name": "read", "parameters": {"properties": {"path": {"type": "string"}}}},
{"name": "write", "parameters": {"properties": {"path": {"type": "string"}, "content": {"type": "string"}}}}]
READ = "\n\na.py\n\n"
WRITE = "\n\nb.py\n\n\nx = 1\n\n"
def run(text, stream_tools, recover, step):
p = OutputParser(thinking=False, tools=TOOLS, stream_tools=stream_tools, recover=recover)
evs = []
for i in range(0, len(text), step): # fed in small pieces, as a stream arrives
evs += p.feed(text[i:i + step])
evs += p.finish("stop")
calls = [(e.call.name, e.call.arguments) for e in evs if e.kind == "tool_call"]
if stream_tools and not calls:
arguments = "".join(e.text for e in evs if e.kind == "tool_args")
if arguments:
try:
json.loads(arguments)
except ValueError:
pass
else:
raise AssertionError("an unconfirmed call has complete streamed arguments")
return calls, "".join(e.text for e in evs if e.kind == "content")
class UnclosedCall(unittest.TestCase):
def check(self, text, want_calls, want_content=""):
for stream_tools in (False, True):
for recover in (False, True):
for step in (1, 3, 7, 4096):
with self.subTest(stream_tools=stream_tools, recover=recover, step=step):
calls, content = run(text, stream_tools, recover, step)
self.assertEqual(calls, want_calls)
self.assertEqual(content.strip(), want_content)
def test_closed_calls_are_unchanged(self):
self.check("\n" + READ + "\n\n\n" + WRITE + "\n",
[("read", {"path": "a.py"}), ("write", {"path": "b.py", "content": "x = 1"})])
def test_a_second_call_after_an_unclosed_one_is_its_own_call(self):
self.check("\n" + READ + "\n\n" + WRITE + "\n",
[("read", {"path": "a.py"}), ("write", {"path": "b.py", "content": "x = 1"})])
def test_quoted_incomplete_wrapper_stays_literal(self):
text = "Format: \n" + READ + "\nand then I explain it."
self.check(text, [], text)
def test_following_literal_marker_does_not_complete_an_unclosed_call(self):
text = "\n" + READ + "\n is a format marker."
self.check(text, [], text)
def test_omitted_wrapper_recovers_only_on_a_natural_stop(self):
for closed in (True, False):
text = "\n" + READ + ("\n" if closed else "")
for reason in (None, "stop", "length", "cancel", "disconnect"):
for step in (1, 7, 4096):
with self.subTest(closed=closed, reason=reason, step=step):
p = OutputParser(thinking=False, tools=TOOLS, stream_tools=True)
events = []
for offset in range(0, len(text), step):
events += p.feed(text[offset:offset + step])
events += p.finish(reason)
calls = [e for e in events if e.kind == "tool_call"]
complete = closed or reason in (None, "stop")
self.assertEqual(len(calls), int(complete))
if not complete:
with self.assertRaises(ValueError):
json.loads("".join(e.text for e in events if e.kind == "tool_args"))
def test_a_value_that_holds_the_tags_still_ends_at_the_calls_own_closer(self):
value = "a and a in a string"
call = ("\n\n\nc.py\n\n\n" + value +
"\n\n\n")
self.check(call, [("write", {"path": "c.py", "content": value})])
if __name__ == "__main__":
unittest.main()