Skip to content

fix(llmcore): reconstruct pseudo-XML tool calls and never execute a truncated one - #873

Closed
leonsong09 wants to merge 3 commits into
lsdefine:mainfrom
leonsong09:fix/pseudo-xml-tool-calls
Closed

leonsong09 wants to merge 3 commits into
lsdefine:mainfrom
leonsong09:fix/pseudo-xml-tool-calls

Conversation

@leonsong09

@leonsong09 leonsong09 commented Nov 12, 2026 •

Copy link
Copy Markdown

Closes #863.

What this fixes

Some upstreams emit the tool call as plain content text instead of a native
tool_calls array. _parse_text_tool_calls only understood a trailing tool_use
JSON array plus <tool_use>/<tool_call> JSON wrappers, so every observed spelling
fell through to do_no_tool; no guard matched there, and the turn ended silently
with no error and nothing executed.

Reproduced with the eight shapes from #863 (plain content, zero native tool calls):

shape before after
A <code_run><script>… task ends executed
B <parameter name="script"> task ends executed
C <file_read"> (stray quote) task ends executed with the right arg name
D <type>-wrapped parameters task ends retry
E ||DSML||invoke retry (tail heuristic) executed
F <function=code_run><parameter=script> task ends executed
G </code_run><code_run></code_run> task ends retry
H cut mid-stream task ends / executes half a script retry, never executed

Where the cut-off guard lives

The guard is inside _parse_text_tool_calls, not in do_no_tool. A call that was
cut mid-stream must not become a real tool_call in the first place: once it is, the
normal response path runs it and do_no_tool is never reached. This matters beyond
syntax errors — a cut that happens to leave valid Python would otherwise run half a
script.

textual_tool_residue(content) classifies what could not be executed:

  • unclosed — the stream was cut mid-call
  • empty — block closes but carries no arguments
  • orphan — <parameter> with no tool name (<type>-wrapped)
  • invalid — block closes, but the rebuilt call cannot run safely (half a script,
    or a script that fails ast.parse)

do_no_tool retries on any of them.

Also

_retry_or_exit used to return StepOutcome({}, should_exit=True) with nothing
printed after three unusable replies — the same silent stop, one level up. It now
yields a visible [任务中止] note first.

Scope and risk

  • All eight shapes are reconstructed only when the block is closed, the tool name
    is in assets/tools_schema.json, and at least one argument is recoverable.
  • Fenced and inline code is masked out, so examples that merely quote the syntax are
    never executed.
  • No behaviour change for native tool_calls; the fallback only runs when
    tool_calls is empty.

Tests

pytest tests/test_text_tool_call.py -q → 8 passed

Covers A–H, the cut-but-valid-Python case, the closed-but-broken-script case, and the
negatives (prose, inline code, fenced block, JSON example).

GenericAgent added 3 commits November 11, 2026 17:11
…runcated one

Issue lsdefine#863: some upstreams emit the tool call as plain content text instead of a
native `tool_calls` array. `_parse_text_tool_calls` only understood a trailing
`tool_use` JSON array and `<tool_use>`/`<tool_call>` JSON wrappers, so every observed
spelling fell through to `do_no_tool`, which had no matching guard, and the turn ended
silently with no error.

llmcore
  * `_parse_pseudo_xml_tool_calls`: rebuild closed blocks into MockToolCall.
    Handles `<code_run><script>`, `<parameter name=|name=>`, a stray quote after the
    tool name, `<function=name>`, and the `||DSML||` shell. Names are validated
    against assets/tools_schema.json; fenced and inline code is masked out so quoted
    examples are never executed.
  * `textual_tool_residue`: 'unclosed' / 'empty' / 'orphan' / 'invalid' for anything
    that cannot be executed, so callers can retry instead of delivering it.
  * The safety gate lives inside `_parse_text_tool_calls`, not in `do_no_tool`: a
    truncated call must not become a real tool_call first. A cut that happens to leave
    valid Python is rejected too, and a closed block whose script fails to parse is
    reported as residue rather than silently dropped.

ga
  * `do_no_tool` retries on any residue, naming the bogus formats in the prompt so the
    model does not simply re-emit them.
  * `_retry_or_exit` now yields a visible abort note instead of returning
    `should_exit=True` with nothing printed - same "silent stop" failure mode.

tests/test_text_tool_call.py covers shapes A-H, the cut-but-valid-Python case, the
closed-but-broken-script case, and the negatives (prose, inline code, fenced block,
JSON example).
正文里解释格式("用法: <parameter name=…>")不该触发重试。残渣判定与块扫描
保持同一条边界: 只有独立成行的标签才算泄漏。形状 D 仍命中(它的 <type> 与
<parameter> 各自独占一行)。

tests: 8 passed (新增 2: 说明句不误报 / 孤行 orphan 仍重试)。
@leonsong09

Copy link
Copy Markdown
Author

superseded by #880 — see #880 / #881.

Folded into #880 as the first commit, unchanged: the pseudo-XML reconstruction, the textual_tool_residue states, and the cut-mid-stream guard inside _parse_text_tool_calls. Its test file and all eight shapes from #863 travel with it, so this PR's review comments would apply there.

Closing to keep one open diff per change; the branch is untouched if you'd rather look at it in isolation.

@leonsong09 leonsong09 closed this Nov 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Pseudo-XML tool calls emitted as plain text are treated as a final answer, silently ending the task (<code_run>, <parameter name=>, <function=>, DSML)

1 participant