Documented tree replacement (the history has no git-subtree metadata): 3-way merge of the old subtree (570201a:engine/strata) against upstream 41cf05a06a25dd29103b7b27a2279a3f60c21876 (2026-10-25), merge base = the old pin d9ab8435 (tree-identical to upstream 5b734e42; d9ab8435 is not an ancestor of upstream main). Local patches re-applied by hand where they conflicted (15 files): STRATA_CUDA_STATIC profile, experimental KAR experts and KAR PLE arms, keyless V-only arm (now beside upstream's K8V4 hybrid KV and fused norm+rope). Details, conflict table and left-out files are in docs/upstream.md. Left out (weights, captures, logs, media): new bench/results (2022 files), binary media and app assets (61), docs/work/evidence (24), *.bin / *.gguf data files, and the 7 files the first import already left out. scripts/build-cpu.sh: build the 11 CPU tests upstream registered since the old pin (they ran as "Not Run"). Checked: build-cpu.sh 16/16 ctest in WSL; sm_86 CUDA objects of the patched files compile, also with the KAR macros on. No GPU run, no quality gate. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
95 lines
4.6 KiB
Python
95 lines
4.6 KiB
Python
"""A missing wrapper closer ends before a following call, but prose alone does not confirm a call.
|
|
|
|
A call's own </function> 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 = "<function=read>\n<parameter=path>\na.py\n</parameter>\n</function>"
|
|
WRITE = "<function=write>\n<parameter=path>\nb.py\n</parameter>\n<parameter=content>\nx = 1\n</parameter>\n</function>"
|
|
|
|
|
|
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("<tool_call>\n" + READ + "\n</tool_call>\n<tool_call>\n" + WRITE + "\n</tool_call>",
|
|
[("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("<tool_call>\n" + READ + "\n<tool_call>\n" + WRITE + "\n</tool_call>",
|
|
[("read", {"path": "a.py"}), ("write", {"path": "b.py", "content": "x = 1"})])
|
|
|
|
def test_quoted_incomplete_wrapper_stays_literal(self):
|
|
text = "Format: <tool_call>\n" + READ + "\nand then I explain it."
|
|
self.check(text, [], text)
|
|
|
|
def test_following_literal_marker_does_not_complete_an_unclosed_call(self):
|
|
text = "<tool_call>\n" + READ + "\n<tool_call> 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 = "<tool_call>\n" + READ + ("\n</tool_call>" 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 </function> and a <tool_call> in a string"
|
|
call = ("<tool_call>\n<function=write>\n<parameter=path>\nc.py\n</parameter>\n<parameter=content>\n" + value +
|
|
"\n</parameter>\n</function>\n</tool_call>")
|
|
self.check(call, [("write", {"path": "c.py", "content": value})])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|