serve: skip tool-body rescans until a closing tag arrives
This commit is contained in:
1 parent
b145a4a2d6
commit
fc27296dee
2 files changed
+77
-1
No files matched your search
+16
-1
@@ -1039,6 +1039,7 @@ class OutputParser:
|
||||
return out
|
||||
|
||||
def _reset_scan(self):
|
||||
self._call_end_checked = 0 # closing-tag search progress for the current wrapped call
|
||||
self.sp = 0 # how much of self.buf (the call body) the scanner has consumed
|
||||
self.ss = "name" # name -> between -> str|raw -> ... -> done
|
||||
self.scall = None # the ToolCall being streamed (its id is reused by the final event)
|
||||
@@ -1046,6 +1047,20 @@ class OutputParser:
|
||||
self.sval_started = False
|
||||
self.sdeclared = {}
|
||||
|
||||
def _wrapped_call_end(self) -> int:
|
||||
# Without a closing tag the structural walk cannot finish. Search only
|
||||
# newly appended text plus enough overlap for a tag split across feeds.
|
||||
# A literal closing tag inside a value needs structural disambiguation:
|
||||
# once any candidate appears, keep the original full walk for this call.
|
||||
# Recovery can rewrite the buffer, so it always uses the full walk.
|
||||
if not self.recover and self._call_end_checked >= 0:
|
||||
start = max(0, self._call_end_checked - len(CALL_END) + 1)
|
||||
if self.buf.find(CALL_END, start) < 0:
|
||||
self._call_end_checked = len(self.buf)
|
||||
return -1
|
||||
self._call_end_checked = -1
|
||||
return call_end(self.buf)
|
||||
|
||||
def _scan(self) -> list[Event]:
|
||||
"""Advance the streaming view of the call body in self.buf (see stream_tools)."""
|
||||
out = []
|
||||
@@ -1360,7 +1375,7 @@ class OutputParser:
|
||||
elif after and not CALL_END.startswith(after):
|
||||
drop = 0
|
||||
else:
|
||||
i = call_end(self.buf)
|
||||
i = self._wrapped_call_end()
|
||||
if i >= 0:
|
||||
drop = len(CALL_END)
|
||||
if self.recover: # a batch in one wrapper: the next call follows this one's
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
"""Wrapped-call precheck parity against the original structural walk."""
|
||||
import itertools
|
||||
import random
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
from serve import frontend
|
||||
|
||||
|
||||
class ReferenceParser(frontend.OutputParser):
|
||||
def _wrapped_call_end(self):
|
||||
return frontend.call_end(self.buf)
|
||||
|
||||
|
||||
def events_without_ids(events):
|
||||
return [(e.kind, e.text, (e.call.name, e.call.arguments) if e.call else None) for e in events]
|
||||
|
||||
|
||||
class CallScanTests(unittest.TestCase):
|
||||
def test_chunk_boundaries_literal_tags_multiple_calls_and_recovery(self):
|
||||
def call(value):
|
||||
return "<tool_call><function=write><parameter=text>" + value + "</parameter></function></tool_call>"
|
||||
bodies = [call("ordinary"), call("literal </tool_call> text </function> here"),
|
||||
call("literal </parameter> prose"), call("a") + call("b"),
|
||||
call("unfinished")[:-5], call("incomplete </tool_call> prose")[:-30],
|
||||
"<tool_call><function=write></function></tool_call>",
|
||||
"<function=write><parameter=text>bare</parameter></function>",
|
||||
"<tool_call><function=write><parameter=text>a</parameter></function>"
|
||||
"<parameter=write><parameter=text>b</parameter></function></tool_call>"]
|
||||
tools = [{"name": "write", "parameters": {"properties": {"text": {"type": "string"}}}}]
|
||||
rng = random.Random(1030)
|
||||
for text, stream, recover in itertools.product(bodies, (False, True), (False, True)):
|
||||
# Every fixed chunk width, plus random partitions; compare after EACH feed.
|
||||
for width in range(1, 16):
|
||||
a = frontend.OutputParser(thinking=False, tools=tools, stream_tools=stream, recover=recover)
|
||||
b = ReferenceParser(thinking=False, tools=tools, stream_tools=stream, recover=recover)
|
||||
pos = 0
|
||||
while pos < len(text):
|
||||
end = pos + (rng.randint(1, 20) if width == 15 else width)
|
||||
delta = text[pos:end]
|
||||
self.assertEqual(events_without_ids(a.feed(delta)), events_without_ids(b.feed(delta)))
|
||||
pos = end
|
||||
self.assertEqual(events_without_ids(a.finish()), events_without_ids(b.finish()))
|
||||
|
||||
def test_no_structural_rescans_until_closer_and_reset_for_next_call(self):
|
||||
parser = frontend.OutputParser(thinking=False)
|
||||
with patch.object(frontend, "call_end", wraps=frontend.call_end) as walk:
|
||||
for _ in range(2):
|
||||
parser.feed("<tool_call><function=write><parameter=text>")
|
||||
before = walk.call_count
|
||||
for _ in range(100):
|
||||
parser.feed("abcdefgh")
|
||||
self.assertEqual(walk.call_count, before)
|
||||
for char in "</parameter></function></tool_call>":
|
||||
parser.feed(char)
|
||||
self.assertEqual(walk.call_count, before + 1)
|
||||
self.assertEqual(parser._call_end_checked, 0)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user