[yasamcp] get_file_by_keyword 在 use_regex=True 下非法正则静默返回空列表,且与 ripgrep 正则引擎存在差异
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 63/100
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() 时,存在两处影响可用性的问题:
- 静默吞错返回空列表(且整批丢弃合法词命中):
- 当任一子正则语法非法(例如未转义的
()时,接口不返回错误(MCP 侧isError=False,CLI 正常退出码0),而是直接返回{"results": [], "truncated": false, "total_count": 0}; - 在多关键词查询中(例如
["foo", "("]),即使foo在仓库中真实命中,也会因同批次存在非法正则而被整批静默丢弃; - 在 AI Agent 通过 MCP 调用的场景下,调用方无法区分「代码库确实无匹配」与「正则语法写错」,容易误判为无相关代码而跳过排查。
- 当任一子正则语法非法(例如未转义的
- 预检引擎与执行引擎不一致:
- 前置校验使用 Python 标准库
re.compile(),但实际执行搜索依赖ripgrep(Rustregex引擎); - 合法的 ripgrep/Rust 正则(如 Unicode 属性类
\p{L}+)会被 Pythonre.compile()判定为bad escape \p并静默返回空列表;反之 Python 合法但 Rust 默认不支持的语法(如前瞻(?=foo))会绕过预检并在rg执行阶段才报错。
- 前置校验使用 Python 标准库
环境信息
- 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 的正则方言差异。
改进建议
- 显式报错替代静默返空:当正则无效时,抛出明确的参数错误(MCP 侧返回
isError=True,携带具体非法模式及报错信息;CLI 返回非零退出码并输出错误提示),避免上层 Agent 将语法错误误判为“无命中”。 - 统一正则校验口径:建议直接依赖
ripgrep执行返回的语法错误信息(退出码2及 stderr 中的regex parse error)向调用方透出,避免使用 Pythonre.compile()提前拦截合法的 ripgrep 正则。
- Dominant language
- TypeScript
- Stars
- 339
- Forks
- 40
- Avg merge
- 2h 1m
- Merged PRs (30d)
- 1
Getting set up
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from antgroup/YASA-Engine
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
antgroup/YASA-Engine#170 ·
-
整个工程扫描,异常退出Open
Difficulty 4/5 3-5 days Newbie friendliness 25/100
antgroup/YASA-Engine#166 ·
-
Difficulty 3/5 1-2 days Newbie friendliness 55/100
antgroup/YASA-Engine#162 ·
-
pkg 二进制在大中型 Java 项目上因 Node 18.5.0 V8 bug 崩溃May be free again @AntJiuFo claimed this 138 days ago, and no pull request is open. Open
antgroup/YASA-Engine#143 · 1 assignee ·
-
OSS26-Team
Difficulty 5/5 Over a week Newbie friendliness 35/100
antgroup/YASA-Engine#142 ·
All issues in antgroup/YASA-Engine
Similar issues
-
blocklist addition
Difficulty 1/5 Under an hour Newbie friendliness 76/100
MetaMask/eth-phishing-detect#319194 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
continuedev/continue#13533 · 1 comment ·
Maintainers usually reply within 6 days
-
bug codex:automation
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
genfeedai/genfeed.ai#12060 ·
Maintainers usually reply within 1 day
-
bounty good first issue help wanted
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
fitzss/valun-open#15 · 1 comment ·
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 1 day