Skip to content

Qwen3.8-Flash-Next on any consumer hardware: one-click install for Windows / Linux. Strata inference engine, OpenAI/Anthropic API on localhost, optional image input.

C++

serve: a string argument that quotes a tool call or reopens its parameter stays exact (#4078)

Tsury77794e34003d
diff --git a/serve/frontend.py b/serve/frontend.py
index 63e319b82..1687983fd 100644
--- a/serve/frontend.py
+++ b/serve/frontend.py
@@ -748,17 +748,26 @@ _LOOSE_OPEN = re.compile(r"<\s*(?:function\s*[=:]\s*|=\s*)?([A-Za-z0-9_.\-]+)\s*
_LOOSE_PART = re.compile(r"<\s*(?:function[ \t]*[=:]?\s*|=\s*)?[A-Za-z0-9_.\-]*\s*$")
-def param_end(text: str, final: bool = False) -> int:
- """Where a parameter value in `text` ends: the first `</parameter>` followed (after whitespace) by the next
- `<parameter=` or `</function>` - the same text inside a value (a file that documents the call format, #210) is
- part of the value. -1: none yet; -2: a candidate whose follower has not arrived (streaming; `final` accepts it)."""
+def param_end(text: str, final: bool = False, name: str | None = None) -> int:
+ """Where a parameter value in `text` ends: the first PARAM_END followed (after whitespace) by the next
+ PARAM_START or FUNC_END - the same text inside a value (a file that documents the call format, #210) is
+ part of the value. #4078: a close inside a call the value itself quotes (more FUNC_START than FUNC_END before
+ it), or one followed by the same parameter `name` again, is part of the value too. `text` starts at the value.
+ -1: none yet; -2: a candidate whose follower has not arrived (streaming; `final` accepts it)."""
at = text.find(PARAM_END)
while at >= 0:
- after = text[at + len(PARAM_END):].lstrip()
- if after.startswith(("<parameter=", FUNC_END)):
- return at
- if not after or "<parameter=".startswith(after) or FUNC_END.startswith(after):
- return at if final else -2
+ if text.count(FUNC_START, 0, at) <= text.count(FUNC_END, 0, at):
+ after = text[at + len(PARAM_END):].lstrip()
+ if after.startswith(PARAM_START):
+ gt = after.find(">")
+ if gt < 0:
+ return at if final else -2
+ if name is None or after[len(PARAM_START):gt].strip() != name:
+ return at
+ elif after.startswith(FUNC_END):
+ return at
+ elif not after or PARAM_START.startswith(after) or FUNC_END.startswith(after):
+ return at if final else -2
at = text.find(PARAM_END, at + 1)
return -1
@@ -783,7 +792,7 @@ def call_end(text: str) -> int:
gt = text.find(">", pos)
if gt < 0:
return -1
- end = param_end(text[gt + 1:])
+ end = param_end(text[gt + 1:], name=text[pos:gt].split("=", 1)[1].strip())
if end < 0:
return -1
pos = gt + 1 + end + len(PARAM_END)
@@ -865,7 +874,7 @@ def function_end(text: str) -> int:
gt = text.find(">", pos)
if gt < 0:
return -1
- end = param_end(text[gt + 1:])
+ end = param_end(text[gt + 1:], name=text[pos:gt].split("=", 1)[1].strip())
if end < 0:
return -1
pos = gt + 1 + end + len(PARAM_END)
@@ -918,7 +927,7 @@ def parse_tool_call(body: str, schema: dict | None = None) -> ToolCall:
rest = rest[rest.index("<parameter=") + len("<parameter="):]
pname = rest[:rest.index(">")]
rest = rest[rest.index(">") + 1:]
- end = param_end(rest, final=True)
+ end = param_end(rest, final=True, name=pname.strip())
value = rest[:end] if end >= 0 else rest
rest = rest[end + len(PARAM_END):] if end >= 0 else ""
if value.startswith("\n"):
@@ -1175,6 +1184,8 @@ class OutputParser:
self.sfirst = True
self.sval_started = False
self.sdeclared = {}
+ self.spname = None
+ self.svstart = 0
def _wrapped_call_end(self) -> int:
# Without a closing tag the structural walk cannot finish. Search only
@@ -1227,6 +1238,7 @@ class OutputParser:
args(("" if self.sfirst else ",") + json.dumps(pname) + ":")
self.sfirst = False
self.sp += b + 1
+ self.spname = pname.strip()
if self.sdeclared.get(pname) == "string":
args('"')
self.ss, self.sval_started = "str", False
@@ -1245,7 +1257,10 @@ class OutputParser:
self.sp += 1
rest = rest[1:]
self.sval_started = True
- end = param_end(rest)
+ self.svstart = self.sp
+ # the walk starts at the value (a quoted call's opener counts); only a close in the new text can end it
+ end = param_end(self.buf[self.svstart:], name=self.spname) if PARAM_END in rest else -1
+ end = end - (self.sp - self.svstart) if end >= 0 else end
if end >= 0:
value = rest[:end]
if value.endswith("\n"):
@@ -1263,7 +1278,7 @@ class OutputParser:
self.sp += safe
return out
elif self.ss == "raw":
- end = param_end(rest)
+ end = param_end(rest, name=self.spname)
if end < 0:
return out
value = rest[:end]
diff --git a/serve/test_quoted_call_values.py b/serve/test_quoted_call_values.py
new file mode 100644
index 000000000..7d5eac3ae
--- /dev/null
+++ b/serve/test_quoted_call_values.py
@@ -0,0 +1,55 @@
+"""#4078: a string argument that quotes a whole tool call, or reopens its own parameter, stays exact.
+
+ python -m unittest serve.test_quoted_call_values -v
+"""
+import json
+import unittest
+
+from serve.frontend import (CALL_END, CALL_START, FUNC_END, FUNC_START, PARAM_END, PARAM_START, OutputParser,
+ parse_tool_call)
+
+SCHEMA = {"name": "write", "parameters": {"properties": {"path": {"type": "string"}, "content": {"type": "string"}}}}
+QUOTED = (f"{CALL_START}\n{FUNC_START}write>\n{PARAM_START}path>\nx.txt\n{PARAM_END}\n{FUNC_END}\n{CALL_END}")
+VALUES = {
+ "quoted call": f"example transcript:\n{QUOTED}\nafter block",
+ "quoted call at the end": f"example transcript:\n{QUOTED}",
+ "heredoc": f"python3 - <<'PY'\nprint('''{QUOTED}''')\nPY",
+ "repeated parameter": f"head part\n{PARAM_END}\n{PARAM_START}content>\ntail part",
+}
+
+
+def call_text(value):
+ return (f"{CALL_START}\n{FUNC_START}write>\n{PARAM_START}path>\nprobe.txt\n{PARAM_END}\n"
+ f"{PARAM_START}content>\n{value}\n{PARAM_END}\n{FUNC_END}\n{CALL_END}")
+
+
+class QuotedCallValues(unittest.TestCase):
+ def test_values_are_exact_whole_streamed_and_chunked(self):
+ for label, value in VALUES.items():
+ text = call_text(value)
+ expected = {"path": "probe.txt", "content": value}
+ self.assertEqual(parse_tool_call(text[len(CALL_START):-len(CALL_END)], SCHEMA).arguments, expected)
+ for stream in (False, True):
+ for size in (1, 7, len(text)):
+ with self.subTest(label=label, stream=stream, size=size):
+ parser = OutputParser(thinking=False, tools=[SCHEMA], stream_tools=stream)
+ events = []
+ for i in range(0, len(text), size):
+ events.extend(parser.feed(text[i:i + size]))
+ events.extend(parser.finish())
+ calls = [e.call for e in events if e.kind == "tool_call"]
+ self.assertEqual([c.arguments for c in calls], [expected])
+ self.assertEqual("".join(e.text for e in events if e.kind == "content").strip(), "")
+ if stream:
+ self.assertEqual(json.loads("".join(e.text for e in events if e.kind == "tool_args")),
+ expected)
+
+ def test_prose_between_two_calls_still_ends_the_first(self):
+ first, second = call_text("one"), call_text("two")
+ parser = OutputParser(thinking=False, tools=[SCHEMA], stream_tools=True)
+ events = parser.feed(first + "\nnow the second:\n" + second) + parser.finish()
+ self.assertEqual([e.call.arguments["content"] for e in events if e.kind == "tool_call"], ["one", "two"])
+
+
+if __name__ == "__main__":
+ unittest.main()