Skip to content

fix(tool): preserve binary matches in code_search - #2046

Merged
lizhengfeng101 merged 7 commits into
alibaba:mainfrom
Bbbtt04:fix/code-search-binary-only
Nov 25, 2026
Merged

lizhengfeng101 merged 7 commits into
alibaba:mainfrom
Bbbtt04:fix/code-search-binary-only

Conversation

@Bbbtt04

@Bbbtt04 Bbbtt04 commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Description

code_search could return an empty result when every match was inside a binary file. Git emits a path-only message for a binary hit, which the parser normally skipped. The review loop treats empty tool content as a failed round, so repeated binary-only searches could stop a review early.

The tool now preserves binary hits as file paths and omits their content and line numbers. A genuine no-match still returns No matches found. The first 100 matches share a single limit in Git output order. Each text line or matching binary file counts as one result.

The behavior analysis path retains binary entries for files in its authorized scope and applies the same shared limit across batches. Binary entries produce no line-level evidence. The tool documentation describes both output forms.

The parser still has known edge cases for paths containing :<digits>: in workspace output and for Git-quoted paths in scoped analysis.

Type of Change

  • Bug fix (non-breaking change that fixes an issue)

How Has This Been Tested?

  • TestGitGrep_ResultLimit covers binary-only and mixed matches in workspace, commit/ref, and plain-directory modes, including exact-limit and truncation cases.
  • The behavior analysis tests cover scope filtering, binary-only output, and one shared limit across multiple batches.
  • make check passed. The targeted make coverage run for internal/tool and internal/behaviortask passed with 93.7% combined coverage.

The full local coverage run reported a Windows pipe-cancellation test failure in internal/gitcmd. The local race run could not start without GCC.

AI/LLM disclosure

The original patch used DeepSeek Harness (deepseek-v4-flash). The follow-up used OpenAI Codex (GPT-6) and the OCR delegation skill for review.

Fixes #2045

git answers a binary hit with "Binary file <path> matches", a line with no
line number that the result parser drops. A search whose hits are all binary
therefore returned an empty string instead of the documented "No matches
found", and the tool-use loop reads an empty result as a call that produced
nothing, so three such rounds stopped the subtask with StopEmptyRounds.

Pass -I so git excludes binary files from the match set. Ref mode then exits
1 and reuses the existing no-match path; text and mixed matches are unchanged.
@github-actions

Copy link
Copy Markdown
Contributor

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

@verdrellw60-max verdrellw60-max mentioned this pull request Nov 22, 2026
1 task done
@chaojixinren

Copy link
Copy Markdown
Contributor

Thanks for the detailed reproduction. The empty-result bug is real, but I don’t think -I is the right output contract. It turns “the search text exists in b.bin” into “No matches found”, discarding information that can be important when searching for embedded credentials, license text, resource names, or version strings. The model may then stop investigating instead of reporting or further examining the relevant artifact.

Could we keep Git’s binary matches and parse Binary file matches explicitly instead? For example:

text
File: b.bin
Binary file matches; content not shown

This fixes the root cause—the tool no longer returns an empty string—while preserving the path-level signal. It also avoids inventing line numbers or trying to decode binary content, and the same Git output is available in workspace, ref, and --no-index modes.

The current parser already drops this line, so preserving it is a small output-contract improvement as well as a bug fix. I would prefer that over permanently excluding binary files.

Please also update internal/behaviortask/search.go: it currently flushes a file only when it has numbered text matches, so a binary-only entry would otherwise be silently discarded on the analysis path.

@Bbbtt04 Bbbtt04 changed the title fix(tool): keep binary files out of code_search matches fix(tool): preserve binary matches in code_search Nov 23, 2026
@Bbbtt04

Bbbtt04 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the feedback, @chaojixinren. Here is the consolidated fix at d385933.

  • code_search keeps Git's binary matches in workspace, commit/ref, and plain-directory searches. It returns the matching file path with Binary file matches; content not shown. A genuine miss still returns No matches found.
  • Scoped behavior analysis keeps binary matches for authorized paths and emits line locations only for text matches.
  • Each matching text line and each matching binary file count toward the first 100 results in Git output order. The analysis applies that cap across batches. Both layers use their own package-local limit set to 100.
  • The tool documentation describes both output forms and how the limit is counted.

make check and the focused internal/tool and internal/behaviortask suites passed locally. Regression coverage includes binary-only and mixed searches, exact-limit and truncation cases, and the shared cap across analysis batches. The PR description records the remaining path-format edge cases.

@lizhengfeng101

Copy link
Copy Markdown
Contributor

Thanks for preserving binary hits without inventing a line number. I checked the two path-format limitations noted in the PR against d385933 using independent Git fixtures. Could you address both before merge? They affect whether callers can trust the reported path and line, not just how the result is displayed.

  1. A binary hit can look identical to a real text hit. In both workspace and --no-index searches, a matching binary named asset:12:data.bin and a text file named Binary file asset whose twelfth line is data.bin matches produce exactly the same output: Binary file asset:12:data.bin matches. The parser reports line 12 for both; reversing the checks would misclassify the text file instead. With git grep -z, the text record has NUL field separators and the binary diagnostic does not. A paired regression test should require the real text line in one case and only the binary path and marker in the other.

  2. A quoted path can turn a real hit into No matches found. For a binary named asset"v2.bin, ref-mode git grep emits Binary file <ref>:"asset\"v2.bin" matches even with core.quotepath=false. git ls-files -z returns the literal filename. The scoped lookup compares those two spellings and drops the authorized hit. Please preserve the original path before rendering File: rather than relaxing the scope check. A regression test should assert the literal filename in ordinary search and the same authorized hit with zero line locations in scoped Analysis.

@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.

Withdrawn.

@lizhengfeng101

Copy link
Copy Markdown
Contributor

Thanks for following up on the earlier feedback, @Bbbtt04. After reconsidering the trade-off, I reverted the binary-match reporting changes in 626c49f and kept your original git grep -I fix.

There are cases where knowing that a binary file contains a search term is useful, but I think they’re too rare in code review to justify adding binary search support to code_search. On balance, I’d rather keep the tool focused on text matches: if only binary files contain the term, it returns No matches found.

Sorry for the back-and-forth, especially after I asked you to address the path-handling edge cases. This was a change in my judgment about what the tool should do, not a problem with how you responded to the 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

@Bbbtt04

Bbbtt04 commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for following up on the earlier feedback, @Bbbtt04. After reconsidering the trade-off, I reverted the binary-match reporting changes in 626c49f and kept your original git grep -I fix.

There are cases where knowing that a binary file contains a search term is useful, but I think they’re too rare in code review to justify adding binary search support to code_search. On balance, I’d rather keep the tool focused on text matches: if only binary files contain the term, it returns No matches found.

Sorry for the back-and-forth, especially after I asked you to address the path-handling edge cases. This was a change in my judgment about what the tool should do, not a problem with how you responded to the review.

@lizhengfeng101 Got it. That’s what I originally had in mind. I’m still getting familiar with the project, so thanks for your guidance!

@lizhengfeng101
lizhengfeng101 merged commit d4f568f into alibaba:main Nov 25, 2026
12 checks passed
@sayedtamim840088-netizen

Copy link
Copy Markdown

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

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.

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

4 participants