[PR #5183] [Engine] tool parser: one stray backtick no longer hides the rest of the paragraph's calls (Fix #4923) #5335

Open
opened 2026-11-07 16:34:26 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Niko1221/Strata/pull/5183
Author: @XiaoBinGan
Created: 11/4/2026
Status: 🔄 Open

Base: main ← Head: reserve/fix-parser-stray-backtick


📝 Commits (1)

  • 0b5afe8 [Engine] tool parser: one stray backtick no longer hides the rest of the paragraph's calls (#4923)

📊 Changes

3 files changed (+90 additions, -5 deletions)

View changed files

📝 serve/frontend.py (+13 -4)
➕ serve/test_parser_stray_backtick.py (+72 -0)
📝 serve/test_parser_track.py (+5 -1)

📄 Description

Problem and result

The inline-code bookkeeping in serve/frontend.py's OutputParser counted backticks across the whole paragraph:
self.ticks += line.count("")`, reset only on a blank line. A model that wrote a tool name as inline code and never
closed it 鈥?

Let me use `run_commands to check.
<tool_call>
<function=run_commands>
...

鈥?left the count odd for the rest of the paragraph, and _opener_ok() / _in_code() then read every later
<tool_call> at the start of a line as quoted inline code. The call was kept as reasoning text, the block was closed
normally, and the client got a well-formed, complete reply with no tool_use block: an agentic client has nothing
to run and waits forever while the engine has already finished the turn. Silent, model-independent, and no
configuration avoids it (tool_call_recovery included).

After the change the call is delivered. From the reproduction in the issue, against main c62d5e3a and against this
branch (the exact script is the issue's "Smallest steps" block):

A paired               whole=True  streamed=True     # before and after
B stray                whole=False streamed=False    # before
B stray                whole=True  streamed=True     # after
C stray, earlier line  whole=False streamed=False    # before
C stray, earlier line  whole=True  streamed=True     # after

#4923 鈥?the report, with the parser-level reproduction and the note that the same paragraph-wide count is on main
(serve/frontend.py, _track/_opener_ok/_in_code). It is the output-side sibling of #1058 (a quoted call
delivered as real); #1058's guarantee has to keep holding, and does (see below).

Approach

One rule: an unmatched backtick followed by prose on its line ("Let me use run_commands to check."`) is a
stray marker, not the opening of a code span that crosses the newline. It is not carried into the next line:

self.ticks += line.count("`")
if self.ticks % 2 == 1:
    after = line[line.rfind("`") + 1:].strip()
    if after and not after.startswith("<"):
        self.ticks -= 1

The two shapes the existing controls keep as text are untouched:

  • a backtick at the end of its line ("Use " and the example on the next line 鈥?test_the_reporters_negative_
    controls' check("Use \n" + RM, 0), and test_quoted_calls_stay_text's "Use x, then \n" + RM): after` is
    empty there, so the span still crosses the newline;
  • a backtick immediately followed by a tag ("Use <tool_call> here."): after starts with <, so the span still
    opens and the quoted call stays text.

A call inside a one-line inline span ("The format is <tool_call>... and nothing else.") and inside a fence keeps
following the current-line/fence rules unchanged.

_opener_ok() and _in_code() keep their existing formulas; only what _track() records changed. The acknowledged
streaming/whole-consistency rule also holds: serve/test_parser_track.py's reference implementation and its
fragment/random-boundary property tests were updated with the same rule, so every streamed fragment produces the
same tracking state as before.

Validation

Passed: the new regression test, serve/test_parser_stray_backtick.py (4 tests, 7 subtests):

$ python -m pytest serve/test_parser_stray_backtick.py -q
4 passed, 7 subtests passed

It covers the issue's three inputs (paired inline code, one stray backtick, a stray on an earlier line) whole and
streamed in 1- and 7-character pieces and with recover=True, plus the two negative controls: a backtick at the end
of its line still opens the span (text, not a call), a one-line inline quote stays text, a fenced call stays text.

Passed: the A/B 鈥?with serve/frontend.py reverted to main, the same new test fails on the two stray-backtick
inputs:

$ python -m pytest serve/test_parser_stray_backtick.py -q       # frontend.py from main
2 failed, 4 passed, 7 subtests passed
SUBFAILED[stray backtick] ... AssertionError: False is not true : whole
SUBFAILED[stray backtick on an earlier line] ... AssertionError: False is not true : whole

Passed: the surrounding parser and tool-call suites (this checkout, Windows 11, Python 3.11.16):

$ python -m pytest serve/test_parser_stray_backtick.py serve/test_parser_track.py serve/test_reasoning_rescue.py \
    serve/test_tool_corpus.py serve/test_tool_call_unclosed.py serve/test_native_tool_parser.py \
    serve/test_quoted_call_values.py serve/test_parser_markdown_code.py -q
57 passed, 1117 subtests passed

serve/test_reasoning_rescue.py includes the #804/#1058 controls, the 37-specimen corpus (whole and in chunks) and
the three HTTP APIs; serve/test_tool_corpus.py holds the quoted-call corpus. All pass on this branch.

Failed: none.

Not run: a real model emitting the stray backtick inside a thinking block on served hardware 鈥?the parser-level
reproduction in the issue is what was reproduced, not an end-to-end GPU run. python -m pytest serve (the whole
directory) was not completed on this box: it does not finish here within several minutes, so the tool-call and
reasoning files above were run one by one. No engine build is touched: this is the Python server only.

Limits

  • The rule is a heuristic for a stray marker: a model that wants a code span to start mid-sentence and continue on
    the next line ("Use run_commands then the example" with the backtick closed on the same line) still
    counts; only a line ending with prose after an unmatched backtick is treated as stray. A span genuinely opened
    mid-sentence and closed on a later line is no longer treated as inline code 鈥?that shape is the one that was
    swallowing the calls, and no test in the tree asserts it.
  • A stray backtick followed by nothing but a tag on later lines still opens the span (the Use <tool_call>``
    controls), so a stray ` before a real call on the next line would still hide that call. That is the #1058 case and
    is unchanged on purpose.
  • This fixes the paragraph carry-over only; no other parser path was touched. In particular a quoted call still
    needs the existing fence / one-line inline-code / `Use `` shapes to stay text, as above.

Note on the base: this branch was rebuilt on ffa80d73 (0.2.10) after the release; serve/frontend.py keeps the 0.2.10 additions (_tool_references, _close_verdict) and only gains the stray-backtick rule, and the tests below were run on that rebuilt head.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Niko1221/Strata/pull/5183 **Author:** [@XiaoBinGan](https://github.com/XiaoBinGan) **Created:** 11/4/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `reserve/fix-parser-stray-backtick` --- ### 📝 Commits (1) - [`0b5afe8`](https://github.com/Niko1221/Strata/commit/0b5afe81559be8e3facc5d5b003a98194fb36359) [Engine] tool parser: one stray backtick no longer hides the rest of the paragraph's calls (#4923) ### 📊 Changes **3 files changed** (+90 additions, -5 deletions) <details> <summary>View changed files</summary> 📝 `serve/frontend.py` (+13 -4) ➕ `serve/test_parser_stray_backtick.py` (+72 -0) 📝 `serve/test_parser_track.py` (+5 -1) </details> ### 📄 Description ## Problem and result The inline-code bookkeeping in `serve/frontend.py`'s `OutputParser` counted backticks across the **whole paragraph**: `self.ticks += line.count("`")`, reset only on a blank line. A model that wrote a tool name as inline code and never closed it 鈥? ```text Let me use `run_commands to check. <tool_call> <function=run_commands> ... ``` 鈥?left the count odd for the rest of the paragraph, and `_opener_ok()` / `_in_code()` then read every later `<tool_call>` at the start of a line as quoted inline code. The call was kept as reasoning text, the block was closed normally, and the client got a well-formed, complete reply with **no `tool_use` block**: an agentic client has nothing to run and waits forever while the engine has already finished the turn. Silent, model-independent, and no configuration avoids it (`tool_call_recovery` included). After the change the call is delivered. From the reproduction in the issue, against `main` `c62d5e3a` and against this branch (the exact script is the issue's "Smallest steps" block): ```text A paired whole=True streamed=True # before and after B stray whole=False streamed=False # before B stray whole=True streamed=True # after C stray, earlier line whole=False streamed=False # before C stray, earlier line whole=True streamed=True # after ``` ## Related discussion #4923 鈥?the report, with the parser-level reproduction and the note that the same paragraph-wide count is on `main` (`serve/frontend.py`, `_track`/`_opener_ok`/`_in_code`). It is the output-side sibling of #1058 (a quoted call delivered as real); #1058's guarantee has to keep holding, and does (see below). ## Approach One rule: an **unmatched** backtick followed by prose on its line (`"Let me use `run_commands to check."`) is a stray marker, not the opening of a code span that crosses the newline. It is not carried into the next line: ```python self.ticks += line.count("`") if self.ticks % 2 == 1: after = line[line.rfind("`") + 1:].strip() if after and not after.startswith("<"): self.ticks -= 1 ``` The two shapes the existing controls keep as **text** are untouched: - a backtick at the end of its line (`"Use `"` and the example on the next line 鈥?`test_the_reporters_negative_ controls`' `check("Use `\n" + RM, 0)`, and `test_quoted_calls_stay_text`'s `"Use `x`, then `\n" + RM`): `after` is empty there, so the span still crosses the newline; - a backtick immediately followed by a tag (`"Use `<tool_call>` here."`): `after` starts with `<`, so the span still opens and the quoted call stays text. A call inside a one-line inline span (`"The format is `<tool_call>...` and nothing else."`) and inside a fence keeps following the current-line/fence rules unchanged. `_opener_ok()` and `_in_code()` keep their existing formulas; only what `_track()` records changed. The acknowledged streaming/whole-consistency rule also holds: `serve/test_parser_track.py`'s reference implementation and its fragment/random-boundary property tests were updated with the same rule, so every streamed fragment produces the same tracking state as before. ## Validation **Passed:** the new regression test, `serve/test_parser_stray_backtick.py` (4 tests, 7 subtests): ```text $ python -m pytest serve/test_parser_stray_backtick.py -q 4 passed, 7 subtests passed ``` It covers the issue's three inputs (paired inline code, one stray backtick, a stray on an earlier line) whole and streamed in 1- and 7-character pieces and with `recover=True`, plus the two negative controls: a backtick at the end of its line still opens the span (text, not a call), a one-line inline quote stays text, a fenced call stays text. **Passed:** the A/B 鈥?with `serve/frontend.py` reverted to `main`, the same new test fails on the two stray-backtick inputs: ```text $ python -m pytest serve/test_parser_stray_backtick.py -q # frontend.py from main 2 failed, 4 passed, 7 subtests passed SUBFAILED[stray backtick] ... AssertionError: False is not true : whole SUBFAILED[stray backtick on an earlier line] ... AssertionError: False is not true : whole ``` **Passed:** the surrounding parser and tool-call suites (this checkout, Windows 11, Python 3.11.16): ```text $ python -m pytest serve/test_parser_stray_backtick.py serve/test_parser_track.py serve/test_reasoning_rescue.py \ serve/test_tool_corpus.py serve/test_tool_call_unclosed.py serve/test_native_tool_parser.py \ serve/test_quoted_call_values.py serve/test_parser_markdown_code.py -q 57 passed, 1117 subtests passed ``` `serve/test_reasoning_rescue.py` includes the #804/#1058 controls, the 37-specimen corpus (whole and in chunks) and the three HTTP APIs; `serve/test_tool_corpus.py` holds the quoted-call corpus. All pass on this branch. **Failed:** none. **Not run:** a real model emitting the stray backtick inside a thinking block on served hardware 鈥?the parser-level reproduction in the issue is what was reproduced, not an end-to-end GPU run. `python -m pytest serve` (the whole directory) was not completed on this box: it does not finish here within several minutes, so the tool-call and reasoning files above were run one by one. No engine build is touched: this is the Python server only. ## Limits - The rule is a heuristic for a stray marker: a model that wants a code span to start mid-sentence and continue on the next line (`"Use `run_commands` then the example"` with the backtick closed on the **same** line) still counts; only a line ending with prose after an unmatched backtick is treated as stray. A span genuinely opened mid-sentence and closed on a later line is no longer treated as inline code 鈥?that shape is the one that was swallowing the calls, and no test in the tree asserts it. - A stray backtick followed by nothing but a tag on later lines still opens the span (the `Use `<tool_call>`` controls), so a stray ` before a real call on the next line would still hide that call. That is the #1058 case and is unchanged on purpose. - This fixes the paragraph carry-over only; no other parser path was touched. In particular a quoted call still needs the existing fence / one-line inline-code / `Use `` shapes to stay text, as above. **Note on the base:** this branch was rebuilt on `ffa80d73` (0.2.10) after the release; `serve/frontend.py` keeps the 0.2.10 additions (`_tool_references`, `_close_verdict`) and only gains the stray-backtick rule, and the tests below were run on that rebuilt head. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Niko1221/Strata#5335
No description provided.