Skip to content

Preserve closing tool tags inside JSON strings - #1771

Open
VIOLETee0 wants to merge 1 commit into
CodebuffAI:mainfrom
VIOLETee0:fix/xml-tags-in-json-strings
Open

VIOLETee0 wants to merge 1 commit into
CodebuffAI:mainfrom
VIOLETee0:fix/xml-tags-in-json-strings

Conversation

@VIOLETee0

Copy link
Copy Markdown

Problem / Why

The streaming XML tool parser treats every </codebuff_tool_call> as a frame boundary, including one inside a valid JSON string. A tool argument such as "content": "</codebuff_tool_call>" therefore ends the frame early: JSON parsing fails, the tool call disappears, and the remaining arguments become ordinary text.

This affects the text-to-tool path used by processStreamWithTools. A local dispatcher fixture reproduces a missing file write from otherwise complete arguments.

Changes

Track JSON string and backslash-escape state across chunks. Recognize the closing tag only outside a string, while keeping the existing body buffer and partial-tag handling.

Strengthen one existing parser test with a complete closing tag, escaped quotes, and a trailing backslash. It retains the previous partial-tag input and checks both the tool arguments and filtered text for chunk sizes 1–24.

Verification

  • The streaming parser, batch parser, tool-stream, and duplicate-text suites: main with the changed case has 46 passed / 1 failed; the patch has 47 passed / 0 failed, 428 assertions.
  • A local processStreamWithTools probe using three-character chunks invokes its injected execution callback once and writes the complete literal-tag content. Main invokes it zero times. The unchanged newline-framed batch parser accepts the same arguments on both versions.
  • Agent-runtime and SDK typechecks pass with native TypeScript 7.0.2. Frozen-lockfile install passes.
  • ESLint: 0 errors, 4 local resolver warnings. Whole-file Prettier checks still report existing differences; comparing with main finds no new formatting differences.
  • SDK and binary builds remain blocked by the existing declaration-generator dependency: its locked native TypeScript 7.0.2 has no ts.sys. No build or binary-smoke pass is claimed.

Notes / Scope

Two agent-runtime files, including two fields on the internal parser state. No dependency or XML wire-format changes.

An unterminated JSON string leaves the frame open; this patch does not guess a boundary or repair malformed arguments. Native tool-call handling and the newline-framed batch parser are unchanged.

Validation ran on Windows with Bun 1.3.14 and local fixtures. Live models, other operating systems, the full tool authorization chain, and the full monorepo suite were not run.

@codebuff-team

Copy link
Copy Markdown
Contributor

Good catch and a clean, minimal fix. The root cause is accurately diagnosed: parseStreamChunk's text.indexOf(endToolTag) in stream-xml-parser.ts matches </codebuff_tool_call> even when it appears inside a JSON string value, truncating the frame and corrupting the tool call. The new findToolCallEnd helper walks the chunk character-by-character, tracking inJsonString/jsonEscapeNext on the persistent parser state so the scan correctly carries over string/escape context across chunk boundaries (verified by the trailing-backslash case in the updated test). Outside of a JSON string the behavior is unchanged (text.startsWith(endToolTag, i) mirrors the old indexOf semantics), so this shouldn't regress existing callers.

The test update is solid - it keeps the original partial-tag coverage, adds an embedded closing tag plus escaped quote and trailing backslash, and checks both toolCalls and surrounding text across chunk sizes 1-24, which is exactly the kind of boundary-sensitive coverage this parser needs.

A couple of things worth double-checking before landing: (1) the same false-positive risk likely exists for the opening <codebuff_tool_call> tag detection when it appears inside preceding literal text that happens to contain quotes - out of scope here, but worth a follow-up issue; (2) no handling for malformed/unterminated JSON strings is claimed or needed, which is reasonable since the PR explicitly scopes that out.

Small, self-contained change confined to packages/agent-runtime, with a clear repro and before/after test counts. This looks straightforward to port.

@codebuff-team codebuff-team added bot:triaged Classified by the community triage bot pr:port-candidate Worth porting into the private source tree labels Dec 1, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:triaged Classified by the community triage bot pr:port-candidate Worth porting into the private source tree

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants