[Engine] serve/frontend.py: recover missing '>' before newline and minor low-bit tool-name stutters in <function=NAME> (including streaming _scan)

未关闭 适合新手
#7,403 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 4 天内回复

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
88/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
python
领域
api, backend

调研方向

从 terminal/serve/frontend.py 开始,重点关注 parse_tool_call()、_callable()、_scan() 和 _finish_call(),然后运行现有的 terminal/serve 单元测试。实现所提出的基于 schema 白名单的名称恢复机制,并允许第一个参数之前缺少 >。当格式错误的流式和非流式用例通过、未声明的名称仍然被拒绝,并且所有现有的 terminal/serve 测试仍然通过时,即视为完成。

由索引模型根据 Issue 内容生成。

描述

Problem

When serving low-bit quantizations (e.g. Qwen3.8-Flash-Next IQ3_S) to OpenAI-compatible tool-calling clients / coding agents with recover=True, two minor, unambiguously repairable <function=NAME> formatting glitches still fail parsing in terminal/serve/frontend.py and leak raw <tool_call> markup into assistant text (terminating the agent turn):

  1. Missing > before newline in <function=NAME>:

    <tool_call>
    <function=bash
    <parameter=command>echo ok</parameter>
    </function>
    </tool_call>
    

    Both parse_tool_call() and _finish_call() require > to exist and appear before the first <parameter=...>. When the model omits > and immediately emits \n<parameter=...>, parse_tool_call() raises malformed tool call (or splits on the > inside <parameter=...>), corrupting the tool name and dropping the call.

  2. Minor quantization stutters in <function=NAME> (and missing recovery in streaming _scan()):
    Under low-bit quantizations, the model occasionally emits a single-character stutter or trailing delimiter on an otherwise valid declared tool name, such as:

    • Trailing punctuation or accidental =param: <function=bash,>, <function=bash=command>
    • Doubled leading character: <function=ggrep_text> (for declared grep_text)
    • Single-letter + underscore stutter prefix: <function=p_present_files> (for declared present_files)

    Currently _callable(), streaming _scan(), and _finish_call() only do .strip() + alias lookup, so these calls are rejected and leaked as plain text even when the repaired name uniquely matches a declared tool in self.schemas.

(Related to but distinct from #6022 [hallucinated undeclared names] and #6979 [argument name typos]. Checked v0.2.23: neither fix is present in terminal/serve/frontend.py yet.)

Proposed Fix (minimal, strictly whitelisted to schemas)

Add a conservative helper _recover_declared_name(raw, schemas, aliases) that only accepts a repaired name when it matches a declared tool in schemas (never invents an undeclared tool), and wire it into parse_tool_call(), _callable(), streaming _scan(), and _finish_call() when recover=True:

def _recover_declared_name(raw: str, schemas: dict | None, aliases: dict | None = None) -> str:
    """Safely repair minor low-bit quantization stutters in `<function=NAME>` ONLY when the repaired name
    uniquely matches a declared tool in `schemas` (e.g. `ggrep_text` -> `grep_text`, `p_present_files` -> `present_files`,
    `bash,` or `bash=command` -> `bash`). Never invents an undeclared tool."""
    name = raw.strip()
    if aliases:
        name = aliases.get(name, name)
    if not schemas or name in schemas:
        return name
    # 1. Strip stray trailing punctuation or `=param` (e.g. `bash,`, `bash=command`)
    cand = re.split(r"[,;:\s=]", name, maxsplit=1)[0].strip()
    if cand and cand in schemas:
        return cand
    # 2. Doubled leading letter stutter (e.g. `ggrep_text` -> `grep_text`)
    if len(name) >= 3 and name[0] == name[1] and name[1:] in schemas:
        return name[1:]
    # 3. Single-letter + underscore stutter prefix (e.g. `p_present_files` -> `present_files`)
    if len(name) >= 4 and name[1] == "_" and name[2:] in schemas and name[0] == name[2]:
        return name[2:]
    return name

Key wiring changes (diff against v0.2.17 terminal/serve/frontend.py):

  • parse_tool_call() & _finish_call(): when recover=True and \n occurs before > (or > is missing before \n), slice the function name at \n instead of failing or slicing into <parameter=...>.
  • _callable(): return True if self.recover and _recover_declared_name(name, self.schemas, self.aliases) in self.schemas.
  • Streaming _scan() & _finish_call(): resolve name = _recover_declared_name(raw_name, self.schemas, self.aliases) if self.recover else ....
Verification
  • Running in daily production for 1 week on Windows 11 + RTX 4070 Ti (12 GB) + Qwen3.8-Flash-Next IQ3_S (strata 0.1.63 -> 0.1.65 -> 0.1.70) with OpenAI-compatible coding agents.
  • Passed 12/12 new malformed-call unit cases (both streaming and non-streaming, including negative cases verifying undeclared names are still rejected) and 72/72 existing terminal/serve unit tests.
  • Zero leaked <tool_call> blocks observed since applying this patch.
主要语言
C++
星标
11.6k
派生
1k
PR 合并指标
30 天内没有已合并 PR

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

Niko1221/Strata 的其他 Issue

查看 Niko1221/Strata 的全部 Issue

相似的 Issue

更多 C++ Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。