Repository navigation
fix(tool): return a model-facing error for invalid code_search regex - #1811
Conversation
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.
|
✅ OpenCodeReview: Review complete: 0 finding(s) across 1 selected item(s). |
|
The current implementation only covers a few specific PCRE diagnostics; other invalid regular expressions still return the generic 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.
|
@chaojixinren good call — enumerating diagnostics left a gap. Reworked The regression test now exercises four distinct failures — unmatched |
NanaseInori
left a comment
There was a problem hiding this comment.
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.
|
@OUAO-FRANK fix ci |
|
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 AI disclosure for the maintainer changes only: OpenAI Codex CLI ( |
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
…#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]>
Summary
code_searchwithuse_perl_regexp=trueand an invalid pattern (e.g.func (unclosed) makesgit grep -Pexit 128 with a regex-compile message in stderr.CodeSearchProvider.Executereturned 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 repeatdiagnostic) and returnsError: invalid regular expression: <stderr>with anilerror, so the caller/LLM can adjust the pattern and retry.Testing
Updated
TestGitGrep_PerlRegexp_InvalidPattern_ReturnsErrorto assert the new contract:err == niland the result is ainvalid regular expressionmessage carrying the git diagnostic. It fails on the previous code (git grep failed: exit status 128: ...) and passes now. The detection covers both the olderpcre2_compile failedwording and the newermissing closing parenthesiswording.gofmt -lon the changed files is clean.Fixes #1570.