Skip to content

Commit 4b12aba

Browse files
author
Bertan Ari
committed
fix: track inline code span width when detecting quoted wrapper tags
Backtick parity treated an even-width code span as two toggles, so a quoted <function_calls> example armed wrapped-only recovery and a later bare invoke was replayed as a live tool call. Both quoting checks now share one CommonMark-correct helper that closes a span only on an equal-width backtick run.
1 parent 0f6cc3c commit 4b12aba

2 files changed

Lines changed: 51 additions & 3 deletions

File tree

‎src/api/providers/__tests__/vscode-lm.spec.ts‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1720,6 +1720,40 @@ describe("leaked tool-call parser contracts", () => {
17201720
expect(callsOf(text)).toHaveLength(0)
17211721
})
17221722

1723+
it("does not let a wrapper opener inside a double-backtick span arm a later bare invoke", () => {
1724+
const text = [`Example: \`\`<function${"_calls"}>\`\``, "", todo()].join("\n")
1725+
const { calls, leftoverText } = extractLeakedToolCalls(text, tools)
1726+
1727+
expect(calls).toHaveLength(0)
1728+
expect(leftoverText).toBe(text)
1729+
})
1730+
1731+
it("keeps a double-backtick span quoted when it nests a literal single backtick", () => {
1732+
const text = [`Example: \`\`<function${"_calls"}> \` here\`\``, "", todo()].join("\n")
1733+
1734+
expect(callsOf(text)).toHaveLength(0)
1735+
})
1736+
1737+
it("does not let a wrapper opener inside a four-backtick span arm a later bare invoke", () => {
1738+
const text = [`Example: \`\`\`\`<function${"_calls"}>\`\`\`\``, "", todo()].join("\n")
1739+
1740+
expect(callsOf(text)).toHaveLength(0)
1741+
})
1742+
1743+
it("does not close a double-backtick span with a wider backtick run", () => {
1744+
const text = [`Example: \`\`<function${"_calls"}>\`\`\` still quoted`, "", todo()].join("\n")
1745+
1746+
expect(callsOf(text)).toHaveLength(0)
1747+
})
1748+
1749+
it("still arms on a wrapper opener that is not inside any backtick span", () => {
1750+
expect(callsOf(wrap(todo()))).toHaveLength(1)
1751+
})
1752+
1753+
it("does not leak an unterminated double-backtick span across a newline", () => {
1754+
expect(callsOf(wrapLines("see ``\n" + todo()))).toHaveLength(1)
1755+
})
1756+
17231757
it("still arms on a real wrapper opener that follows a closed code fence", () => {
17241758
const text = ["```", "example", "```", "", `<function${"_calls"}>`, todo()].join("\n")
17251759

‎src/api/providers/vscode-lm.ts‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ class QuotingScanState {
114114
// boundary, which always falls on the `<` of an `<invoke>` marker.
115115
for (const wrapperTag of line.matchAll(/<(\/?)(?:antml:)?function_calls\s*>/gi)) {
116116
const textBeforeTag = this.sameLineBefore.slice(0, lineStart + wrapperTag.index)
117-
if ((textBeforeTag.match(/`/g)?.length ?? 0) % 2 === 0) {
117+
if (!this.insideInlineSpanAt(textBeforeTag)) {
118118
this.wrapperOpen = wrapperTag[1] === ""
119119
}
120120
}
@@ -131,9 +131,23 @@ class QuotingScanState {
131131
return this.fenceAfterCurrentLine() !== null
132132
}
133133

134-
/** An odd count means the stream ends inside an inline code span. */
134+
/** True when the stream so far ends inside an inline code span. */
135135
hasOddBacktickCountOnLine(): boolean {
136-
return (this.sameLineBefore.match(/`/g)?.length ?? 0) % 2 === 1
136+
return this.insideInlineSpanAt(this.sameLineBefore)
137+
}
138+
139+
/** CommonMark: a code span closes only on a backtick run of the SAME width that opened it. */
140+
private insideInlineSpanAt(prefix: string): boolean {
141+
let activeWidth = 0
142+
for (const run of prefix.matchAll(/`+/g)) {
143+
const width = run[0].length
144+
if (activeWidth === 0) {
145+
activeWidth = width
146+
} else if (width === activeWidth) {
147+
activeWidth = 0
148+
}
149+
}
150+
return activeWidth !== 0
137151
}
138152

139153
isInsideFunctionCallsWrapper(): boolean {

0 commit comments

Comments
 (0)