[yasamcp] get_file_by_keyword 在 use_regex=True 下非法正则静默返回空列表,且与 ripgrep 正则引擎存在差异

Open
#171 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
63/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python
Domain
backend, search

Research direction

Read yasamcp/src/yasa_mcp/service/code_search_service.py around lines 791–800, then trace how its search errors reach the MCP and CLI callers. Check the get_file_by_keyword and yasamcp keywords --use-regex paths with an invalid pattern and with a ripgrep-specific pattern such as \p{L}+. Done when invalid syntax is reported to callers and valid ripgrep patterns are no longer rejected by Python regex validation.

Written by the indexing model from the issue text.

Description

问题描述

在 yasamcp 的 get_file_by_keyword(以及 CLI yasamcp keywords --use-regex)中,当 use_regex=True 且传入的正则无法通过 Python re.compile() 时,存在两处影响可用性的问题:

  1. 静默吞错返回空列表(且整批丢弃合法词命中):
    • 当任一子正则语法非法(例如未转义的 ()时,接口不返回错误(MCP 侧 isError=False,CLI 正常退出码 0),而是直接返回 {"results": [], "truncated": false, "total_count": 0};
    • 在多关键词查询中(例如 ["foo", "("]),即使 foo 在仓库中真实命中,也会因同批次存在非法正则而被整批静默丢弃;
    • 在 AI Agent 通过 MCP 调用的场景下,调用方无法区分「代码库确实无匹配」与「正则语法写错」,容易误判为无相关代码而跳过排查。
  2. 预检引擎与执行引擎不一致:
    • 前置校验使用 Python 标准库 re.compile(),但实际执行搜索依赖 ripgrep(Rust regex 引擎);
    • 合法的 ripgrep/Rust 正则(如 Unicode 属性类 \p{L}+)会被 Python re.compile() 判定为 bad escape \p 并静默返回空列表;反之 Python 合法但 Rust 默认不支持的语法(如前瞻 (?=foo))会绕过预检并在 rg 执行阶段才报错。
环境信息
  • yasamcp 版本:YASA-MCP 1.2.0 (build 20260924, commit f5965e6)(上游 main 分支当前最新相关提交 59d22349 同样存在)
  • 操作系统:Ubuntu 24.04.4 LTS (x86_64, WSL2)
  • 关联 Issue:与 #170 同属 get_file_by_keyword 的正则处理路径
最小复现步骤
mkdir -p /tmp/yasamcp-repro
printf 'def hello():\n    return "foo"\n' > /tmp/yasamcp-repro/demo.py
yasamcp init /tmp/yasamcp-repro

# 对照:单查 foo 正常命中 1 条
yasamcp keywords -p /tmp/yasamcp-repro --use-regex -j foo

# 复现 1:单非法正则 '(',无报错,静默返回空结果
yasamcp keywords -p /tmp/yasamcp-repro --use-regex -j '('

# 复现 2:合法命中词 'foo' 与非法正则 '(' 同传,无报错,且 'foo' 的命中被静默吞掉
yasamcp keywords -p /tmp/yasamcp-repro --use-regex -j foo '('

实际输出(复现 1 与复现 2 均返回):

{
  "results": [],
  "truncated": false,
  "total_count": 0
}
根因定位

问题位于 yasamcp/src/yasa_mcp/service/code_search_service.py 第 791–800 行:

    if use_regex:
        for value in keywords:
            try:
                re.compile(value)
            except re.error as exc:
                log_util.warning(
                    f"get_file_by_keyword: 非法正则 keyword={value!r}, error={exc}",
                    logger=logger,
                )
                return []

这里仅记录了服务端日志,随后直接 return [],既没有向调用方透出语法错误,也引入了 Python re 与 ripgrep 的正则方言差异。

改进建议
  1. 显式报错替代静默返空:当正则无效时,抛出明确的参数错误(MCP 侧返回 isError=True,携带具体非法模式及报错信息;CLI 返回非零退出码并输出错误提示),避免上层 Agent 将语法错误误判为“无命中”。
  2. 统一正则校验口径:建议直接依赖 ripgrep 执行返回的语法错误信息(退出码 2 及 stderr 中的 regex parse error)向调用方透出,避免使用 Python re.compile() 提前拦截合法的 ripgrep 正则。
Dominant language
TypeScript
Stars
339
Forks
40
Avg merge
2h 1m
Merged PRs (30d)
1

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from antgroup/YASA-Engine

All issues in antgroup/YASA-Engine

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.