Skip to content

code_search fails with exit 128 when a regex-mode search term contains an unbalanced parenthesis #1340

Done

Description

@adolphinvx

OpenCodeReview Version

open-code-review v1.12.4 (f1101fd) darwin/arm64, built at 2026-09-16T09:58:39Z

Operating System

macOS (Apple Silicon)

Installation Method

npm (global)

LLM Provider

Other OpenAI-compatible endpoint

Bug Description

In regex mode (use_perl_regexp: true), code_search passes the model's search_text verbatim to git grep -P. A search term containing an unbalanced ( fails PCRE compilation, so the tool returns exit 128 instead of running the search.

Models search for literals like Set("gorm: when reviewing GORM code, so this comes up often. In one review run, 31 of 348 code_search calls failed this way (about 9%); in another, 11 of 236.

Steps to Reproduce

  1. Run ocr review -f json --audience agent on a changeset that leads the model to search for a literal containing (. A Go codebase using GORM's db.Set("gorm:...") triggers it quickly.
  2. Or run the underlying command directly, in any repo:
$ git grep -P -e 'Set("gorm:'
fatal: -e option, 'Set("gorm:': missing closing parenthesis

The same term under default BRE does not error, because a bare ( is a literal there. Only the -P path fails, and -P is what regex-mode code_search uses.

Expected Behavior

Either run the search as a literal match (for example fall back to git grep -F when the regex fails to compile), or validate the regex first and return an error the model can act on.

Logs / Error Output

[ocr]   ✘ code_search failed: git grep failed: exit status 128: fatal: -e option, 'Set("gorm:': missing closing parenthesis
[ocr]   ✘ code_search failed: git grep failed: exit status 128: fatal: -e option, 'ray.method(': missing closing parenthesis

AI/LLM Disclosure

An AI coding agent working for me collected the failure counts from ocr's session logs. The reproduce command and its output above were run and verified directly in a terminal.

Additional Context

Failures return in milliseconds, so nothing stalls. The cost is one wasted agent round-trip per failure: the model retries with different terms or falls back to reading files chunk by chunk, which slows the review and weakens the search. Escaping all metacharacters is not a good fix, since regex mode is intentional and models pass alternations like a|b|c. Falling back to git grep -F on a compile error, and telling the model about the fallback, keeps both uses working. Related earlier git grep argument-handling bugs in code_search: #645, #53.

Activity

  1. changed the title [-]code_search: 正则模式下含未闭合括号的搜索词使 git grep -P 直接 exit 128[/-] [+]code_search fails with exit 128 when a regex-mode search term contains an unbalanced parenthesis[/+] on Sep 17, 2026
  2. LiRunGuo commented on Sep 17, 2026

    @LiRunGuo

    Opened #1351 for this. On the PCRE compile failure the search is retried as a fixed string with every other argument unchanged (case sensitivity, pathspec, ref, --untracked/--no-index), and the result opens with a note that repeats git's reason (missing closing parenthesis) so the model can escape the pattern if it really wanted a regex.

    Two things the fix deliberately does not do: a pattern PCRE accepts is never retried, and failures that are not about the pattern (unresolvable ref, invalid pathspec) still return exactly the same error as before, because git diagnoses those before it compiles patterns.

  3. sommmen commented on Nov 17, 2026

    @sommmen

    Second reporter, on a different platform and a much later version. Still
    reproduces on v1.13.8 (windows/amd64), and the current main still has no
    retry on a PCRE compile failure — gitGrep in internal/tool/code_search.go
    retries only on isNotGitRepoError (the --no-index path), so a rejected
    pattern goes straight to git grep failed: exit status 128.

    I note #1351 was closed without merging, so I assume the fix is still open for
    whoever picks it up.

    Independent data

    A different corpus, collected from an automated review harness that invokes
    ocr review per run. 99 stored ocr.json manifests, 3,896 code_search calls,
    24 failures (0.62%) — lower than your 9%, and the gap is itself informative:
    these are mostly C#/TypeScript/Angular repositories rather than Go, so the rate
    appears to track how often a language's idioms put a bare ( inside a search
    literal, not anything about the harness.

    20 of the 24 are the unbalanced-paren case you describe. The distinct rejected
    patterns:

    public Commit(
    public GitHubCommit(
    public GitHubCommitStats(
    GitHubCommitStats(
    GetValue(string
    GetValue(string propertyName, StringComparison
    WithNpm(bool
    saveAction =|onSave(
    cdkDropList|moveItemInArray|drop(
    moveUp|moveDown|swap(|Up(index|Down(index
    GetById|Show|Get(
    ResolveStruct|ResolveText|Build(
    login(authService
    public.*class NhSetPropertyCalls|Apply(
    new ContentBlockModels.StickySidebarLayoutContent|StickySidebarLayoutContent(
    export abstract class NhBaseApiService|class BaseApiService|apiService.put<T|put<T>(
    fake0001|recorder.SessionId ==|Equal(.*SessionId
    changedProperties.Select(x =>|messageArguments: changedProperties
    QueryExecutionException(.*ex\)
    replace(/^www\./i
    

    This supports the point in your Additional Context that blanket escaping is the
    wrong fix: 10 of the 20 carry a deliberate | alternation, and two
    (Equal(.*SessionId, QueryExecutionException(.*ex\)) use .* and an escaped
    paren. The model wanted a regex; it just appended a trailing ( to a method
    name. A -F retry preserves both intents, whereas escaping would break the
    alternations.

    A second failure class the same retry would cover

    Four of my 24 are not unbalanced parens. Two are a different PCRE rejection:

    [ocr] ✘ code_search failed: git grep failed: exit status 128:
          fatal: -e option, '\u200d': PCRE2 does not support \F, \L, \l, \N{name}, \U, or \u
    [ocr] ✘ code_search failed: git grep failed: exit status 128:
          fatal: -e option, 'C\u200dursor': PCRE2 does not support \F, \L, \l, \N{name}, \U, or \u
    

    The model was searching for a zero-width joiner by its JavaScript/C# escape
    form, \u200d. PCRE2 spells that \x{200d}, so the pattern is rejected at
    compile time exactly like an unbalanced paren — same exit 128, same -e option
    prefix, same wasted round trip. A literal retry handles it correctly too, since
    \u200d as a fixed string is what a model looking for the characters \u200d
    in source would usually want anyway.

    Worth mentioning because a fix scoped narrowly to paren balancing would miss it,
    whereas "retry as -F when PCRE rejects the pattern" catches both without
    enumerating PCRE's rules.

    Impact in an automated setting

    Agreeing with your framing and adding one consequence specific to unattended use.

    These reviews run under a fixed per-review time cap. A wasted round trip is not
    just slower — it competes directly with coverage, and a review that reaches the
    cap is reported as partial. In the day I looked at most closely, 5 of 17
    reviews ended partial with at least one file receiving no verdict at all. I
    cannot attribute those caps to code_search failures specifically, and I am not
    claiming that: at 0.62% the direct time cost is small, and the honest statement
    is that the failures and the caps coexist rather than that one caused the other.

    The clearer cost is the one you already named: the model loses its best search
    tool for that turn and falls back to reading files, which is more expensive in
    both tokens and wall clock than the fix would be.

    Environment

    OCR version @alibaba-group/open-code-review 1.13.8 (win32-x64 native binary)
    Latest published 1.13.11 (2026-11-17) — main still lacks the retry
    OS Windows 10.0.26200
    Install npm (global)
    Invocation ocr review --from <sha> --to <sha> …, non-interactive
    Evidence 99 ocr.json manifests' tool_calls.failure_details
  4. lizhengfeng101 commented on Nov 24, 2026

    @lizhengfeng101
    Contributor

    Resolved by #1811, which implements the actionable-error option from this report. An invalid regex still fails, but the diagnostic now explains how to escape a literal parenthesis or set use_perl_regexp=false if the whole search term is literal. There is no automatic fallback. Regression tests verify that correcting the parenthesis preserves intended alternation and wildcard matches. Thanks for the examples and failure data.

  5. lee259 commented on Nov 26, 2026

    @lee259

    I hit the same class of failure with \u00A0 on OpenCodeReview v1.14.4 (d4f568f, Linux amd64) and Git 2.47.3. In this run, OCR exited nonzero and our review service marked the entire review as failed.

    Redacted production log (retry guidance omitted):

    ERROR [REDACTED] OCR review failed (no fallback): OCR review failed: [ocr] code_search failed: git grep failed: exit status 128: fatal: -e option, '\u00A0': PCRE2 does not support \F, \L, \l, \N{name}, \U, or \u
    

    To reproduce the underlying Git error, run this from a repository root, using any existing text file in place of README.md:

    git grep --no-index -P -e '\u00A0' -- README.md
    printf 'git grep exit: %s\n' "$?"

    I reproduced exit 128 in the affected container. The review invocation was ocr review --from <base> --to HEAD --format json --audience agent. The trigger during the review is a code_search call with arguments like these:

    {"search_text":"\\u00A0","use_perl_regexp":true}

    The Git command isolates the search error. Reproducing the full review failure also requires the model to issue that tool call. The retry hint added by #1811 was present in the production log, but OCR still exited nonzero in this run. Could this pattern error be returned to the model as a recoverable tool result so it can retry and finish the review?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions