Skip to content

fix(tool): return a model-facing error for invalid code_search regex - #1811

Merged
lizhengfeng101 merged 7 commits into
alibaba:mainfrom
OUAO-FRANK:fix/1570-code-search-regex-error
Nov 24, 2026
Merged

lizhengfeng101 merged 7 commits into
alibaba:mainfrom
OUAO-FRANK:fix/1570-code-search-regex-error

Conversation

@OUAO-FRANK

Copy link
Copy Markdown
Contributor

Summary

code_search with use_perl_regexp=true and an invalid pattern (e.g. func (unclosed) makes git grep -P exit 128 with a regex-compile message in stderr. CodeSearchProvider.Execute returned that as a fatal Go error (git grep failed: ...), which in an MCP agent workflow aborts the whole tool session instead of giving the model a message it can inspect and self-correct.

The fix detects the regex-compile failure (exit 128 + a pcre2_compile failed / missing closing parenthesis / missing terminating ] / nothing to repeat diagnostic) and returns Error: invalid regular expression: <stderr> with a nil error, so the caller/LLM can adjust the pattern and retry.

Testing

Updated TestGitGrep_PerlRegexp_InvalidPattern_ReturnsError to assert the new contract: err == nil and the result is a invalid regular expression message carrying the git diagnostic. It fails on the previous code (git grep failed: exit status 128: ...) and passes now. The detection covers both the older pcre2_compile failed wording and the newer missing closing parenthesis wording.

$ GOTOOLCHAIN=go1.26.9 go test ./internal/tool/... -count=1
ok  	github.com/alibaba/open-code-review/internal/tool	28.6s

gofmt -l on the changed files is clean.

Fixes #1570.

git grep -P with an invalid pattern exits 128 with a regex-compile message in
stderr, and code_search returned that as a fatal Go error. In an MCP agent
workflow that aborts the whole tool session instead of giving the model a
message it can inspect and self-correct. Detect the regex-compile failure and
return "Error: invalid regular expression: <stderr>" with a nil error instead.
@github-actions

Copy link
Copy Markdown
Contributor

✅ OpenCodeReview: Review complete: 0 finding(s) across 1 selected item(s).

@chaojixinren

Copy link
Copy Markdown
Contributor

The current implementation only covers a few specific PCRE diagnostics; other invalid regular expressions still return the generic git grep failed, leaving a functional gap.

To keep the change scoped, consider detecting the stable structure of regex compilation failures instead of continuing to enumerate individual error messages. A few representative tests should be sufficient.

The previous helper enumerated individual PCRE diagnostics (missing closing
parenthesis, missing terminating ], nothing to repeat), leaving a gap for any
other invalid pattern. Detect the stable structure instead: exit 128 with
stderr naming the -e option (newer git) or pcre2_compile failed (older git),
which identifies the pattern argument itself as the failure regardless of the
specific PCRE reason.
@OUAO-FRANK

Copy link
Copy Markdown
Contributor Author

@chaojixinren good call — enumerating diagnostics left a gap. Reworked isPerlRegexpCompileError in 5acf81b to key off the stable structure instead: exit 128 with stderr naming the -e option (newer git) or pcre2_compile failed (older git), which identifies the pattern argument itself as the failure regardless of the specific PCRE reason.

The regression test now exercises four distinct failures — unmatched (, unmatched [, *a (nothing to repeat), and a bad \x escape — and all return the model-facing invalid regular expression message.

@NanaseInori NanaseInori left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed both changed files, #1570 and the discussion, and compared the earlier open #1571. Both address the same provider-error contract. This head has a useful implementation difference: it checks exit 128 and the Git pattern-error envelope, rather than matching only one specific PCRE reason in Execute.

The current change addresses the diagnostic-enumeration concern already raised in the thread. Cancellation/deadline handling stays ahead of the conversion, and ordinary repository/I/O errors retain the error path. The four regression patterns cover distinct syntax failures. I found no blocking defect in this scoped implementation.

I independently ran Git against the four patterns in a temporary Linux repository. Each exited 128 with an '-e option' diagnostic and matched the new predicate, including the quantifier and bad-escape cases that differ from an unmatched parenthesis. This was a Git-diagnostic fixture, not execution of OCR's tool package or an MCP integration.

Approval concerns this code change; only one of the overlapping #1571/#1811 implementations should land. I have not established the description's stronger claim that every caller currently aborts an entire session on the returned Go error.

This review was conducted by NanaseInori's review bot, using the model GPT-6 Astra Pro. If you need a human review, please manually @NanaseInori.

@chaojixinren

Copy link
Copy Markdown
Contributor

@OUAO-FRANK fix ci

@lizhengfeng101

Copy link
Copy Markdown
Contributor

Maintainer update for #1340: 6384cde incorporates the changes from #2069 into this PR. Invalid regex searches still count as failed tool calls; the diagnostic now tells the model how to retry without silently changing the meaning of .* or |. make check and make test passed on the integrated tree.

AI disclosure for the maintainer changes only: OpenAI Codex CLI (gpt-6.2-astra) was used for implementation and testing, and OpenCodeReview (gpt-6.2-astra) for the pre-commit review.

@lizhengfeng101 lizhengfeng101 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@lizhengfeng101
lizhengfeng101 merged commit f6379f7 into alibaba:main Nov 24, 2026
12 checks passed
hybridindie added a commit to hybridindie/ocr-bridge that referenced this pull request Nov 25, 2026
1.14.3 (upstream alibaba/open-code-review#1811) returns an invalid
code_search regex to the model instead of an empty result. Models set
use_perl_regexp for literal text such as `falls(&self, ground:`, which
PCRE rejects.

src/git-shim.sh is the older stopgap: a `git grep -P` whose pattern fails
to compile is retried once with -F; every other git command passes
through. It is opt-in (`--build-arg GIT_SHIM=true`) and can be removed
once 1.14.3 is confirmed. Verified locally: both image variants build,
git 2.47.3, `ocr` 1.14.3, and the shim resolves first only when enabled.

Chart/package 0.5.1. 40 tests pass.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01VDr7G12USSrVSM89v5brik
hybridindie added a commit to hybridindie/ocr-bridge that referenced this pull request Nov 25, 2026
…#4)

1.14.3 (upstream alibaba/open-code-review#1811) returns an invalid
code_search regex to the model instead of an empty result. Models set
use_perl_regexp for literal text such as `falls(&self, ground:`, which
PCRE rejects.

src/git-shim.sh is the older stopgap: a `git grep -P` whose pattern fails
to compile is retried once with -F; every other git command passes
through. It is opt-in (`--build-arg GIT_SHIM=true`) and can be removed
once 1.14.3 is confirmed. Verified locally: both image variants build,
git 2.47.3, `ocr` 1.14.3, and the shim resolves first only when enabled.

Chart/package 0.5.1. 40 tests pass.


Claude-Session: https://claude.ai/code/session_01VDr7G12USSrVSM89v5brik

Co-authored-by: Claude Opus 5.5 <[email protected]>
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.

4 participants