Skip to content

[Bug] 搜索工具的 ignore 参数类型转换导致静默错误 / Type conversion for ignore parameter in search tools causes silent errors #129

@SunYanbox

Description

@SunYanbox

涉及工具 / Affected tools: regex_search, exact_search, symbol_ref

这些工具声明了 ignore: list[str] | None 参数。当 AI 通过 XML 传入参数时,所有值都是字符串。ToolRegistry.execute 调用 tool.convert_args(kwargs) 尝试根据类型注解进行转换,但转换逻辑无法正确处理 list[str] | None 这样的现代类型提示。
These tools declare an ignore: list[str] | None parameter. When the AI passes arguments via XML, all values become strings. ToolRegistry.execute calls tool.convert_args(kwargs) to convert according to type annotations, but the conversion logic cannot handle modern type hints like list[str] | None.

根本原因 / Root cause:
convert_param_type 只识别裸类型名(如 "list""int")和 <class 'xxx'> 格式。对于联合类型 list[str] | None,它得到字符串 "list[str] | none",无法匹配任何转换分支,最终保留原始字符串。
convert_param_type only recognizes bare type names (e.g., "list", "int") and the <class 'xxx'> format. For a union type like list[str] | None, it gets the string "list[str] | none", fails to match any conversion branch, and leaves the original string unchanged.

实际后果 / Actual consequences:
ignore 参数保持为原始字符串。当 AI 传入类似 ".git\n__pycache__" 的值时,代码会逐字符遍历字符串:
ignore remains a raw string. When the AI supplies a value like ".git\n__pycache__", the code iterates over the string character by character:

for ignore_pattern in ignore:  # 字符串的每个字符 / each character of the string
    ignore_patterns.append(re.compile(ignore_pattern))

每个字符(包括 ".""g""i""t"、换行符等)都被编译为正则模式。其中 "." 会匹配任意字符,导致几乎所有文件路径被错误忽略。合法模式(如果有)与垃圾模式混在一起,搜索结果完全不可预测,且没有任何错误输出——这是一个完全静默的错误
Each character (including ".", "g", "i", "t", newline, etc.) is compiled as a regex pattern. The dot "." matches any character, causing almost all file paths to be incorrectly ignored. Legitimate patterns (if any) get mixed with garbage patterns, making search results completely unpredictable, with no error output — this is a completely silent error.

影响范围 / Impact:
所有依赖 ignore 参数的搜索工具。此外,相同的转换逻辑无法处理 Optional[list[str]]dict[str, Any] 或 Python 3.10+ 的任何联合类型语法,可能影响更多工具的参数。
All search tools that rely on the ignore parameter. Moreover, the same conversion logic fails to handle Optional[list[str]], dict[str, Any], or any union type syntax from Python 3.10+, potentially affecting parameters of other tools.

建议修复 / Suggested fix:
重写 convert_param_type,使用 evaltyping.get_origin / typing.get_args 安全地解析类型注解(注意安全性,仅解析已知类型),或者要求工具作者显式提供自定义转换器。更好的方案是改变参数传递协议,让 XML 能够表达嵌套结构。
Rewrite convert_param_type to safely parse type annotations using eval (with a restricted namespace) or typing.get_origin / typing.get_args, or require tool authors to provide explicit custom converters. A better long-term solution is to change the argument passing protocol so that XML can represent nested structures.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions