Skip to content

fix: correct file count display for Grep and Glob tool results#562

Open
zuikusanxun wants to merge 4 commits intositeboon:mainfrom
zuikusanxun:fix/grep-file-count-display
Open

fix: correct file count display for Grep and Glob tool results#562
zuikusanxun wants to merge 4 commits intositeboon:mainfrom
zuikusanxun:fix/grep-file-count-display

Conversation

@zuikusanxun
Copy link
Copy Markdown

@zuikusanxun zuikusanxun commented Mar 20, 2026

Summary

  • Fix incorrect file count display in Grep and Glob tool result panels
  • When toolUseResult structured data (numFiles / filenames) is unavailable, fall back to parsing the content string to count actual file lines
  • Filter out non-file lines (e.g., "Found X files", "No files found", "No matches found") for accurate counting

Details

The Grep and Glob tool configs in toolConfigs.ts previously relied solely on toolUseResult.numFiles or toolUseResult.filenames.length for the file count. When these structured fields were missing or empty (which happens in certain response formats), the count would incorrectly show 0.

This fix:

  1. Prefers structured data (numFiles / filenames) when available (using ?? instead of ||)
  2. Falls back to parsing result.content line-by-line, filtering out summary/status lines
  3. Uses the same fallback logic in both title() and getContentProps() to keep count and file list consistent

Test

  • npm run build passes
  • Verified Grep/Glob tool results now show correct file counts in both structured and unstructured response scenarios

Summary by CodeRabbit

  • Bug Fixes

    • Search results now correctly filter status/summary lines so file counts and lists display accurately.
    • Handles empty or unexpected search output more robustly to prevent misleading or missing results.
  • Refactor

    • Standardized and more resilient processing of tool results, improving consistency and null-safety.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Mar 20, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: b8944787-b180-4bc6-87a7-fa5b0afe6c79

📥 Commits

Reviewing files that changed from the base of the PR and between 56b3613 and c2b3805.

📒 Files selected for processing (1)
  • src/components/chat/tools/configs/toolConfigs.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/components/chat/tools/configs/toolConfigs.ts

📝 Walkthrough

Walkthrough

Added shared file-output helpers (STATUS_LINES, parseToolFileLines, getFileCountTitle, getFileList) to normalize tool result content and prefer structured file counts/filenames; updated Grep and Glob configs to use these helpers; minor formatting/indentation and subagent text-joining tweaks in Task tool logic.

Changes

Cohort / File(s) Summary
Tool Result Handling
src/components/chat/tools/configs/toolConfigs.ts
Added STATUS_LINES and parseToolFileLines(content) to normalize and filter status lines; added getFileCountTitle(result) and getFileList(result) to prefer toolUseResult.numFiles/toolUseResult.filenames with safe fallbacks to parsed content. Updated Grep and Glob result title and getContentProps to use these helpers. Minor indentation/formatting and subagent text-joining tweaks in Task tool conditions.

Poem

🐰 I hop through lines both short and long,
Trim the noise and sort the song,
Count the files with gentle care,
Fallbacks ready, tidy fare,
Small hops, big fixes — code feels strong.

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'fix: correct file count display for Grep and Glob tool results' directly and clearly summarizes the main change: fixing incorrect file count display in Grep and Glob tool results by adding fallback parsing logic when structured data is unavailable.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/components/chat/tools/configs/toolConfigs.ts`:
- Around line 188-191: The code uses "fromStructured as number" which only
affects TypeScript types and doesn't coerce runtime values (so strings like "1"
will break pluralization); update the logic in toolConfigs.ts (look for the
fromStructured variable set from toolData.numFiles and
toolData.filenames?.length, and the later similar block around lines 228-231) to
coerce and validate the numeric value (e.g., use Number(...) or parseInt and
check Number.isFinite or isNaN) before assigning to count, defaulting to 0 or
length when invalid, and then use that numeric count for the `Found ${count}
${count === 1 ? 'file' : 'files'}` string so pluralization is correct at
runtime.
- Around line 193-194: Guard against non-string result?.content before
splitting: derive a normalized string (if Array.isArray(result?.content) join
with '\n', else use String(result?.content ?? '')). Then split on '\n', trim
each line before checks, and apply the existing filters against the trimmedLine
(e.g. check startsWith('Found '), equal 'No files found' / 'No matches found').
Update the occurrences where lines are computed (the lines variable and
subsequent count) so they use this normalized-and-trimmed pipeline to avoid
exceptions when content is an object/array and to correctly ignore status lines
with surrounding whitespace.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 53f00a4d-2709-4950-bd92-d124ba68b704

📥 Commits

Reviewing files that changed from the base of the PR and between 08a6653 and b906a26.

📒 Files selected for processing (1)
  • src/components/chat/tools/configs/toolConfigs.ts

… guard

- Replace `as number` with `Number()` + `Number.isFinite()` to coerce
  and validate structured numFiles at runtime
- Extract `parseToolFileLines()` helper to safely handle non-string
  content (array/object) and trim lines before filtering status lines
- Deduplicate parsing logic across Grep and Glob tool configs
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Nitpick comments (1)
src/components/chat/tools/configs/toolConfigs.ts (1)

220-220: mode is currently dead data in file-list content props.

mode: 'grep' | 'glob' is returned but not consumed by the file-list rendering path, so it adds surface area without effect. Consider removing it, or wiring it into FileListContent if intended.

Also applies to: 261-261

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/components/chat/tools/configs/toolConfigs.ts` at line 220, The returned
object from the file-list config includes a dead "mode" property ('grep' |
'glob') that is not used by the FileListContent rendering path; either remove
the mode property from the return values in the functions in toolConfigs.ts that
produce { files, mode: ... } (refer to the return sites that currently emit
mode) or thread the mode into the FileListContent props and its consumer
components (update FileListContent's prop type and its render/handler logic to
respect mode), and remove the duplicate unused occurrences (also adjust the
second occurrence analogous to the first).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/components/chat/tools/configs/toolConfigs.ts`:
- Around line 217-220: The code assigns files using toolData.filenames?.length
which can be truthy for non-array values and later breaks consumers expecting an
array; update the selection logic around the files variable to ensure
toolData.filenames is an actual array (use Array.isArray(toolData.filenames))
before using it, otherwise fall back to parseToolFileLines(result?.content);
apply the same guard where files is set again (the other occurrence referencing
toolData.filenames and parseToolFileLines) so downstream code that calls
files.map always receives a true array.
- Around line 205-212: The current branch that handles fromStructured (the
variable fromStructured) returns "Found 0 files" when Number(fromStructured) is
not finite, which prevents falling back to parsing actual file lines; change the
logic in the block that uses fromStructured so that if Number(fromStructured) is
not a finite number you do not return early but instead fall through to use
parseToolFileLines(result?.content) and compute the count from parsed lines
(same fix needed in the other identical block around the parseToolFileLines
usage); update the branches referencing fromStructured and parseToolFileLines to
prefer the numeric structured count only when Number.isFinite(count) is true,
otherwise compute count via parseToolFileLines(result?.content) and return the
correct "Found X file(s)" string.

---

Nitpick comments:
In `@src/components/chat/tools/configs/toolConfigs.ts`:
- Line 220: The returned object from the file-list config includes a dead "mode"
property ('grep' | 'glob') that is not used by the FileListContent rendering
path; either remove the mode property from the return values in the functions in
toolConfigs.ts that produce { files, mode: ... } (refer to the return sites that
currently emit mode) or thread the mode into the FileListContent props and its
consumer components (update FileListContent's prop type and its render/handler
logic to respect mode), and remove the duplicate unused occurrences (also adjust
the second occurrence analogous to the first).

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: a5a1adab-e263-4d95-970d-f75712a81009

📥 Commits

Reviewing files that changed from the base of the PR and between b906a26 and 56b3613.

📒 Files selected for processing (1)
  • src/components/chat/tools/configs/toolConfigs.ts

zuikusanxun and others added 2 commits March 21, 2026 03:34
- Extract shared `getFileCountTitle()` and `getFileList()` helpers to
  eliminate duplicated logic across Grep and Glob tool configs
- Use `Array.isArray()` guard on `toolData.filenames` before accessing
  `.length`, preventing strings/objects from being treated as arrays
- Replace `as number` with `Number()` + `Number.isFinite()` for runtime
  type coercion; fall through to content parsing when value is invalid
  instead of returning a hardcoded "Found 0 files"
- Remove unused `mode` property from `getContentProps` return value as
  it is not consumed by `FileListContent`
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.

1 participant