Skip to content

feat(core): cap JIT context upward traversal at git root#23074

Merged
SandyTao520 merged 1 commit intomainfrom
st/feat/jit-git-root-traversal-ceiling
Mar 19, 2026
Merged

feat(core): cap JIT context upward traversal at git root#23074
SandyTao520 merged 1 commit intomainfrom
st/feat/jit-git-root-traversal-ceiling

Conversation

@SandyTao520
Copy link
Copy Markdown
Contributor

@SandyTao520 SandyTao520 commented Mar 19, 2026

Summary

Cap JIT context upward traversal at the git root instead of the trusted root. Also fix findProjectRoot to recognize .git files (submodules/worktrees), not just .git directories.

Details

Both getEnvironmentMemoryPaths (Tier 2) and loadJitSubdirectoryMemory (Tier 3) previously used the trusted root as the traversal ceiling for GEMINI.md discovery. This was too narrow — Tier 2 only checked the workspace root directory itself (since startDir === stopDir), and Tier 3 stopped at the deepest trusted root.

Changes:

  1. Git root as ceiling: Both functions now call findProjectRoot() to locate .git and use that as the traversal ceiling. This prevents walking into unrelated parent directories while still picking up GEMINI.md files between the trusted root and the git root.

  2. No-git fallback: When no git root is found (e.g., a quick script folder), the trusted root is used as the ceiling — preserving the old behavior so GEMINI.md files at the trusted root itself are still loaded.

  3. Submodule/worktree support: findProjectRoot now uses fs.access() instead of fs.lstat() + isDirectory(), so .git files (used by submodules and worktrees) are recognized as project roots. This was a pre-existing gap that affected all callers of findProjectRoot, including the non-JIT path.

Related Issues

Related to #18007

How to Validate

  1. Run npm test -w @google/gemini-cli-core -- src/utils/memoryDiscovery.test.ts — all 39 tests pass.
  2. Key test cases:
    • getEnvironmentMemoryPaths → "should traverse upward from trusted root to git root" — traversal reaches the git root but not beyond.
    • getEnvironmentMemoryPaths → "should fall back to trusted root as ceiling when no .git exists" — non-git dirs still load their own GEMINI.md.
    • getEnvironmentMemoryPaths → "should recognize .git as a file (submodules/worktrees)" — .git file is treated as a project root.
    • loadJitSubdirectoryMemory → "should fall back to trusted root as ceiling when no git root exists" — JIT still works in non-git dirs.
    • loadJitSubdirectoryMemory → "should use the deepest trusted root when multiple nested roots exist" — traversal reaches git root from the deepest trusted root.

Pre-Merge Checklist

  • Updated relevant documentation and README (if needed)
  • Added/updated tests (if needed)
  • Noted breaking changes (if any)
  • Validated on required platforms/methods:
    • MacOS
      • npm run
      • npx
      • Docker
      • Podman
      • Seatbelt
    • Windows
      • npm run
      • npx
      • Docker
    • Linux
      • npm run
      • npx
      • Docker

@SandyTao520 SandyTao520 requested a review from a team as a code owner March 19, 2026 06:15
@gemini-cli
Copy link
Copy Markdown
Contributor

gemini-cli bot commented Mar 19, 2026

Hi @SandyTao520, thank you so much for your contribution to Gemini CLI! We really appreciate the time and effort you've put into this.

We're making some updates to our contribution process to improve how we track and review changes. Please take a moment to review our recent discussion post: Improving Our Contribution Process & Introducing New Guidelines.

Key Update: Starting January 26, 2026, the Gemini CLI project will require all pull requests to be associated with an existing issue. Any pull requests not linked to an issue by that date will be automatically closed.

Thank you for your understanding and for being a part of our community!

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request refines how JIT context memory is discovered by aligning the upward traversal boundary with the project's git root. Previously, traversal was limited by a "trusted root," which could lead to either overly narrow or broad context discovery. By using the git root as the ceiling, and skipping traversal altogether if no git root is present, the system now more intelligently identifies relevant GEMINI.md files, ensuring that the provided context is more precise and project-specific.

Highlights

  • Git Root as Traversal Ceiling: Changed the upward traversal ceiling for JIT context discovery from the "trusted root" to the "git root" for both getEnvironmentMemoryPaths (Tier 2) and loadJitSubdirectoryMemory (Tier 3) functions.
  • No Traversal Without Git Root: Introduced a new behavior where upward traversal for JIT context discovery is entirely skipped if no .git directory (project root) is found.
  • Improved Context Discovery: This ensures that GEMINI.md files are discovered more accurately within the scope of the project's version control system, preventing irrelevant files from being included or relevant files from being missed.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

The pull request successfully implements the feature to cap JIT context upward traversal at the git root. The changes in memoryDiscovery.ts correctly modify getEnvironmentMemoryPaths and loadJitSubdirectoryMemory to use the findProjectRoot() function and limit traversal to the detected git repository root. If no git root is found, the upward traversal is appropriately skipped. The accompanying test updates in memoryDiscovery.test.ts accurately reflect this new behavior, ensuring the functionality works as intended. The debug logging has also been updated to provide clearer information about the traversal boundaries. The implementation is clean and directly addresses the problem described in the PR description.

@github-actions
Copy link
Copy Markdown

github-actions bot commented Mar 19, 2026

Size Change: +340 B (0%)

Total Size: 26.2 MB

Filename Size Change
./bundle/chunk-FWNARTNB.js 0 B -13.5 MB (removed) 🏆
./bundle/chunk-G4TMH6EN.js 0 B -1.95 MB (removed) 🏆
./bundle/chunk-O54T47DO.js 0 B -3.63 MB (removed) 🏆
./bundle/core-AKUWIBBD.js 0 B -41.6 kB (removed) 🏆
./bundle/devtoolsService-6X7QVMAD.js 0 B -27.7 kB (removed) 🏆
./bundle/interactiveCli-W3LIVSYQ.js 0 B -1.61 MB (removed) 🏆
./bundle/oauth2-provider-PXTDFJXM.js 0 B -9.19 kB (removed) 🏆
./bundle/chunk-4UQJNNJU.js 3.63 MB +3.63 MB (new file) 🆕
./bundle/chunk-NT66OELL.js 13.5 MB +13.5 MB (new file) 🆕
./bundle/chunk-TCYGHW2X.js 1.95 MB +1.95 MB (new file) 🆕
./bundle/core-R4KYIITH.js 41.6 kB +41.6 kB (new file) 🆕
./bundle/devtoolsService-DB5TWP2H.js 27.7 kB +27.7 kB (new file) 🆕
./bundle/interactiveCli-HKJRUPBG.js 1.61 MB +1.61 MB (new file) 🆕
./bundle/oauth2-provider-PYVYKU5D.js 9.19 kB +9.19 kB (new file) 🆕
ℹ️ View Unchanged
Filename Size Change
./bundle/chunk-34MYV7JD.js 2.45 kB 0 B
./bundle/chunk-37ZTTFQF.js 966 kB 0 B
./bundle/chunk-5AUYMPVF.js 858 B 0 B
./bundle/chunk-664ZODQF.js 124 kB 0 B
./bundle/chunk-DAHVX5MI.js 206 kB 0 B
./bundle/chunk-IUUIT4SU.js 56.5 kB 0 B
./bundle/chunk-RJTRUG2J.js 39.8 kB 0 B
./bundle/devtools-36NN55EP.js 696 kB 0 B
./bundle/dist-T73EYRDX.js 356 B 0 B
./bundle/gemini.js 697 kB 0 B
./bundle/getMachineId-bsd-TXG52NKR.js 1.55 kB 0 B
./bundle/getMachineId-darwin-7OE4DDZ6.js 1.55 kB 0 B
./bundle/getMachineId-linux-SHIFKOOX.js 1.34 kB 0 B
./bundle/getMachineId-unsupported-5U5DOEYY.js 1.06 kB 0 B
./bundle/getMachineId-win-6KLLGOI4.js 1.72 kB 0 B
./bundle/memoryDiscovery-KKSSU3SI.js 0 B -922 B (removed) 🏆
./bundle/multipart-parser-KPBZEGQU.js 11.7 kB 0 B
./bundle/node_modules/@google/gemini-cli-devtools/dist/client/main.js 221 kB 0 B
./bundle/node_modules/@google/gemini-cli-devtools/dist/src/_client-assets.js 227 kB 0 B
./bundle/node_modules/@google/gemini-cli-devtools/dist/src/index.js 11.5 kB 0 B
./bundle/node_modules/@google/gemini-cli-devtools/dist/src/types.js 132 B 0 B
./bundle/sandbox-macos-permissive-open.sb 890 B 0 B
./bundle/sandbox-macos-permissive-proxied.sb 1.31 kB 0 B
./bundle/sandbox-macos-restrictive-open.sb 3.36 kB 0 B
./bundle/sandbox-macos-restrictive-proxied.sb 3.56 kB 0 B
./bundle/sandbox-macos-strict-open.sb 4.82 kB 0 B
./bundle/sandbox-macos-strict-proxied.sb 5.02 kB 0 B
./bundle/src-QVCVGIUX.js 47 kB 0 B
./bundle/tree-sitter-7U6MW5PS.js 274 kB 0 B
./bundle/tree-sitter-bash-34ZGLXVX.js 1.84 MB 0 B
./bundle/undici-4X2YZID5.js 360 B 0 B
./bundle/memoryDiscovery-C47CWCMG.js 922 B +922 B (new file) 🆕

compressed-size-action

@SandyTao520 SandyTao520 force-pushed the st/feat/jit-git-root-traversal-ceiling branch from 11cf03f to 1dae438 Compare March 19, 2026 06:24
@gemini-cli gemini-cli bot added the area/unknown Triage automation assigns this label to issues that it is unable to classify label Mar 19, 2026
Previously, JIT context (getEnvironmentMemoryPaths and
loadJitSubdirectoryMemory) used the trusted root as the traversal
ceiling, which could be too narrow. Change both functions to find the
git project root via findProjectRoot and use it as the ceiling instead.

If no git root exists, skip upward traversal entirely to avoid walking
into unrelated parent directories.
@SandyTao520 SandyTao520 force-pushed the st/feat/jit-git-root-traversal-ceiling branch from 1dae438 to c5594c1 Compare March 19, 2026 16:47
@SandyTao520 SandyTao520 enabled auto-merge March 19, 2026 16:48
@SandyTao520 SandyTao520 added this pull request to the merge queue Mar 19, 2026
Merged via the queue into main with commit 4e5dfd0 Mar 19, 2026
28 checks passed
@SandyTao520 SandyTao520 deleted the st/feat/jit-git-root-traversal-ceiling branch March 19, 2026 18:06
@SandyTao520
Copy link
Copy Markdown
Contributor Author

/patch preview

@github-actions
Copy link
Copy Markdown

🚀 [Step 1/4] Patch workflow(s) waiting for approval!

📋 Details:

  • Channels: preview
  • Commit: 4e5dfd0cb75509fdcddf4ba478581cb39c806a68
  • Workflows Created: 1

⏳ Status: The patch creation workflow has been triggered and is waiting for deployment approval. Please visit the specific workflow links below and approve the runs.

🔗 Track Progress:

@github-actions
Copy link
Copy Markdown

🚀 [Step 2/4] Patch PR Created!

📋 Patch Details:

📝 Next Steps:

  1. Review and approve the hotfix PR: #23134
  2. Once merged, the patch release will automatically trigger
  3. You'll receive updates here when the release completes

🔗 Track Progress:

@github-actions
Copy link
Copy Markdown

🚀 [Step 3/4] Patch Release Waiting for Approval!

📋 Release Details:

  • Environment: prod
  • Channel: preview → publishing to npm tag preview
  • Version: v0.35.0-preview.1
  • Hotfix PR: Merged ✅
  • Release Branch: release/v0.35.0-preview.1-pr-23074

⏳ Status: The patch release has been triggered and is waiting for deployment approval. Please visit the specific workflow run link below and approve the deployment. You'll receive another update when it completes.

🔗 Track Progress:

@github-actions
Copy link
Copy Markdown

[Step 4/4] Patch Release Complete!

📦 Release Details:

🎉 Status: Your patch has been successfully released and published to npm!

📝 What's Available:

🔗 Links:

ProthamD pushed a commit to ProthamD/gemini-cli that referenced this pull request Mar 29, 2026
warrenzhu25 pushed a commit to warrenzhu25/gemini-cli that referenced this pull request Apr 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/unknown Triage automation assigns this label to issues that it is unable to classify

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants