Skip to content

fix(tool): code_search returns an empty result when only binary files match #2045

Done

Description

@Bbbtt04

Problem

code_search returns an empty string when every match is inside a binary file, instead of the documented No matches found (pages/src/content/docs/en/tools.md).

An empty tool result is not a harmless no-op downstream: internal/llmloop/loop.go replaces empty tool content with Error: Tool execution returned no result. and leaves hasValidResult false. Three consecutive such rounds end the subtask with StopEmptyRounds, which classifyMainLoopStop maps to the unknown failure class. That group is then reported as failed(class=unknown), and because it is not a declared budget stop, the process exits non-zero. A search whose hits happen to be all-binary can therefore truncate a review and fail CI.

Reproduction

A real binary file (NUL byte present) is enough. git grep answers with a line that has no line number:

$ printf 'needle here\n' > a.txt
$ printf '\0needle\n' > b.bin

$ git grep --no-index --exclude-standard -F -n -e needle .
a.txt:1:needle here
Binary file b.bin matches        # exit 0

When only the binary file matches, in every mode the tool can run (workspace, commit/ref, and the --no-index plain-directory path):

$ git grep -F -n -e needle -- b.bin
Binary file b.bin matches        # exit 0

internal/tool/code_search.go parses lines of the shape path:line:content (or ref:path:line:content). Binary file <path> matches has no line number, so the line is skipped by if len(parts) < splitN { continue }, no match is ever appended, and the function returns sb.String(), nil — an empty string. Because git exits 0 for a binary hit, the existing exitCode == 1 → "No matches found" branch does not run either.

Expected: No matches found, the string the tool already returns for a genuine no-match and the one the documentation promises.

The regression test written for this reports it directly in all three modes:

code_search_test.go:240: binary-only search = "", want "No matches found"
--- FAIL: TestGitGrep_ResultLimit/workspace/binary_only
--- FAIL: TestGitGrep_ResultLimit/commit/binary_only
--- FAIL: TestGitGrep_ResultLimit/plain_directory/binary_only

Proposed fix

Pass -I (--binary-files=without-match) to git grep so git excludes binary files from the match set:

  • binary-only match → git exits 1 → the existing "No matches found" path is reused;
  • text and mixed matches → unchanged, since the parser already discarded binary hits;
  • the --no-index path used for plain directories honours -I as well. The long form --binary-files=without-match is rejected there (error: unknown option, exit 129), so the short option is required.

Treating binary hits as "not a result" matches the existing design: the binary_before_text, binary_with_exact_limit, binary_with_truncation and binary_only cases of TestGitGrep_ResultLimit all assert text-match counts that exclude binary files. This change only makes git perform that exclusion at the source.

Scope

Any ocr review or ocr scan run whose code_search call matches only binary files: images, archives, compiled artifacts, or a source file containing a stray NUL byte. Searching for a resource name, a license string or an embedded version number is enough to land there.

AI/LLM disclosure

This issue and the linked change were prepared with DeepSeek Harness (model deepseek-v4-flash). Every command, output and test result quoted above was run locally on Windows with git 2.47.1 and Go 1.26.10.

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