trae 0.2.9: <toolcall> without the underscore is read as <tool_call>, and a reply that ends inside an opener is cut short, not the answer (#38)

XcodeFish's two leaks on 0.2.6, still on 0.2.8: glm-5.3-flash wrote
<toolcall></toolcall>, and a reply ended at "<tool_call name". Both went
out as the answer with a 200.

- The opener, close, leftovers and held marks take <toolcall> and
  </toolcall> too; an empty block is dropped as <tool_call></tool_call>
  is, one with JSON or parameters is a call, one that can't be read is
  the error it is for <tool_call>.
- At the end of a reply, an opener still open (<tool_call, <tool_call
  name) outside backticks or a fence ends the turn with the cut-short
  error.

Run: the issue's repro (A-G) on main and on this; the new test fails on
the old index.mjs (1 fail) and passes here; HOME=$(mktemp -d) bun test
for the repo, 1153 pass 0 fail; bun scripts/check.mjs green.
This commit is contained in:
yetone
2026-10-13 04:40:26 +08:00
parent 66facacf14
commit 0e2a52efdc
3 changed files with 50 additions and 13 deletions
+30 -12
View File
@@ -1067,6 +1067,15 @@ class TextTools {
this.after = true
}
if (end) {
// an opener the stream ended inside (<tool_call name): a call cut
// short, not the answer (plugins#38)
const h = this.buf.search(OPEN_HOLD)
if (h >= 0 && !quoted(this.sent + text + this.buf.slice(0, h))) {
text += this.buf.slice(0, h)
this.buf = ""
this.sent += text
return { text, calls, reasoning, error: CUT_SHORT }
}
text += this.buf
this.buf = ""
} else {
@@ -1088,11 +1097,11 @@ class TextTools {
// gave as <tool_call name="…">.
block(body, named, end) {
if (!end && !body.trim()) return { wait: true }
const close = body.indexOf(CLOSE)
const [close, closeLen] = closeAt(body)
if (named) {
if (close < 0 && !end) return { wait: true }
const region = close >= 0 ? body.slice(0, close) : body
const rest = close >= 0 ? body.slice(close + CLOSE.length) : ""
const rest = close >= 0 ? body.slice(close + closeLen) : ""
if (region.trimStart().startsWith("{")) {
const v = repairedJSON(region.trim())
if (!v) return { error: close >= 0 ? UNREADABLE : CUT_SHORT }
@@ -1116,7 +1125,7 @@ class TextTools {
const region = body.slice(ph[0].length, close >= 0 ? close : body.length)
const p = paramArgs(region, ph[1], this.tools)
if (close < 0 && !p.closed) return { error: CUT_SHORT }
return { call: { name: ph[1], arguments: JSON.stringify(p.args) }, rest: close >= 0 ? body.slice(close + CLOSE.length) : "" }
return { call: { name: ph[1], arguments: JSON.stringify(p.args) }, rest: close >= 0 ? body.slice(close + closeLen) : "" }
}
const o = objectEnd(body, 0)
if (o > 0 && !this.bad.has(":" + o)) {
@@ -1128,10 +1137,10 @@ class TextTools {
}
// not whole, or not JSON as it stands: read up to a close (a file's
// content may hold one), or at the end all of it
for (let j = close; j >= 0; j = body.indexOf(CLOSE, j + 1)) {
for (let [j, n] = [close, closeLen]; j >= 0; [j, n] = closeAt(body, j + 1)) {
if (this.bad.has("<" + j)) continue
const v = callJSON(body.slice(0, j))
if (v) return { call: { name: String(v.name), arguments: callArgs(v) }, rest: body.slice(j + CLOSE.length) }
if (v) return { call: { name: String(v.name), arguments: callArgs(v) }, rest: body.slice(j + n) }
this.bad.add("<" + j)
}
if (!end) return { wait: true }
@@ -1145,7 +1154,7 @@ class TextTools {
const region = close >= 0 ? body.slice(0, close) : body
const w = glmCall(region, this.tools)
// GLM's call, cut short at the end, is still the call
if (w) return { call: { name: w.name, arguments: callArgs(w) }, rest: close >= 0 ? body.slice(close + CLOSE.length) : "" }
if (w) return { call: { name: w.name, arguments: callArgs(w) }, rest: close >= 0 ? body.slice(close + closeLen) : "" }
if (/^\s*[\w.:-]{1,128}\s*<arg_key>/.test(body)) return { error: close >= 0 ? UNREADABLE : CUT_SHORT }
return { text: true }
}
@@ -1155,9 +1164,18 @@ const CUT_SHORT = "the model's tool call, written as text, was cut short; it is
const UNREADABLE = "the model's tool call, written as text, can't be read; it is not sent on as the answer"
// an opener: <tool_call>, one with attributes (<tool_call name="bash">),
// or <tool_call right before its JSON; and one still being written
const OPEN_AT = /<tool_call(?:(\s[^<>{}]*)?>|(?=\s*\{))/
const OPEN_HOLD = /<tool_call(?:\s[^<>{}\n]{0,200})?$/
// or <tool_call right before its JSON; and one still being written. GLM
// also writes it without the underscore, <toolcall> (plugins#38)
const OPEN_AT = /<tool_?call(?:(\s[^<>{}]*)?>|(?=\s*\{))/
const OPEN_HOLD = /<tool_?call(?:\s[^<>{}\n]{0,200})?$/
// closeAt is where the next close is from i, </tool_call> or </toolcall>,
// and its length: [-1, 0] when there is none
function closeAt(s, i = 0) {
const re = /<\/tool_?call>/g
re.lastIndex = i
const m = re.exec(s)
return m ? [m.index, m[0].length] : [-1, 0]
}
// DeepSeek's half-and-half call: {"name":"x"} (or its brace or > left
// out), then <parameter name="k">v</parameter> or Qwen's <parameter=k>
const PARAM_HEAD = /^\s*\{\s*"name"\s*:\s*"([^"]+)"\s*\}?\s*>?\s*(?=<parameter\b)/
@@ -1303,7 +1321,7 @@ const GLM_ARG = /<arg_key>\s*([\s\S]*?)\s*<\/arg_key>\s*<arg_value>([\s\S]*?)<\/
// a value that isn't a string. A name alone is a call only to a tool the
// request has.
function glmCall(s, tools = []) {
s = String(s ?? "").replace(/<\/tool_call>\s*$/, "")
s = String(s ?? "").replace(/<\/tool_?call>\s*$/, "")
const k = s.indexOf("<arg_key>")
const name = (k < 0 ? s : s.slice(0, k)).trim()
if (!/^[\w.:-]{1,128}$/.test(name)) return null
@@ -1373,9 +1391,9 @@ const DSML_CLOSE = new RegExp(String.raw`<\s*\/\s*[||][\s||]*DSML[\s||]*in
const DSML_PARAM = new RegExp(String.raw`<\s*[||][\s||]*DSML[\s||]*parameter\s+name\s*=\s*"([^"]+)"((?:\s+\w+\s*=\s*"[^"]*")*)\s*>([\s\S]*?)` + DSML + String.raw`parameter\s*>`, "g")
// what is left after a call: blanks, our close, DeepSeek's tags but the
// next call's
const LEFTOVER = new RegExp(String.raw`^(?:\s+|<\/tool_calls?>|<tool_calls>|` + DSML + String.raw`(?![\s||]*invoke\b)[^<>]*>)+`)
const LEFTOVER = new RegExp(String.raw`^(?:\s+|<\/tool_?calls?>|<tool_?calls>|` + DSML + String.raw`(?![\s||]*invoke\b)[^<>]*>)+`)
// what text may be the start of, so held back
const MARKS = [OPEN, CLOSE, "<|DSML|", "<|DSML|", "</|DSML|", "</|DSML|", '{"reasoning_content"']
const MARKS = [OPEN, CLOSE, "<toolcall>", "</toolcall>", "<|DSML|", "<|DSML|", "</|DSML|", "</|DSML|", '{"reasoning_content"']
// reasoning a model wrote into its text as JSON
const REASON_AT = /\{\s*"reasoning_content"\s*:/
const MARK_MAX = Math.max(...MARKS.map((m) => m.length))
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@magpie-community/opencode-trae-auth",
"version": "0.2.8",
"version": "0.2.9",
"description": "OpenCode provider plugins for Trae: Trae CN (trae.cn) and Trae Global (trae.ai), free tier included, with Trae's own browser sign-in",
"type": "module",
"main": "./index.mjs",
+19
View File
@@ -122,6 +122,25 @@ test("#38's shapes: escaped quotes and an attribute-framed opener are calls; a b
expect(bad.text).toBe("")
})
test("XcodeFish's two on 0.2.6 (#38): <toolcall> without the underscore is read as <tool_call> is, and a stream that ends inside an opener is cut short", () => {
for (const size of [0, 1, 4]) {
// glm-5.3-flash's empty block beside a real call: no call, and not text
expect(read("<toolcall></toolcall>", size)).toEqual({ text: "", calls: [], error: null })
expect(read('<toolcall>{"name":"bash","arguments":{"command":"ls"}}</toolcall>', size)).toEqual({ text: "", calls: [["bash", { command: "ls" }]], error: null })
expect(read('<toolcall name="bash">\n<parameter name="command">ls</parameter>\n</toolcall>', size)).toEqual({ text: "", calls: [["bash", { command: "ls" }]], error: null })
expect(read('<toolcall>{"name":"bash","arguments":{"command":}}</toolcall>', size).error).toContain("can't be read")
for (const cut of ["<tool_call name", "<tool_call", '<tool_call name="ba', "<toolcall name"]) {
const r = read("Running it.\n" + cut, size)
expect(r).toEqual({ text: "Running it.\n", calls: [], error: r.error })
expect(r.error).toContain("cut short")
}
}
// in backticks it is prose, as <tool_call> is
for (const s of ["Each call goes in a `<toolcall>` block.", "The tag is `<tool_call name", "Write `<toolcall>{\"name\":\"x\"}</toolcall>` to call."]) {
for (const size of [0, 1, 6]) expect(read(s, size)).toEqual({ text: s, calls: [], error: null })
}
})
test("prose that mentions <tool_call> stays text: in backticks, in a code fence, or a tag named in passing", () => {
for (const s of [
'Write `<tool_call>{"name":"bash","arguments":{}}</tool_call>` to call a tool.',