Skip to content

bug: read-many-files treats substring overlap as 'explicitly requested' binary asset, inlining unrequested images #29045

Closed

Description

@aniruddhaadak80

What happened?

read-many-files decides whether a matched binary asset was "explicitly requested" using substring containment between the glob pattern string and the file's stem/extension — not actual pattern matching. Any overlap between a directory-name fragment in the pattern and a binary file's stem marks the asset as requested, so it gets embedded as a base64 inlineData part even though the user never asked for binaries. The two checks are also inconsistently cased (extension compare is case-insensitive, stem compare case-sensitive).

Affected code

packages/core/src/tools/read-many-files.ts:306-310:

const requestedExplicitly = include.some(
  (pattern: string) =>
    pattern.toLowerCase().includes(fileExtension) ||
    pattern.includes(fileNameWithoutExtension),
);

How can this be reproduced?

{ "include": ["**/*report*/**"], "exclude": [] }

With a matched file quarterlyreport.png: stem quarterlyreport is a substring of the pattern → requestedExplicitly === true → the PNG is inlined as an image part, consuming large context and surprising the user.

Conversely, a genuine request like "assets/logo.png" fails the intent for logo.PNG on case-sensitive filesystems due to the asymmetric casing.

What did you expect to happen?

"Explicitly requested" should mean the include pattern actually matches this specific binary file (e.g., run the pattern through the same matcher used for discovery), not fuzzy string overlap.

Impact

Unexpected large binary payloads injected into model context; token bloat; potential policy surprises for users who believed they only requested text.

Suggested direction

Match the file's path against each include pattern with the project's glob engine (minimatch/glob) instead of includes(), normalizing case consistently.


Found by source audit on current main (commit 5411f113c); platform-independent. No open issue/PR covering this was found (searched: read-many-files binary explicitly requested).

Activity

  1. added
    status/need-triage
    area/agent
    on Aug 24, 2026
  2. Jokasa7 commented on Aug 29, 2026

    @Jokasa7

    I reviewed the requestedExplicitly branch in packages/core/src/tools/read-many-files.ts. At the moment it compares the raw include-pattern string with the file’s stem and extension, which explains why incidental text overlap can opt a binary file into inline content. I’d keep discovery, exclusions, and binary rendering unchanged, and replace only this intent check with an actual match of the normalized relative file path using the repository’s existing glob semantics.

    In packages/core/src/tools/read-many-files.test.ts, I’d add the reported false-positive shape (**/*report*/** alongside quarterlyreport.png) and verify that the image is not treated as explicitly requested. A second case would use an actual path such as assets/logo.png and verify that explicit binary inclusion still works; I’d also follow the project’s existing expectations for path separators and case handling.

    There is one semantic choice worth confirming before implementation: should “explicitly requested” mean a real glob match against the file path, or only a literal file-path include? If maintainers prefer the former, I’m happy to send the focused fix and regression tests after approval, with the targeted suite and npm run preflight completed first.

  3. added
    area/core
    priority/p2
    and removed
    status/need-triage
    area/agent
    on Aug 29, 2026
  4. gemini-cli commented on Aug 29, 2026

    @gemini-cli
    Contributor

    This is a bug in the file reading tool where it incorrectly inlines binary assets. Please provide your CLI version.

  5. added theissue type on Aug 29, 2026
  6. added
    effort/small
    on Aug 29, 2026
  7. gemini-cli commented on Aug 29, 2026

    @gemini-cli
    Contributor

    Effort Analysis:
    The ReadManyFilesToolInvocation.execute method in packages/core/src/tools/read-many-files.ts incorrectly determines if a binary asset was 'explicitly requested' by using String.prototype.includes() on the glob pattern string (lines 306-310). This results in incorrect behavior where any substring overlap between the pattern and the file's stem or extension triggers a match. The fix involves replacing this includes() logic with proper glob matching against the actual file path using the project's established matching libraries (glob or picomatch, both already available in packages/core). This is a highly localized, single-file logic error with no platform dependencies or complex state management, mapping directly to the 'effort/small' guideline.

  8. 2 remaining items

  9. added and removed
    area/core
    on Sep 2, 2026
  10. gemini-cli commented on Sep 2, 2026

    @gemini-cli
    Contributor

    The read_file and read_many_files tools are failing to correctly handle subdirectory paths on Windows. Additionally, the CLI version (0.1.7) is significantly outdated; please verify if this issue persists in the latest version (0.59.0).

  11. added
    area/core
    priority/p1
    and removed
    area/agent
    priority/p2
    on Sep 2, 2026
  12. priyanshu1976 commented on Oct 16, 2026

    @priyanshu1976

    I've opened #29783 for this. It replaces the substring check with real glob matching (picomatch, already a core dependency): an asset counts as requested only when an include pattern actually matches it and the pattern's last segment names its extension or name. Note the repro in the description doesn't trigger the bug (the old check tests stem-in-pattern), so the PR uses cases that do, e.g. '/report/' inlining reports/report.png. Regression tests included; CLA is signed. Could a maintainer add help wanted?

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

    area/coreeffort/smallkind/bugpriority/p1status/manual-triage

    Type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions